List disks on linux server for nagios.

I wrote this simple script that I named list_disks.pl to help me configure my hosts. I use it as a NRPE check to query a server for all of its available disks, and parse the output to write configuration files. It is useful for automating host configuration files when the hosts don’t have consistent naming schemes or have different numbers of disks or partitions. Make note of the regular expression. If this regular expression won’t work for you, make sure to update it.

#!/usr/bin/perl
use strict;
my $i_disk = 0;
my $i_use = 4;
my $i_mount = 5;
my $delim = "+";
my $result = "OK: ";
my @data = `df -h`;
shift @data;
foreach my $row (@data) {
  my @cols = split(/\s+/, $row);
  if ($cols[0] =~ m/.*sda[0-9]+/) {
    $result .= $cols[$i_disk] 
      . "," . $cols[$i_use] . "," 
      . $cols[$i_mount] . $delim;
  }
}
chop $result;
print $result;

To use the script move it to the libexec directory on the remote host. Update the nrpe.cfg file to have an entry for this remote command. My entry looks like the following.

command[list_disks]=/usr/local/nagios/libexec/list_disks.pl

Now on the machine running the nagios core you can run the following command.

/usr/local/nagios/libexec/check_nrpe -n -H gnucom.cc -c list_disks

The expected output is something like this, depending on what disks and partitions you have available on your remote server. The format can be easily changed, but I haven’t made any special considerations for this in the code itself.

OK: /dev/sda5,30%,/+/dev/sda3,11%,/tmp+/dev/sda1,25%,/boot

For help on parsing it, see my other post here: http://blog.gnucom.cc/2010/parse-list_disk-pl-to-configure-disk-checks-for-nagios/.

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