Monitoring Xen via SNMP

I wanted to monitor disk I/O and CPU usage for xen’s without running SNMP on each xen domain. I couldn’t find anything out of the box to do this (I’m the first do want to do this? surely there’s something?!) – so… here’s my own way of doing it. It involved hooking a script in to your dom0’s snmp server, and then monitoring whatever tool you like. I have a download at the end of the post for cacti.

This article has been updated: Monitoring Xen via SNMP – update

On your Xen host, we’ll create a script that outputs data from xentop… and here it is:

perl really isn’t a strong point for me, so the below might be badly written, sorry 🙂

#!/usr/bin/perl -w

use strict;

# declare...
sub trim($);

# we need to run 2 itterations 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);

# this looks for the start of the 2nd output section
foreach my $line (@result)
{
  if ($line =~ m/^xentop - /)
  {
    last;
  }
  shift(@result);
}


# the next 3 lines are headings..
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: %dn",
    $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;
}

Next step is making this output available via SNMP, so add this to your snmpd.conf

  extend xen-stats   /usr/bin/sudo /usr/local/bin/xen_stats.pl

Restart SNMP, and test:

snmpwalk -v2c -c public  srv-10 NET-SNMP-EXTEND-MIB::nsExtendOutputFull."xen-stats"

To get graphs showing in cacti, you can use my templates and script that I’ve packaged up here for you:
xen-cacti-stats.tar.gz

4 thoughts on “Monitoring Xen via SNMP”

  1. Hi,
    thanks for that nice work, but cacti says: Could not locate XML file. in the data querie..

    The xml is missing in your package. Please export it and reupp, please.

    Many thanks

  2. Hi,

    I love your solution, but I get the following error in cacti after importing your xml’s and adding associate the data query:

    + Running data query [10].
    + Found type = ‘4 ‘[script query].
    + Could not find data query XML file at ‘/usr/share/webapps/cacti/0.8.7b-r2/htdocs/resource/script_queries/xen_stats.xml’
    + Error parsing XML file into an array.
    + Could not find data query XML file at ‘/usr/share/webapps/cacti/0.8.7b-r2/htdocs/resource/script_queries/xen_stats.xml’
    + Could not find data query XML file at ‘/usr/share/webapps/cacti/0.8.7b-r2/htdocs/resource/script_queries/xen_stats.xml’
    + Could not find data query XML file at ‘/usr/share/webapps/cacti/0.8.7b-r2/htdocs/resource/script_queries/xen_stats.xml’

    Please contact me, cause I’d really like to implement this! 🙂

  3. I had to comment out the 3 lines to skip the “headers”. On my 3.1.x install of xen community, this was removing the first 3 domains from my results.

  4. Hi,

    I have the sma eproblem as Tom – it looks like the xen_stats.xml script query isn’t included in the tarball. Any chance of adding it, or uploading it ?

    Many thanks,

    -Mark

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.