| 1 | % Nagios Installation and Configuration | 
|---|
| 2 | % | 
|---|
| 3 |  | 
|---|
| 4 | # Introduction | 
|---|
| 5 |  | 
|---|
| 6 | ## Goals | 
|---|
| 7 |  | 
|---|
| 8 | * Install and configure Nagios disk space plugin | 
|---|
| 9 |  | 
|---|
| 10 | ## Notes | 
|---|
| 11 |  | 
|---|
| 12 | * Commands preceded with "$" imply that you should execute the command as | 
|---|
| 13 | a general user - not as root. | 
|---|
| 14 | * Commands preceded with "#" imply that you should be working as root. | 
|---|
| 15 | * Commands with more specific command lines (e.g. "rtrX>" or "mysql>") | 
|---|
| 16 | imply that you are executing commands on remote equipment, or within | 
|---|
| 17 | another program. | 
|---|
| 18 |  | 
|---|
| 19 | # Exercises | 
|---|
| 20 |  | 
|---|
| 21 | It is possible to alert on disk space thresholds with the existing | 
|---|
| 22 | check_snmp plugin, but it is rather fiddly to set up (because, for example, | 
|---|
| 23 | you have to manually map the filesystem mountpoint to the OID index).  So | 
|---|
| 24 | the approach given here is to install a new plugin, which handles this for | 
|---|
| 25 | you. | 
|---|
| 26 |  | 
|---|
| 27 |  | 
|---|
| 28 | ## 1. Create a plugin script | 
|---|
| 29 |  | 
|---|
| 30 | Create a file /usr/local/bin/check_hrstorage.pl with the following contents | 
|---|
| 31 | (the #! line must be the very first line in the file) | 
|---|
| 32 |  | 
|---|
| 33 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 
|---|
| 34 | #!/usr/bin/perl -w | 
|---|
| 35 |  | 
|---|
| 36 | unless ($ARGV[2]) { | 
|---|
| 37 | print STDERR "Usage: $0 <host> <community> <mountpoint> [<warn%> [<crit%>]]\n"; | 
|---|
| 38 | exit 3; | 
|---|
| 39 | } | 
|---|
| 40 |  | 
|---|
| 41 | my $res = `snmpwalk -On -Oq -r 2 -t 1 -c '$ARGV[1]' -v2c '$ARGV[0]' .1.3.6.1.2.1.25.2.3`; | 
|---|
| 42 | unless ($res =~ /^\.1.3.6/) { | 
|---|
| 43 | print "Unable to contact via SNMP\n"; | 
|---|
| 44 | exit 3; | 
|---|
| 45 | } | 
|---|
| 46 |  | 
|---|
| 47 | my $table = {}; | 
|---|
| 48 | my $index; | 
|---|
| 49 | foreach my $line (split(/\n/,$res)) { | 
|---|
| 50 | my ($k,$v) = split(/ /,$line,2); | 
|---|
| 51 | $table->{$k} = $v; | 
|---|
| 52 | if ($v eq $ARGV[2] && $k =~ /^.1.3.6.1.2.1.25.2.3.1.3\.(\d+)$/) { | 
|---|
| 53 | $index = $1; | 
|---|
| 54 | } | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | unless ($index) { | 
|---|
| 58 | print "Unable to find mountpoint $ARGV[2]\n"; | 
|---|
| 59 | exit 3; | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 62 | my $size = $table->{".1.3.6.1.2.1.25.2.3.1.5.$index"}; | 
|---|
| 63 | $size += 4294967296 if $size < 0; | 
|---|
| 64 | my $used = $table->{".1.3.6.1.2.1.25.2.3.1.6.$index"}; | 
|---|
| 65 | $used += 4294967296 if $used < 0; | 
|---|
| 66 | my $percent = $used * 100.0 / $size; | 
|---|
| 67 |  | 
|---|
| 68 | my $block = $table->{".1.3.6.1.2.1.25.2.3.1.4.$index"}; | 
|---|
| 69 | if ($block =~ /^(\d+)/) { | 
|---|
| 70 | $block = $1; | 
|---|
| 71 | $size = $size * 1.0 * $block / 1024 / 1024; | 
|---|
| 72 | $used = $used * 1.0 * $block / 1024 / 1024; | 
|---|
| 73 | } | 
|---|
| 74 | printf "%.2f%% used (%d out of %d MB)\n", $percent, $used, $size; | 
|---|
| 75 | if ($ARGV[4] && $percent >= $ARGV[4]) { | 
|---|
| 76 | exit 2;  # Critical | 
|---|
| 77 | } | 
|---|
| 78 | if ($ARGV[3] && $percent >= $ARGV[3]) { | 
|---|
| 79 | exit 1;  # Warning | 
|---|
| 80 | } | 
|---|
| 81 | exit 0; | 
|---|
| 82 |  | 
|---|
| 83 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 
|---|
| 84 |  | 
|---|
| 85 |  | 
|---|
| 86 | Make it executable using the following command: | 
|---|
| 87 |  | 
|---|
| 88 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 
|---|
| 89 | # chmod +x /usr/local/bin/check_hrstorage.pl | 
|---|
| 90 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 
|---|
| 91 |  | 
|---|
| 92 |  | 
|---|
| 93 | ## 2. Configure the plugin | 
|---|
| 94 |  | 
|---|
| 95 | Create a new configuration file: | 
|---|
| 96 |  | 
|---|
| 97 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 
|---|
| 98 | # editor /etc/nagios-plugins/config/hrstorage.cfg | 
|---|
| 99 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 
|---|
| 100 |  | 
|---|
| 101 |  | 
|---|
| 102 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 
|---|
| 103 | define command { | 
|---|
| 104 | command_name check_hrstorage | 
|---|
| 105 | command_line /usr/local/bin/check_hrstorage.pl '$HOSTADDRESS$' '$ARG1$' '$ARG2$' '$ARG3$' '$ARG4$' | 
|---|
| 106 | } | 
|---|
| 107 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 
|---|
| 108 |  | 
|---|
| 109 |  | 
|---|
| 110 | ## 3. Add a service check | 
|---|
| 111 |  | 
|---|
| 112 | A service check can now be added to an individual host, or to a hostgroup. | 
|---|
| 113 | For example: | 
|---|
| 114 |  | 
|---|
| 115 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 
|---|
| 116 | define service { | 
|---|
| 117 | host_name                       noc | 
|---|
| 118 | service_description             Disk space / | 
|---|
| 119 | check_command                   check_hrstorage!NetManage!/!80!90 | 
|---|
| 120 | use                             generic-service | 
|---|
| 121 | notification_interval           0 | 
|---|
| 122 | } | 
|---|
| 123 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 
|---|
| 124 |  | 
|---|
| 125 | The parameters to this check_hrstorage are: | 
|---|
| 126 |  | 
|---|
| 127 | * SNMP community | 
|---|
| 128 | * Filesystem mount point | 
|---|
| 129 | * Warning threshold (%) | 
|---|
| 130 | * Critical threshold (%) | 
|---|