Virsh script fun

I can’t remember how it came up the other day, but the sysadmin and I were talking about the virtual environment we have, and how I have moved a few things around, decommissioned a few machines and in general moved a few things around. When he went to go looking for them, they weren’t in the spot that he had written down or remembered. It’s been so easy with just one person (him) in the past, it’s been a bit of a challenge to keep things on the same page.

Long story short, the idea came up that it would be awesome if we had something that could generate a list of what VM is on what machine, see what resources are in use, etc. This would be useful since we are spinning up a new environment in a little bit, and new things need to be added occasionally. I have been reviewing security controls and documentation a lot as of late, so I jumped on the idea to work on something different for a bit.

Here’s the script that I wrote:

virsh list --all --title
# Get total CPUs
virsh nodeinfo | grep "CPU(s)" | awk '{print "Total CPUs on server: " $2}'
# Get allocated CPUs
virsh list | awk '{print $1}' | grep -oIE [0-9]* | while read machine; do virsh dominfo ${machine} | grep "CPU.s"; done | awk 'BEGIN {cpu=0;} {cpu=cpu+$2;} END {print "Total CPUs in use: " cpu;}'
# Get total Memory
free -g | grep Mem | awk '{print "Total system memory in GB: " $2'}
# Get allocated memory in GB
virsh list | awk '{print $1}' | grep -oIE [0-9]* | while read machine; do virsh dominfo ${machine} | grep "Used memory"; done | awk 'BEGIN {mem=0;} {mem=mem+$3;} END {print "Total memory allocated to VMs: " mem/1024/1024;}'

It produces output like this:

 Id     Name                           State      Title
  2     machine1                       running
  4     machine2                       running
  10    machine3                       running    machine3
  21    machine4                       running
  25    machine5                       running    Service1
  26    machine6                       running    Service2
  27    machine7                       running    Service3
  -     machine8                       shut off   Decomissioned

Total CPUs on server: 48
Total CPUs in use: 36
Total system memory in GB: 125
Total memory allocated to VMs: 72

I’ve removed the system name and title since they aren’t needed to demonstrate it’s functionality. Overall I was pleased with it. I could have made the script more readable and friendly, but it was something quick and dirty. I used ansible to upload it to all the VM’s and then used the following command to execute it. Yeah, I know it could have been a .yaml file, but again, quick and dirty to get the job done. And yes, afterwards I added a page for it in our documentation.

ansible xen -i inventory.txt [other options ] -u [user] -m shell -a "/root/bin/" -f 1 

This was great because it went through each of the VM hosts and ran the script one at a time and generated the output. I sent it to him and it was exactly the sort of thing he was looking for. It made my day.

Leave a Reply