Agenda: exercises-plugin-disk-space.txt

File exercises-plugin-disk-space.txt, 3.7 KB (added by carmas, 5 years ago)
Line 
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
21It is possible to alert on disk space thresholds with the existing
22check_snmp plugin, but it is rather fiddly to set up (because, for example,
23you have to manually map the filesystem mountpoint to the OID index).  So
24the approach given here is to install a new plugin, which handles this for
25you.
26
27
28## 1. Create a plugin script
29
30Create 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
36unless ($ARGV[2]) {
37  print STDERR "Usage: $0 <host> <community> <mountpoint> [<warn%> [<crit%>]]\n";
38  exit 3;
39}
40
41my $res = `snmpwalk -On -Oq -r 2 -t 1 -c '$ARGV[1]' -v2c '$ARGV[0]' .1.3.6.1.2.1.25.2.3`;
42unless ($res =~ /^\.1.3.6/) {
43  print "Unable to contact via SNMP\n";
44  exit 3;
45}
46
47my $table = {};
48my $index;
49foreach 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
57unless ($index) {
58  print "Unable to find mountpoint $ARGV[2]\n";
59  exit 3;
60}
61
62my $size = $table->{".1.3.6.1.2.1.25.2.3.1.5.$index"};
63$size += 4294967296 if $size < 0;
64my $used = $table->{".1.3.6.1.2.1.25.2.3.1.6.$index"};
65$used += 4294967296 if $used < 0;
66my $percent = $used * 100.0 / $size;
67
68my $block = $table->{".1.3.6.1.2.1.25.2.3.1.4.$index"};
69if ($block =~ /^(\d+)/) {
70  $block = $1;
71  $size = $size * 1.0 * $block / 1024 / 1024;
72  $used = $used * 1.0 * $block / 1024 / 1024;
73}
74printf "%.2f%% used (%d out of %d MB)\n", $percent, $used, $size;
75if ($ARGV[4] && $percent >= $ARGV[4]) {
76  exit 2;  # Critical
77}
78if ($ARGV[3] && $percent >= $ARGV[3]) {
79  exit 1;  # Warning
80}
81exit 0;
82
83~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
84
85
86Make 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
95Create a new configuration file:
96
97~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
98# editor /etc/nagios-plugins/config/hrstorage.cfg
99~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
100
101
102~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
103define 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
112A service check can now be added to an individual host, or to a hostgroup.
113For example:
114
115~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
116define 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
125The parameters to this check_hrstorage are:
126
127* SNMP community
128* Filesystem mount point
129* Warning threshold (%)
130* Critical threshold (%)