Getting XenServer VM metrics in Java

XenServer is a product of Citrix that can run virtual machines on a set of physical hosts called a pool. There is an API that allows you to create programs that interact with the XenServer pool. There are some function calls that return metrics of the virtual machines and the physical hosts.

Let us have a look at retrieving the CPU load of a virtual machine (VM). This can be accomplished with the following API call (assuming that vm is of type com.xensource.xenapi.VM and connection is of type com.xensource.xenapi.Connection):

vm.getMetrics(connection).getVCPUsUtilisation(connection)

However, as many have noted, the answer is always 0.0. As of XenServer version 5.5, the way these metrics are stored has changed. The preferred way to get VM metrics now, is to make a direct HTTP call to the physical hosts. This call will return RRD XML files. This makes things more difficult because now there are multiple calls to make, one to the XenServer API and one call per physical host machine. And one needs to dig into these files to get the CPU load, instead of having the value returned in a single call.

There is a way to make the XenServer API behave like it did in version 5.0 and prior. We start by logging into the console of a physical machine and entering:

xe host-list

Note the UUID values of all the physical host machines. Now enter the following command for each UUID:

xe host-param-set uuid=<uuid found in previous command> other-config:rrd_update_interval=1

Now reboot all physical host machines and the VM metric command will work like a charm.

3 thoughts on “Getting XenServer VM metrics in Java

  1. Permalink  ⋅ Reply

    JoshG

    March 5, 2011 at 4:52am

    Awesome Thanks so much for this! I was wondering why I was getting 0.0 for all results!

  2. Permalink  ⋅ Reply

    Rob B

    April 19, 2012 at 4:32am

    Quick one-liner to enable rrd with interval of 1 second (On a newly installed box)

    xe host-param-set uuid=$(xe host-list –minimal) other-config:rrd_update_interval=1

    Enjoy 🙂

Leave a Reply

Your email will not be published. Name and Email fields are required.