Code

Exit code added (604837 - Lachlan Cameron-Smith)
[nagiosplug.git] / contrib / check_hprsc.pl
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/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             "warning=i"          => \$warning_opt,
49             "critical=i"         => \$critical_opt);
51 if ($chk_fs) {
52     walk_data($snmpwalk, $target_host, $target_community, $mounted_OID );
53     walk_data($snmpwalk, $target_host, $target_community, $totalspace_OID );
54     walk_data($snmpwalk, $target_host, $target_community, $freespace_OID );    check_filesystem($fs_opt, $warning_opt, $critical_opt);
55 } elsif ($show_fs) {
56     walk_data($snmpwalk, $target_host, $target_community, $filesystemID1_OID);
57     walk_data($snmpwalk, $target_host, $target_community, $mounted_OID );
58     walk_data($snmpwalk, $target_host, $target_community, $path_OID);
59     show_filesystem();
60 } elsif ($chk_fsid){
61     $totalspace_fsID_OID = "$totalspace_OID.$fsid1_opt";
62     $freespace_fsID_OID = "$freespace_OID.$fsid1_opt";
63     walk_data($snmpwalk, $target_host, $target_community, $totalspace_fsID_OID);
64     walk_data($snmpwalk, $target_host, $target_community, $freespace_fsID_OID);
65     check_filesystemID1($fsid1_opt, $warning_opt, $critical_opt);
66 } elsif ($chk_cpu) {
67     get_cpu_load($snmpwalk, $target_host, $target_community, $cpu_5min_OID);
68     check_cpu_5min($cpu, $warning_opt, $critical_opt);
69 } else {
70     print "\n\nUsage:\n";
71     print "Checking 5-min CPU Load:\n";
72     print "     $0 --check-cpu -warning <threshold> --critical <threshold> --host <yourhost> --community <SNMP community>\n\n";
73     print "Checking local filesystem mounted on a host:\n";
74     print "     $0 --show-filesystems --host <hostname> --community <SNMP community>\n\n";
75     print "Checking by filesystem name:\n";
76     print "     $0 --check-filesystem --filesystem </dev/vg00/lvol1> --warning <% used space> --critical <% used space> --host <hostname> --community <SNMP community>\n\n";
77     print "Checking by filesystem ID:\n";
78     print "     $0 --check-filesystemID --filesystemID <filesystemID1> --warning <% used space> --critical <% used space> --host <hostname> --community <SNMP community>\n\n";
79 }
81 sub get_cpu_load {
82     my ($snmpwalk, $target_host, $target_community, $OID) = @_;
83     die "cannot fork: $!" unless defined($pid = open(SNMPWALK, "-|"));
85     if ($pid) {   # parent
86         while (<SNMPWALK>) {
87             my @snmpdata = split(/:/,$_);
88             $cpu = $snmpdata[1]/100;
89         }
90         close(SNMPWALK) or warn "kid exited $?";
91     } else {      # child
92         exec($snmpwalk,$target_host,$target_community,$OID)  or die "can't exec program: $!";
93     }
94 }
96 sub walk_data {
97 #This function queries the SNMP daemon for the specific OID
98     my ($snmpwalk, $target_host, $target_community, $OID) = @_;
100     die "cannot fork: $!" unless defined($pid = open(SNMPWALK, "-|"));
102     if ($pid) {   # parent
103         while (<SNMPWALK>) {
104             $output = $_;
105             sort_walk_data($output);
106         }
107         close(SNMPWALK) or warn "kid exited $?";
108     } else {      # child
109         exec($snmpwalk,$target_host,$target_community,$OID)  or die "can't exec program: $!";
110     }
113 sub sort_walk_data {
114     my ($snmp_data) = @_;
115     @fields = split(/\./,$snmp_data);
116     $item = $fields[8];
117     $filesystemID1 = $fields[9];
118     @fields2 = split(/=/,$fields[10]);
119 #   $filesystemID2 = $fields2[0];
120     $value = $fields2[1];
121     chomp($value);
122     if ($value =~ /"/) {
123         @fields3 = split(/"/,$value);
124         $value = $fields3[1];
125     }
126     if ($item == 3) {
127         $mounted{$filesystemID1} = "$value";
128     } elsif ($item == 4) {
129         $totalspace{$filesystemID1} = "$value";
130     } elsif ($item == 6) {
131         $freespace{$filesystemID1} = "$value";
132     } elsif ($item == 10) {
133         $filesystempath{$filesystemID1} = "$value";
134     }
137 sub show_filesystem {
138     print "\n\nfilesystemID1\tmounted filesystem\tfilesystem path\n";
139     foreach $element (keys %mounted) {
140         print "$element\t$mounted{$element}\t\t$filesystempath{$element}\n";
141     }
142     print "\n\n";
145 sub check_filesystem {
147 # Warning  = percentage of used space >= $warning and < $critical
148 # Critical = percentage of used space > $warning and >= $critical
149 # OK       = percentage of used space < $warning and < $critical
151     my ($mounted_filesystem, $warning, $critical) = @_;
152     foreach $element (keys %mounted) {
153         if ($mounted{$element} eq $mounted_filesystem) {
154             my $warning_result = $totalspace{$element}*(100-$warning)/100;
155             my $critical_result = $totalspace{$element}*(100-$critical)/100;
156             my $result_percent = $freespace{$element}*100/$totalspace{$element};
157             if (($freespace{$element} <= $warning_result) && ($freespace{$element} > $critical_result)) {
158                 printf "Only %d M (%d%s) free\n",$freespace{$element}/1024,$result_percent,"%";
159                 exit 1;
160             } elsif ($freespace{$element} <= $critical_result) {
161                 printf "Only %d M (%d%s) free\n",$freespace{$element}/1024,$result_percent,"%";
162                 exit 2;
163             } else {
164                 printf "Disk ok - %d M (%d%s) free\n",$freespace{$element}/1024,$result_percent,"%";
165                 exit 0;
166             }
167         }
168     }
169     print "$mounted_filesystem doesn't exist in $target_host\n\n";
170     exit -1;
173 sub check_filesystemID1{
174 # Warning  = percentage of used space >= $warning and < $critical
175 # Critical = percentage of used space > $warning and >= $critical
176 # OK       = percentage of used space < $warning and < $critical
178     my ($fsid1, $warning, $critical) = @_;
179     foreach $element (keys %totalspace) {
180         if ($element eq  $fsid1) {
181             my $warning_result = $totalspace{$element}*(100-$warning)/100;
182             my $critical_result = $totalspace{$element}*(100-$critical)/100;
183             my $result_percent = $freespace{$element}*100/$totalspace{$element};
184             if (($freespace{$element} <= $warning_result) && ($freespace{$element} >= $critical_result)) {
185                 printf "Only %d M (%d%s) free\n",$freespace{$element}/1024,$result_percent,"%";
186                 exit 1;
187             } elsif ($freespace{$element} <= $critical_result) {
188                 printf "Only %d M (%d%s) free\n",$freespace{$element}/1024,$result_percent,"%";
189                 exit 2;
190             } else {
191                 printf "Disk ok - %d M (%d%s) free\n",$freespace{$element}/1024,$result_percent,"%";
192                 exit 0;
193             }
194         }
195     }
196     print "$fsid1 doesn't exist in $target_host\n\n";
197     exit -1;
200 sub check_cpu_5min {
201     my ($cpu, $warn, $crit) = @_;
202     if ($cpu >= $crit) {
203         print "Critical- 5-min load: $cpu\n";
204         exit 2;
205     } elsif ($cpu >= $warn) {
206         print "Warning - 5-min load: $cpu\n";
207         exit 1;
208     } else {
209         print "Load ok - 5-min load: $cpu\n";
210         exit 0;
211     }