# Introduction

## Goals

- Install and configure Nagios disk space plugin

## Notes

- Commands preceded with “$” imply that you should execute the command as a general user - not as root.
- Commands preceded with “#” imply that you should be working as root.
- Commands with more specific command lines (e.g. “rtrX>” or “mysql>”) imply that you are executing commands on remote equipment, or within another program.

# Exercises

It is possible to alert on disk space thresholds with the existing check_snmp plugin, but it is rather fiddly to set up (because, for example, you have to manually map the filesystem mountpoint to the OID index). So the approach given here is to install a new plugin, which handles this for you.

## 1. Create a plugin script

Create a file /usr/local/bin/check_hrstorage.pl with the following contents (the #! line must be the very first line in the file)

    #!/usr/bin/perl -w

    unless ($ARGV[2]) {
      print STDERR "Usage: $0 <host> <community> <mountpoint> [<warn%> [<crit%>]]\n";
      exit 3;
    }

    my $res = `snmpwalk -On -Oq -r 2 -t 1 -c '$ARGV[1]' -v2c '$ARGV[0]' .1.3.6.1.2.1.25.2.3`;
    unless ($res =~ /^\.1.3.6/) {
      print "Unable to contact via SNMP\n";
      exit 3;
    }

    my $table = {};
    my $index;
    foreach my $line (split(/\n/,$res)) {
      my ($k,$v) = split(/ /,$line,2);
      $table->{$k} = $v;
      if ($v eq $ARGV[2] && $k =~ /^.1.3.6.1.2.1.25.2.3.1.3\.(\d+)$/) {
        $index = $1;
      }
    }

    unless ($index) {
      print "Unable to find mountpoint $ARGV[2]\n";
      exit 3;
    }

    my $size = $table->{".1.3.6.1.2.1.25.2.3.1.5.$index"};
    $size += 4294967296 if $size < 0;
    my $used = $table->{".1.3.6.1.2.1.25.2.3.1.6.$index"};
    $used += 4294967296 if $used < 0;
    my $percent = $used * 100.0 / $size;

    my $block = $table->{".1.3.6.1.2.1.25.2.3.1.4.$index"};
    if ($block =~ /^(\d+)/) {
      $block = $1;
      $size = $size * 1.0 * $block / 1024 / 1024;
      $used = $used * 1.0 * $block / 1024 / 1024;
    }
    printf "%.2f%% used (%d out of %d MB)\n", $percent, $used, $size;
    if ($ARGV[4] && $percent >= $ARGV[4]) {
      exit 2;  # Critical
    }
    if ($ARGV[3] && $percent >= $ARGV[3]) {
      exit 1;  # Warning
    }
    exit 0;

Make it executable using the following command:

    # chmod +x /usr/local/bin/check_hrstorage.pl

## 2. Configure the plugin

Create a new configuration file:

    # editor /etc/nagios-plugins/config/hrstorage.cfg

    define command {
        command_name check_hrstorage
        command_line /usr/local/bin/check_hrstorage.pl '$HOSTADDRESS$' '$ARG1$' '$ARG2$' '$ARG3$' '$ARG4$'
    }

## 3. Add a service check

A service check can now be added to an individual host, or to a hostgroup. For example:

    define service {
            host_name                       noc
            service_description             Disk space /
            check_command                   check_hrstorage!NetManage!/!80!90
            use                             generic-service
            notification_interval           0
    }

The parameters to this check_hrstorage are:

- SNMP community
- Filesystem mount point
- Warning threshold (%)
- Critical threshold (%)