Xen VMs performance collection

Unlike VMware Server, Xen’s HyperVisor does not allow an easy collection of performance information. The management machine, called “Domain-0″ is actually a privileged virtual machine, and thus – get its own small share of CPUs and RAM. Collecting performance information on it will lead to, well, collecting performance information for a single VM, and not the whole bunch.

Local tools, such as “xentop” allows collection of information, however, combining this with Cacti, or any other SNMP-based collection tool is a bit tricky.

A great solution is provided by Ian P. Christian in his blog post about Xen monitoring. He has created a Perl script to collect information. I have taken the liberty to fix several minor things with his permission. The modified scripts are presented below. Name the script (according to your version of Xen) “/usr/local/bin/xen_stats.pl” and set it to be executable:

For Xen 3.1

?Download xen_stats.pl
#!/usr/bin/perl -w
 
use strict;
 
# declare...
sub trim($);
#<a href="/blog/files/xen_cloud.tar.gz" title="xen_cloud.tar.gz" target="_blank">xen_cloud.tar.gz</a>
# we need to run 2 iterations because CPU stats show 0% on the first, and I'm putting .1 second betwen them to speed it up
my @result = split(/\n/, `xentop -b -i 2 -d.1`);
 
# remove the first line
shift(@result);
 
shift(@result) while @result &amp;&amp; $result[0] !~ /^xentop - /;
 
# the next 3 lines are headings..
shift(@result);
shift(@result);
shift(@result);
shift(@result);
 
foreach my $line (@result)
{
  my @xenInfo = split(/[\t ]+/, trim($line));
  printf("name: %s, cpu_sec: %d, cpu_percent: %.2f, vbd_rd: %d, vbd_wr: %d\n",
    $xenInfo[0],
    $xenInfo[2],
    $xenInfo[3],
    $xenInfo[14],
    $xenInfo[15]
    );
}
 
# trims leading and trailing whitespace
sub trim($)
{
  my $string = shift;
  $string =~ s/^\s+//;
  $string =~ s/\s+$//;
  return $string;
}

For Xen 3.2 and Xen 3.3

?Download xen_stats.pl
#!/usr/bin/perl -w
 
use strict;
 
# declare…
sub trim($);
 
# we need to run 2 iterations because CPU stats show 0% on the first, and I’m putting .1 second between them to speed it up
my @result = split(/\n/, `/usr/sbin/xentop -b -i 2 -d.1`);
 
# remove the first line
shift(@result);
shift(@result) while @result &amp;&amp; $result[0] !~ /^[\t ]+NAME/;
shift(@result);
 
foreach my $line (@result)
{
        my @xenInfo = split(/[\t ]+/, trim($line));
        printf(“name: %s, cpu_sec: %d, cpu_percent: %.2f, vbd_rd: %d, vbd_wr: %d\n,
        $xenInfo[0],
        $xenInfo[2],
        $xenInfo[3],
        $xenInfo[14],
        $xenInfo[15]
        );
}
# trims leading and trailing whitespace
sub trim($)
{
        my $string = shift;
        $string =~ s/^\s+//;
        $string =~ s/\s+$//;
        return $string;
}

Cron settings for Domain-0

Create a file “/etc/cron.d/xenstat” with the following contents:

# This will run xen_stats.pl every minute
*/1 * * * * root /usr/local/bin/xen_stats.pl > /tmp/xen-stats.new && cat /tmp/xen-stats.new > /var/run/xen-stats

SNMP settings for Domain-0

Add the line below to “/etc/snmp/snmpd.conf” and then restart the snmpd service

extend xen-stats   /bin/cat /var/run/xen-stats

Cacti

I reduced Ian Cacti script to be based on a per-server setup, meaning this script gets the host (dom-0) name from Cacti, but cannot support live migrations. I will try to deal with combining live migrations with Cacti in the future.

Download and extract my modified xen_cloud.tar.gz file. Extract it, place the script and config in its relevant location, and import the template into Cacti. It should work like charm.

A note – the PHP script will work only on PHP5 and above. Works flawlessly on Centos5.2 for me.

RHEL5 disk performance collection (sysstat)...

Related posts brought to you by Yet Another Related Posts Plugin.

Tags: , , , , , , ,

