Agenda: exercises-plugin-disk-space.txt

File exercises-plugin-disk-space.txt, 2.7 KB (added by b.candler, 6 years ago)
Line 
1It is possible to alert on disk space thresholds with the existing
2check_snmp plugin, but it is rather fiddly to set up (because, for example,
3you have to manually map the filesystem mountpoint to the OID index).  So
4the approach given here is to install a new plugin, which handles this for
5you.
6
7
81. Create a plugin script
9
10Create a file /usr/local/bin/check_hrstorage.pl with the following contents
11(the #! line must be the very first line in the file)
12
13# ----------8<-------start-copying-below-this-line----------8<--------
14#!/usr/bin/perl -w
15
16unless ($ARGV[2]) {
17  print STDERR "Usage: $0 <host> <community> <mountpoint> [<warn%> [<crit%>]]\n";
18  exit 3;
19}
20
21my $res = `snmpwalk -On -Oq -r 2 -t 1 -c '$ARGV[1]' -v2c '$ARGV[0]' .1.3.6.1.2.1.25.2.3`;
22unless ($res =~ /^\.1.3.6/) {
23  print "Unable to contact via SNMP\n";
24  exit 3;
25}
26
27my $table = {};
28my $index;
29foreach my $line (split(/\n/,$res)) {
30  my ($k,$v) = split(/ /,$line,2);
31  $table->{$k} = $v;
32  if ($v eq $ARGV[2] && $k =~ /^.1.3.6.1.2.1.25.2.3.1.3\.(\d+)$/) {
33    $index = $1;
34  }
35}
36
37unless ($index) {
38  print "Unable to find mountpoint $ARGV[2]\n";
39  exit 3;
40}
41
42my $size = $table->{".1.3.6.1.2.1.25.2.3.1.5.$index"};
43$size += 4294967296 if $size < 0;
44my $used = $table->{".1.3.6.1.2.1.25.2.3.1.6.$index"};
45$used += 4294967296 if $used < 0;
46my $percent = $used * 100.0 / $size;
47
48my $block = $table->{".1.3.6.1.2.1.25.2.3.1.4.$index"};
49if ($block =~ /^(\d+)/) {
50  $block = $1;
51  $size = $size * 1.0 * $block / 1024 / 1024;
52  $used = $used * 1.0 * $block / 1024 / 1024;
53}
54printf "%.2f%% used (%d out of %d MB)\n", $percent, $used, $size;
55if ($ARGV[4] && $percent >= $ARGV[4]) {
56  exit 2;  # Critical
57}
58if ($ARGV[3] && $percent >= $ARGV[3]) {
59  exit 1;  # Warning
60}
61exit 0;
62
63# ----------8<-------stop-copying-below-this-line----------8<--------
64
65Make it executable using the following command:
66
67    # chmod +x /usr/local/bin/check_hrstorage.pl
68
69
702. Configure the plugin
71
72Create a new configuration file:
73
74    # editor /etc/nagios-plugins/config/hrstorage.cfg
75
76define command {
77    command_name check_hrstorage
78    command_line /usr/local/bin/check_hrstorage.pl '$HOSTADDRESS$' '$ARG1$' '$ARG2$' '$ARG3$' '$ARG4$'
79}
80
81
823. Add a service check
83
84A service check can now be added to an individual host, or to a hostgroup.
85For example:
86
87define service {
88        host_name                       noc
89        service_description             Disk space /
90        check_command                   check_hrstorage!NetManage!/!80!90
91        use                             generic-service
92        notification_interval           0
93}
94
95The parameters to this check_hrstorage are:
96
97* SNMP community
98* Filesystem mount point
99* Warning threshold (%)
100* Critical threshold (%)