Monitoring Xen via SNMP – update

In a previous post,
Monitoring Xen via SNMP, I documented a method for monitoring xens from dom0. This article shows a few improvements and bug fixes, and allows for monitoring a ‘cloud’ of domains.

The perl scripts posted before where buggy, here’s an improved version:

For Xen 3.1

#!/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);

shift(@result) while @result && $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: %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;
}

For Xen 3.2

#!/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);
shift(@result) while @result && $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: %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;
}

Another change I made was to run this script though cron, removing the need for sudo. Here’s my crontab:

 * * * * * root /usr/local/bin/xen_stats.pl > /tmp/xen-stats.new && mv /tmp/xen-stats.new /var/run/xen-stats

And the updated snmpd.conf:

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

I’ve attached some cacti stuff again, this time you will need to modify the file ‘scripts/xen_cloud_stats.php’ – it contains an array of your xen servers. The script uses APC (if available) to cache the SNMP data for 10 seconds, as cacti will call this script lots of times in the process of updating the RRDs every 5 minutes. The physical server your domains are on is hidden from cacti using this system – this is how I wanted it was we have automatic migrations going on.

xen_cloud.tar.gz

11 thoughts on “Monitoring Xen via SNMP – update”

  1. Hi,

    I am missing something.. but I try not to think too hard about PHP:

    + Executing script query ‘/usr/bin/php /usr/share/cacti/site/scripts/xen_cloud_stats.php query xen_name’
    + Found item [xen_name=’ Call to undefined function snmp_set_valueretrieval() in /usr/share/cacti/site/scripts/xen_cloud_stats.php on line 73′] index: Fatal error

  2. I am trying to gauge how stressed the physical CPU is on any one dom0.

    If I were to add a TOTAL cdef on the cpu_sec, would it all add up to 1 second on average – for a single core system? and 4 seconds for a quad core?

    I have the graph – but I can’t share it with you here..

    C.

  3. Hello,

    I have installed cacti to make use of your work here. When i try to import the template ‘cacti_data_query_xen_cloud.xml’ file into cacti i get an error: “Error: XML: Hash version does not exist.”

    Also if i run the command from the previos posts: ‘/usr/bin/php /usr/share/cacti/site/scripts/xen_cloud_stats.php query xen_name’

    i get the error: “Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or ‘}’ in /usr/share/cacti/site/scripts/xen_cload_stats.php on line 27”

    am i missing something?

  4. if you’re trying to use PHP4, that would probably explain it. I’m not sure I do anything that’s specifically PHP4, but I WILL NEVER write PHP4 compatable code ever again 😉

    I’ve no idea what the hash problem is about, if you can’t get it working drop me a mail for my consultancy rates. Good luck!

  5. Hi Ian.

    I’m using Xen 3.1 on FC8 and I can’t do your template works for my Cacti.

    Could you help me?

    I’ve done everything you said here on Dom0. That’s right?
    Do I have to do something on DomUs?

    I would be very grateful if you could help me by mail. I’ll use it in my Graduation Project.

    Thanks!

  6. When I use the snmpwalk command I got the following message.

    [root]# snmpwalk -v2c -c public x1.x2.x3.x4 NET-SNMP-EXTEND-MIB::nsExtendOutputFull.”xen-stats”
    NET-SNMP-EXTEND-MIB::nsExtendOutputFull.”xen-stats” = No more variables left in this MIB View (It is past the end of the MIB tree)

    Thanks for your help

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.