Parse list_disk.pl to configure disk checks for nagios.

I wrote a post earlier regarding listing the available disks on a remote server using list_disks.pl and NRPE. This time, I’m going to provide a code snippet that you can use to parse that output. The code isn’t very complicated, so I’ll just provide it for your uses.

my $list_disks = 'list_disks';
my $libexec = '/usr/local/nagios/libexec';
my $disk_check = "$libexec/check_nrpe -n -H $host -c $list_disks";
# Execute the system command that we built with the parts from above
my $result = `$disk_check`;
#As of writing this script, list_disks.pl returns a string 
#  that follows this format:
#  OK: /dev/sda3,46%,/+/dev/sda5,8%,/tmp+/dev/sda1,37%,/boot
if ($result =~ m/OK: (.*)/)
{
  my @parts = split(/\+/, $1);
  foreach my $part (@parts)
  {
     my @elements = split(/,/, $part);
     # Write the service command to check this disk 
     # $elements[0] - the partition
     # $elements[1] - currently used space, in case you want it
     # $elements[2] - mount point
  }
}
else
{
  #This must mean the remote host doesn't have 
  # NRPE or doesn't have list_disks.pl installed
}

The code itself is pretty easy to understand, the only complicated part may be the string we’re parsing. Simply put, this is just an arbitrary string that I chose that you can change however you would like in list_disks.pl.

I use this code snippet to generate disk checks using NRPE on the fly: I don’t need to know which disks a server has or what partitions are on those disks, I simply need to run my NRPE check first and parse its output. The only obvious downside to this system is that NRPE must be up and running on the remote host and the list_disks.pl script must be installed before you ever think about running this script. However, this downside is really a moot when I consider how much time and sanity this little script saves me.

This entry was posted in Nagios and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.