3 # Copyright (c) 2000 Hugo Gayosso
6 # Nagios plug-in that monitors the resources on an HP-UX machine
7 # by querying the SNMP daemon
9 # License: General Public License (GPL)
10 # http://www.gnu.org/copyleft/gpl.txt
15 # Requirements: Perl 5.005 or higher
17 # Variable initialization
23 if (-e
"/usr/bin/snmpwalk") {
24 $snmpwalk = "/usr/bin/snmpwalk";
25 } elsif (-e
"/usr/local/bin/snmpwalk") {
26 $snmpwalk = "/usr/local/bin/snmpwalk";
31 $filesystemID1_OID = ".1.3.6.1.4.1.11.2.3.1.2.2.1.1";
32 $mounted_OID = ".1.3.6.1.4.1.11.2.3.1.2.2.1.3";
33 $totalspace_OID = ".1.3.6.1.4.1.11.2.3.1.2.2.1.4";
34 $freespace_OID = ".1.3.6.1.4.1.11.2.3.1.2.2.1.6";
35 $path_OID = ".1.3.6.1.4.1.11.2.3.1.2.2.1.10";
36 $cpu_5min_OID = ".1.3.6.1.4.1.11.2.3.1.1.4";
40 GetOptions
( "check-filesystem" => \
$chk_fs,
41 "show-filesystems" => \
$show_fs,
42 "check-filesystemID" => \
$chk_fsid,
43 "check-cpu" => \
$chk_cpu,
44 "host=s" => \
$target_host,
45 "community=s" => \
$target_community,
46 "filesystemID1=i" => \
$fsid1_opt,
47 "filesystem=s" => \
$fs_opt,
48 "protocol:s" => \
$proto_opt,
49 "warning=i" => \
$warning_opt,
50 "critical=i" => \
$critical_opt);
53 unless $proto_opt == 1 ||
58 walk_data
($snmpwalk, $target_host, $target_community, $mounted_OID,$proto_opt );
59 walk_data
($snmpwalk, $target_host, $target_community, $totalspace_OID,$proto_opt );
60 walk_data
($snmpwalk, $target_host, $target_community, $freespace_OID,$proto_opt ); check_filesystem
($fs_opt, $warning_opt, $critical_opt);
62 walk_data
($snmpwalk, $target_host, $target_community, $filesystemID1_OID,$proto_opt);
63 walk_data
($snmpwalk, $target_host, $target_community, $mounted_OID,$proto_opt );
64 walk_data
($snmpwalk, $target_host, $target_community, $path_OID,$proto_opt);
67 $totalspace_fsID_OID = "$totalspace_OID.$fsid1_opt";
68 $freespace_fsID_OID = "$freespace_OID.$fsid1_opt";
69 walk_data
($snmpwalk, $target_host, $target_community, $totalspace_fsID_OID,$proto_opt);
70 walk_data
($snmpwalk, $target_host, $target_community, $freespace_fsID_OID,$proto_opt);
71 check_filesystemID1
($fsid1_opt, $warning_opt, $critical_opt);
73 get_cpu_load
($snmpwalk, $target_host, $target_community, $cpu_5min_OID,$proto_opt);
74 check_cpu_5min
($cpu, $warning_opt, $critical_opt);
77 print "Checking 5-min CPU Load:\n";
78 print " $0 --check-cpu -warning <threshold> --critical <threshold> --host <yourhost> --community <SNMP community> --protocol <SNMP version [1|2c|3]>\n\n";
79 print "Checking local filesystem mounted on a host:\n";
80 print " $0 --show-filesystems --host <hostname> --community <SNMP community> --protocol <SNMP version [1|2c|3]>\n\n";
81 print "Checking by filesystem name:\n";
82 print " $0 --check-filesystem --filesystem </dev/vg00/lvol1> --warning <% used space> --critical <% used space> --host <hostname> --community <SNMP community> --protocol <SNMP version [1|2c|3]>\n\n";
83 print "Checking by filesystem ID:\n";
84 print " $0 --check-filesystemID --filesystemID <filesystemID1> --warning <% used space> --critical <% used space> --host <hostname> --community <SNMP community> --protocol <SNMP version [1|2c|3]>\n\n";
88 my ($snmpwalk, $target_host, $target_community, $OID,$vers) = @_;
89 die "cannot fork: $!" unless defined($pid = open(SNMPWALK
, "-|"));
93 my @snmpdata = split(/:/,$_);
94 $cpu = $snmpdata[1]/100;
96 close(SNMPWALK
) or warn "kid exited $?";
98 exec($snmpwalk,'-c',$target_community,'-v',$vers,$target_host,$OID) or die "can't exec program: $!";
103 #This function queries the SNMP daemon for the specific OID
104 my ($snmpwalk, $target_host, $target_community, $OID,$vers) = @_;
106 die "cannot fork: $!" unless defined($pid = open(SNMPWALK
, "-|"));
111 sort_walk_data
($output);
113 close(SNMPWALK
) or warn "kid exited $?";
115 exec($snmpwalk,'-c',$target_community,'-v',$vers,$target_host,$OID) or die "can't exec program: $!";
120 my ($snmp_data) = @_;
121 @fields = split(/\./,$snmp_data);
123 $filesystemID1 = $fields[9];
124 @fields2 = split(/=/,$fields[10]);
125 # $filesystemID2 = $fields2[0];
126 $value = $fields2[1];
129 @fields3 = split(/"/,$value);
130 $value = $fields3[1];
133 $mounted{$filesystemID1} = "$value";
134 } elsif ($item == 4) {
135 $totalspace{$filesystemID1} = "$value";
136 } elsif ($item == 6) {
137 $freespace{$filesystemID1} = "$value";
138 } elsif ($item == 10) {
139 $filesystempath{$filesystemID1} = "$value";
143 sub show_filesystem
{
144 print "\n\nfilesystemID1\tmounted filesystem\tfilesystem path\n";
145 foreach $element (keys %mounted) {
146 print "$element\t$mounted{$element}\t\t$filesystempath{$element}\n";
151 sub check_filesystem
{
153 # Warning = percentage of used space >= $warning and < $critical
154 # Critical = percentage of used space > $warning and >= $critical
155 # OK = percentage of used space < $warning and < $critical
157 my ($mounted_filesystem, $warning, $critical) = @_;
158 foreach $element (keys %mounted) {
159 if ($mounted{$element} eq $mounted_filesystem) {
160 my $warning_result = $totalspace{$element}*(100-$warning)/100;
161 my $critical_result = $totalspace{$element}*(100-$critical)/100;
162 my $result_percent = $freespace{$element}*100/$totalspace{$element};
163 if (($freespace{$element} <= $warning_result) && ($freespace{$element} > $critical_result)) {
164 printf "Only %d M (%d%s) free\n",$freespace{$element}/1024,$result_percent,"%";
166 } elsif ($freespace{$element} <= $critical_result) {
167 printf "Only %d M (%d%s) free\n",$freespace{$element}/1024,$result_percent,"%";
170 printf "Disk ok - %d M (%d%s) free\n",$freespace{$element}/1024,$result_percent,"%";
175 print "$mounted_filesystem doesn't exist in $target_host\n\n";
179 sub check_filesystemID1
{
180 # Warning = percentage of used space >= $warning and < $critical
181 # Critical = percentage of used space > $warning and >= $critical
182 # OK = percentage of used space < $warning and < $critical
184 my ($fsid1, $warning, $critical) = @_;
185 foreach $element (keys %totalspace) {
186 if ($element eq $fsid1) {
187 my $warning_result = $totalspace{$element}*(100-$warning)/100;
188 my $critical_result = $totalspace{$element}*(100-$critical)/100;
189 my $result_percent = $freespace{$element}*100/$totalspace{$element};
190 if (($freespace{$element} <= $warning_result) && ($freespace{$element} >= $critical_result)) {
191 printf "Only %d M (%d%s) free\n",$freespace{$element}/1024,$result_percent,"%";
193 } elsif ($freespace{$element} <= $critical_result) {
194 printf "Only %d M (%d%s) free\n",$freespace{$element}/1024,$result_percent,"%";
197 printf "Disk ok - %d M (%d%s) free\n",$freespace{$element}/1024,$result_percent,"%";
202 print "$fsid1 doesn't exist in $target_host\n\n";
207 my ($cpu, $warn, $crit) = @_;
209 print "Critical- 5-min load: $cpu\n";
211 } elsif ($cpu >= $warn) {
212 print "Warning - 5-min load: $cpu\n";
215 print "Load ok - 5-min load: $cpu\n";