VirtualBox SDK Machine Status

Posted on April 22, 2010

0


I have taken to using VirtualBox in my day to day development work. The reason for this is it provides a nicely isolated development sandbox which I can go ahead and clutter up with the usual tools, then when things go wrong I just have to trash the machine and clone from an existing image that I have pre-made. So far this has made life much easier for me.

My preferred approach to running my machines is to start them up in headless mode and then shell into them using a static IP assigned through the VirtualBox host-only network. The only issue with using headless mode is that there is potentially no indication of the state of the machine, basically when can I shell into my machine? Since I was already using conky to report the status of my host machine I thought that I would add to my conkyrc so that I could see the state of play with regards to VirtualBox and the machines available to it.

I decided to make use of the VirtualBox Python SDK to write a small application that can report on the status of my machines. The deb installation of VirtualBox supplied by Sun/Oracle contains the SDK as well, I am unsure if this is the case for the version of VirtualBox OSE available on the Ubuntu repositories. Also I don’t know what the state of play is with other linux distributions. You may want to download the SDK anyway as this includes all of the documentation for using the API.

So with that out the way onto the script that I created to report the status of my machines;

#!/usr/bin/env python
from vboxapi import VirtualBoxManager
import getopt
import sys

opts, args = getopt.getopt(sys.argv[1:], "", ['conky', 'vbox-version'])
useConky = False
for o, a in opts:
        if o == '--conky':
                useConky = True

virtualBoxManager = VirtualBoxManager('XPCOM', None)
virtualBox = virtualBoxManager.vbox
for o, a in opts:
        if o == '--vbox-version':
                print virtualBox.version
                sys.exit(0)

machines = virtualBox.getMachines()
for machine in machines:
        machineName = machine.name
        machineStatus = "null"
        machineStatuses = {
                0: 'null',
                1: '${color red}powered off$color' if useConky else 'powered off',
                2: '${color red}saved$color' if useConky else 'saved',
                3: '${color yellow}teleported$color' if useConky else 'teleported',
                4: '${color red}aborted$color' if useConky else 'aborted',
                5: '${color green}running$color' if useConky else 'running',
                6: '${color yellow}paused$color' if useConky else 'paused',
                7: '${color red}stuck$color' if useConky else 'stuck',
                8: '${color blue}teleporting$color' if useConky else 'teleporting',
                9: '${color blue}live snapshotting$color' if useConky else 'live snapshotting',
                10: '${color blue}starting$color' if useConky else 'starting',
                11: '${color blue}stopping$color' if useConky else 'stopping',
                12: '${color blue}saving$color' if useConky else 'saving',
                13: '${color blue}restoring$color' if useConky else 'restoring'
                 }
        machineStatus = machineStatuses.get(machine.state)
        print machineName, "-", machineStatus

This script will accept the following arguments when executed;

virtualboxstatus [--conky] [--vbox-version]

  --conky
    Adds any additional colors for conky to render.

  --vbox-version
    Displays the installed version of VirtualBox then exits.

Once this script is made we just need to add it to our conkyrc in order to render the status of our machines. In my conkyrc I added the following;

${color red}VIRTUALBOX STATUS$color - Version ${execpi 600 ~/bin/virtualboxstatus --vbox-version}
${execpi 1 ~/bin/virtualboxstatus --conky}

Here’s a screenshot of this script in action;
VirtualBox status in conky

Advertisements
Posted in: virtualbox