1 #!/usr/bin/perl -wT
2 #
3 # Copyright (c) 2000 Hugo Gayosso
4 #
5 # Description:
6 # Nagios plug-in that monitors the resources on an HP-UX machine
7 # by querying the SNMP daemon
8 #
9 # License: General Public License (GPL)
10 # http://www.gnu.org/copyleft/gpl.txt
11 #
12 # ChangeLog
13 #
15 # Requirements: Perl 5.005 or higher
17 # Variable initialization
18 $ENV{'PATH'}="";
19 $ENV{'ENV'}="";
20 $ENV{'BASH_ENV'}="";
23 if (-e "/usr/bin/snmpwalk") {
24 $snmpwalk = "/usr/bin/snmpwalk";
25 } elsif (-e "/usr/local/bin/snmpwalk") {
26 $snmpwalk = "/usr/local/bin/snmpwalk";
27 }
30 # HP-UX SNMP OIDs
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";
38 use Getopt::Long;
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);
52 $proto_opt = 1
53 unless $proto_opt == 1 ||
54 $proto_opt == '2c' ||
55 $proto_opt == 3;
57 if ($chk_fs) {
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);
61 } elsif ($show_fs) {
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);
65 show_filesystem();
66 } elsif ($chk_fsid){
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);
72 } elsif ($chk_cpu) {
73 get_cpu_load($snmpwalk, $target_host, $target_community, $cpu_5min_OID,$proto_opt);
74 check_cpu_5min($cpu, $warning_opt, $critical_opt);
75 } else {
76 print "\n\nUsage:\n";
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";
85 }
87 sub get_cpu_load {
88 my ($snmpwalk, $target_host, $target_community, $OID,$vers) = @_;
89 die "cannot fork: $!" unless defined($pid = open(SNMPWALK, "-|"));
91 if ($pid) { # parent
92 while (<SNMPWALK>) {
93 my @snmpdata = split(/:/,$_);
94 $cpu = $snmpdata[1]/100;
95 }
96 close(SNMPWALK) or warn "kid exited $?";
97 } else { # child
98 exec($snmpwalk,'-c',$target_community,'-v',$vers,$target_host,$OID) or die "can't exec program: $!";
99 }
100 }
102 sub walk_data {
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, "-|"));
108 if ($pid) { # parent
109 while (<SNMPWALK>) {
110 $output = $_;
111 sort_walk_data($output);
112 }
113 close(SNMPWALK) or warn "kid exited $?";
114 } else { # child
115 exec($snmpwalk,'-c',$target_community,'-v',$vers,$target_host,$OID) or die "can't exec program: $!";
116 }
117 }
119 sub sort_walk_data {
120 my ($snmp_data) = @_;
121 @fields = split(/\./,$snmp_data);
122 $item = $fields[8];
123 $filesystemID1 = $fields[9];
124 @fields2 = split(/=/,$fields[10]);
125 # $filesystemID2 = $fields2[0];
126 $value = $fields2[1];
127 chomp($value);
128 if ($value =~ /"/) {
129 @fields3 = split(/"/,$value);
130 $value = $fields3[1];
131 }
132 if ($item == 3) {
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";
140 }
141 }
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";
147 }
148 print "\n\n";
149 }
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,"%";
165 exit 1;
166 } elsif ($freespace{$element} <= $critical_result) {
167 printf "Only %d M (%d%s) free\n",$freespace{$element}/1024,$result_percent,"%";
168 exit 2;
169 } else {
170 printf "Disk ok - %d M (%d%s) free\n",$freespace{$element}/1024,$result_percent,"%";
171 exit 0;
172 }
173 }
174 }
175 print "$mounted_filesystem doesn't exist in $target_host\n\n";
176 exit -1;
177 }
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,"%";
192 exit 1;
193 } elsif ($freespace{$element} <= $critical_result) {
194 printf "Only %d M (%d%s) free\n",$freespace{$element}/1024,$result_percent,"%";
195 exit 2;
196 } else {
197 printf "Disk ok - %d M (%d%s) free\n",$freespace{$element}/1024,$result_percent,"%";
198 exit 0;
199 }
200 }
201 }
202 print "$fsid1 doesn't exist in $target_host\n\n";
203 exit -1;
204 }
206 sub check_cpu_5min {
207 my ($cpu, $warn, $crit) = @_;
208 if ($cpu >= $crit) {
209 print "Critical- 5-min load: $cpu\n";
210 exit 2;
211 } elsif ($cpu >= $warn) {
212 print "Warning - 5-min load: $cpu\n";
213 exit 1;
214 } else {
215 print "Load ok - 5-min load: $cpu\n";
216 exit 0;
217 }
218 }