12 Responses to “Xen VMs performance collection”

  1. Monitoring Xen via SNMP - update - Ian P. Christian's personal blog Says:

    [...] a few improvements and bug fixes, and allows for monitoring a ‘cloud’ of domains. Please view http://www.tournament.org.il/run/?p=486 for an updated [...]

  2. ill Says:

    With Xen 3.1 ‘/usr/sbin/xentop -b -i 2 -d.1′ – always returns Domain-0 CPU usage about 100%.
    And it should be full path to xentop since not all crontab jobs have right environment set.

    P.S. Thanks for the tool!

  3. ez-aton Says:

    Thanks for your comment.
    With Cacti, I tend to sample dom0 from other aspects, and not through this specific command. However, thanks for the info!

    Ez

  4. Yann Says:

    Hi,

    Nice job, but sadly the link to xen_cloud.tar.gz seems broken : “Error establishing a database connection”

    Cheers,

  5. ez-aton Says:

    Thanks for letting me know.
    There was a minor problem with the server which caused its backend DB to fail. It was fixed, and should not operate correctly. I have tested it to work.
    Let me know if it doesn’t, please.

    Thanks
    Ez

  6. Roland Says:

    I’m getting this error:
    Error: XML: Hash version does not exist.
    I think this is because you exported template with different version of cacti. Could you let me know which version you used?

  7. ez-aton Says:

    I use 0.8.7c
    Weird…

    Ez

  8. rupertt Says:

    Hi,

    im having trouble getting things to work together.
    I could import the scripts into cacti and copied the 2 scripts into the cactio folders,
    than i used your script on the dom0, and can snmpwalk onto that machine:

    # snmpwalk -v2c -c public 172.17.4.101 NET-SNMP-EXTEND-MIB::nsExtendOutputFull.\”xen-stats\”
    NET-SNMP-EXTEND-MIB::nsExtendOutputFull.”xen-stats” = STRING: name: Domain-0, cpu_sec: 1563511, cpu_percent: 97.10, vbd_rd: 0, vbd_wr: 0
    name: monitoring-2, cpu_sec: 3833949, cpu_percent: 194.00, vbd_rd: 14746, vbd_wr: 20419
    name: uat-vm2, cpu_sec: 154919, cpu_percent: 0.00, vbd_rd: 25191, vbd_wr: 88203
    name: uat-vm4, cpu_sec: 94536, cpu_percent: 0.00, vbd_rd: 16, vbd_wr: 4
    name: vpn-vm2, cpu_sec: 110325, cpu_percent: 0.00, vbd_rd: 21, vbd_wr: 1

    But within cacti no graphs will be selectable when I try to create some,
    this is the output of the verbose query:

    + Running data query [19].
    + Found type = ‘4 ‘[script query].
    + Found data query XML file at ‘/var/www/cacti/resource/script_queries/xen_cloud_stats.xml’
    + XML file parsed ok.
    + Executing script for list of indexes ‘/usr/bin/php /var/www/cacti/scripts/xen_cloud_stats.php index’
    + Executing script query ‘/usr/bin/php /var/www/cacti/scripts/xen_cloud_stats.php query xen_name’
    + Found item [xen_name=''] index:
    + Found data query XML file at ‘/var/www/cacti/resource/script_queries/xen_cloud_stats.xml’
    + Found data query XML file at ‘/var/www/cacti/resource/script_queries/xen_cloud_stats.xml’
    + Found data query XML file at ‘/var/www/cacti/resource/script_queries/xen_cloud_stats.xml’

    Do i miss anything?

    greetings

  9. Mark Ruys Says:

    If you get errors like:

    NET-SNMP-EXTEND-MIB::nsExtendOutputFull.”xen-stats” = STRING: /bin/cat: /var/run/xen-stats: Permission denied

    it might be that SELinux is blocking access. I created this policy using audit2allow:

    module snmpd 1.0;

    require {
    type var_run_t;
    type snmpd_t;
    class file { read getattr };
    }

    #============= snmpd_t ==============
    allow snmpd_t var_run_t:file { read getattr };

    compile & load it, and snmpd is able to read the xen-stats file.

  10. AndreaConsadori Says:

    Hi, i try this script on xen express 5.5 but i’ve this error

    Operator or semicolon missing before &amp at ./xen-stat.pl line 14.
    Ambiguous use of & resolved as operator & at ./xen-stat.pl line 14.
    Useless use of not in void context at ./xen-stat.pl line 14.
    Bareword “amp” not allowed while “strict subs” in use at ./xen-stat.pl line 14.
    Execution of ./xen-stat.pl aborted due to compilation errors.

    how can i fix it?

  11. ez-aton Says:

    This script is meant for Xen Community, and was tested on Centos5 versions. I did not try to adjust it to run on XS just yet.
    Sorry.

  12. Mark Ruys Says:

    > With Xen 3.1 ‘/usr/sbin/xentop -b -i 2 -d.1′ – always returns Domain-0 CPU usage about 100%.
    > And it should be full path to xentop since not all crontab jobs have right environment set.

    Change “-d.1″ to “-d 1″. Apparently, some xentop implementations only accept seconds, not tenths of seconds.

Leave a Reply