1 #!/usr/local/bin/perl -w
2 #
3 # check_ifoperstatus.pl - nagios plugin
4 #
5 # Copyright (C) 2000 Christoph Kron,
6 # Modified 5/2002 to conform to updated Nagios Plugin Guidelines
7 # Added support for named interfaces per Valdimir Ivaschenko (S. Ghosh)
8 #
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License
11 # as published by the Free Software Foundation; either version 2
12 # of the License, or (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #
23 #
24 # Report bugs to: nagiosplug-help@lists.sourceforge.net
25 #
26 # 11.01.2000 Version 1.0
27 # $Id$
28 #
29 # Patches from Guy Van Den Bergh to warn on ifadminstatus down interfaces
30 # instead of critical.
31 #
32 # Primary MIB reference - RFC 2863
35 use POSIX;
36 use strict;
37 use lib utils.pm ;
38 use utils qw($TIMEOUT %ERRORS &print_revision &support);
40 use Net::SNMP;
41 use Getopt::Long;
42 &Getopt::Long::config('bundling');
44 my $PROGNAME = "check_ifoperstatus";
45 my $status;
46 my %ifOperStatus = ('1','up',
47 '2','down',
48 '3','testing',
49 '4','unknown',
50 '5','dormant',
51 '6','notPresent',
52 '7','lowerLayerDown'); # down due to the state of lower layer interface(s)
54 my $state = "UNKNOWN";
55 my $answer = "";
56 my $snmpkey = 0;
57 my $community = "public";
58 my $port = 161;
59 my @snmpoids;
60 my $sysUptime = '1.3.6.1.2.1.1.3.0';
61 my $snmpIfDescr = '1.3.6.1.2.1.2.2.1.2';
62 my $snmpIfAdminStatus = '1.3.6.1.2.1.2.2.1.7';
63 my $snmpIfOperStatus = '1.3.6.1.2.1.2.2.1.8';
64 my $snmpIfName = '1.3.6.1.2.1.31.1.1.1.1';
65 my $snmpIfLastChange = '1.3.6.1.2.1.2.2.1.9';
66 my $snmpIfAlias = '1.3.6.1.2.1.31.1.1.1.18';
67 my $snmpLocIfDescr = '1.3.6.1.4.1.9.2.2.1.1.28';
68 my $hostname;
69 my $ifName;
70 my $session;
71 my $error;
72 my $response;
73 my $snmp_version = 1 ;
74 my $ifXTable;
75 my $opt_h ;
76 my $opt_V ;
77 my $ifdescr;
78 my $key;
79 my $lastc;
80 my $dormantWarn;
81 my $name;
85 # Just in case of problems, let's not hang Nagios
86 $SIG{'ALRM'} = sub {
87 print ("ERROR: No snmp response from $hostname (alarm)\n");
88 exit $ERRORS{"UNKNOWN"};
89 };
90 alarm($TIMEOUT);
93 ### Validate Arguments
95 $status = GetOptions(
96 "V" => \$opt_V, "version" => \$opt_V,
97 "h" => \$opt_h, "help" => \$opt_h,
98 "v=i" => \$snmp_version, "snmp_version=i" => \$snmp_version,
99 "C=s" =>\$community, "community=s" => \$community,
100 "k=i" =>\$snmpkey, "key=i",\$snmpkey,
101 "d=s" =>\$ifdescr, "descr=s" => \$ifdescr,
102 "l=s" => \$lastc, "lastchange=s" => \$lastc,
103 "p=i" =>\$port, "port=i",\$port,
104 "H=s" => \$hostname, "hostname=s" => \$hostname,
105 "I" => \$ifXTable, "ifmib" => \$ifXTable,
106 "n=s" => \$ifName, "name=s" => \$ifName,
107 "w=s" => \$dormantWarn, "warn=s" => \$dormantWarn );
111 if ($status == 0)
112 {
113 print_help();
114 exit $ERRORS{'OK'};
115 }
117 if ($opt_V) {
118 print_revision($PROGNAME,'$Revision$ ');
119 exit $ERRORS{'OK'};
120 }
122 if ($opt_h) {
123 print_help();
124 exit $ERRORS{'OK'};
125 }
127 if (! utils::is_hostname($hostname)){
128 usage();
129 exit $ERRORS{"UNKNOWN"};
130 }
133 unless ($snmpkey > 0 || defined $ifdescr){
134 printf "Either a valid snmpkey key (-k) or a ifDescr (-d) must be provided)\n";
135 usage();
136 exit $ERRORS{"UNKNOWN"};
137 }
140 if (defined $name) {
141 $ifXTable=1;
142 }
144 if ( $snmp_version =~ /[12]/ ) {
145 ($session, $error) = Net::SNMP->session(
146 -hostname => $hostname,
147 -community => $community,
148 -port => $port,
149 -version => $snmp_version
150 );
152 if (!defined($session)) {
153 $state='UNKNOWN';
154 $answer=$error;
155 print ("$state: $answer");
156 exit $ERRORS{$state};
157 }
158 }elsif ( $snmp_version =~ /3/ ) {
159 $state='UNKNOWN';
160 print ("$state: No support for SNMP v3 yet\n");
161 exit $ERRORS{$state};
162 }else{
163 $state='UNKNOWN';
164 print ("$state: No support for SNMP v$snmp_version yet\n");
165 exit $ERRORS{$state};
166 }
168 ## End validation
172 ## map ifdescr to ifindex - should look at being able to cache this value
174 if (defined $ifdescr) {
175 # escape "/" in ifdescr - very common in the Cisco world
176 $ifdescr =~ s/\//\\\//g;
178 $status=fetch_ifdescr(); # if using on device with large number of interfaces
179 # recommend use of SNMP v2 (get-bulk)
180 if ($status==0) {
181 $state = "UNKNOWN";
182 printf "$state: could not retrive snmpkey - $status-$snmpkey\n";
183 $session->close;
184 exit $ERRORS{$state};
185 }
186 }
189 ## Main function
191 $snmpIfAdminStatus = $snmpIfAdminStatus . "." . $snmpkey;
192 $snmpIfOperStatus = $snmpIfOperStatus . "." . $snmpkey;
193 $snmpIfDescr = $snmpIfDescr . "." . $snmpkey;
194 $snmpIfName = $snmpIfName . "." . $snmpkey ;
195 $snmpIfAlias = $snmpIfAlias . "." . $snmpkey ;
197 push(@snmpoids,$snmpIfAdminStatus);
198 push(@snmpoids,$snmpIfOperStatus);
199 push(@snmpoids,$snmpIfDescr);
200 push(@snmpoids,$snmpIfName) if (defined $ifXTable) ;
201 push(@snmpoids,$snmpIfAlias) if (defined $ifXTable) ;
203 if (!defined($response = $session->get_request(@snmpoids))) {
204 $answer=$session->error;
205 $session->close;
206 $state = 'WARNING';
207 print ("$state: SNMP error: $answer\n");
208 exit $ERRORS{$state};
209 }
211 $answer = sprintf("host '%s', %s(%s) is %s\n",
212 $hostname,
213 $response->{$snmpIfDescr},
214 $snmpkey,
215 $ifOperStatus{$response->{$snmpIfOperStatus}}
216 );
219 ## Check to see if ifName match is requested and it matches - exit if no match
220 ## not the interface we want to monitor
221 if ( defined $name && not ($response->{$snmpIfName} eq $name) ) {
222 $state = 'UNKNOWN';
223 $answer = "Interface name ($name) doesn't match snmp value ($response->{$snmpIfName}) (index $snmpkey)";
224 print ("$state: $answer");
225 exit $ERRORS{$state};
226 }
228 ## define the interface name
229 if (defined $ifXTable) {
230 $name = $response->{$snmpIfName} ." - " .$response->{$snmpIfAlias} ;
231 }else{
232 $name = $response->{$snmpIfDescr} ;
233 }
235 ## if AdminStatus is down - some one made a consious effort to change config
236 ##
237 if ( not ($response->{$snmpIfAdminStatus} == 1) ) {
238 $state = 'WARNING';
239 $answer = "Interface $name (index $snmpkey) is administratively down.";
241 }
242 ## Check operational status
243 elsif ( $response->{$snmpIfOperStatus} == 2 ) {
244 $state = 'CRITICAL';
245 $answer = "Interface $name (index $snmpkey) is down.";
246 } elsif ( $response->{$snmpIfOperStatus} == 5 ) {
247 if (defined $dormantWarn ) {
248 if ($dormantWarn eq "w") {
249 $state = 'WARNNG';
250 $answer = "Interface $name (index $snmpkey) is dormant.";
251 }elsif($dormantWarn eq "c") {
252 $state = 'CRITICAL';
253 $answer = "Interface $name (index $snmpkey) is dormant.";
254 }elsif($dormantWarn eq "i") {
255 $state = 'OK';
256 $answer = "Interface $name (index $snmpkey) is dormant.";
257 }
258 }else{
259 # dormant interface - but warning/critical/ignore not requested
260 $state = 'CRITICAL';
261 $answer = "Interface $name (index $snmpkey) is dormant.";
262 }
263 } elsif ( $response->{$snmpIfOperStatus} == 6 ) {
264 $state = 'CRITICAL';
265 $answer = "Interface $name (index $snmpkey) notPresent - possible hotswap in progress.";
266 } elsif ( $response->{$snmpIfOperStatus} == 7 ) {
267 $state = 'CRITICAL';
268 $answer = "Interface $name (index $snmpkey) down due to lower layer being down.";
270 } elsif ( $response->{$snmpIfOperStatus} == 3 || $response->{$snmpIfOperStatus} == 4 ) {
271 $state = 'CRITICAL';
272 $answer = "Interface $name (index $snmpkey) down (testing/unknown).";
274 } else {
275 $state = 'OK';
276 $answer = "Interface $name (index $snmpkey) is up.";
277 }
281 print ("$state: $answer");
282 exit $ERRORS{$state};
285 ### subroutines
287 sub fetch_ifdescr {
288 if (!defined ($response = $session->get_table($snmpIfDescr))) {
289 $answer=$session->error;
290 $session->close;
291 $state = 'CRITICAL';
292 printf ("$state: SNMP error with snmp version $snmp_version ($answer)\n");
293 $session->close;
294 exit $ERRORS{$state};
295 }
297 foreach $key ( keys %{$response}) {
298 if ($response->{$key} =~ /^$ifdescr$/) {
299 $key =~ /.*\.(\d+)$/;
300 $snmpkey = $1;
301 #print "$ifdescr = $key / $snmpkey \n"; #debug
302 }
303 }
304 unless (defined $snmpkey) {
305 $session->close;
306 $state = 'CRITICAL';
307 printf "$state: Could not match $ifdescr on $hostname\n";
308 exit $ERRORS{$state};
309 }
311 return $snmpkey;
312 }
314 sub usage {
315 printf "\nMissing arguments!\n";
316 printf "\n";
317 printf "usage: \n";
318 printf "check_ifoperstatus -k <IF_KEY> -H <HOSTNAME> [-C <community>]\n";
319 printf "Copyright (C) 2000 Christoph Kron\n";
320 printf "check_ifoperstatus.pl comes with ABSOLUTELY NO WARRANTY\n";
321 printf "This programm is licensed under the terms of the ";
322 printf "GNU General Public License\n(check source code for details)\n";
323 printf "\n\n";
324 exit $ERRORS{"UNKNOWN"};
325 }
327 sub print_help {
328 printf "check_ifoperstatus plugin for Nagios monitors operational \n";
329 printf "status of a particular network interface on the target host\n";
330 printf "\nUsage:\n";
331 printf " -H (--hostname) Hostname to query - (required)\n";
332 printf " -C (--community) SNMP read community (defaults to public,\n";
333 printf " used with SNMP v1 and v2c\n";
334 printf " -v (--snmp_version) 1 for SNMP v1 (default)\n";
335 printf " 2 for SNMP v2c\n";
336 printf " SNMP v2c will use get_bulk for less overhead\n";
337 printf " if monitoring with -d\n";
338 printf " -k (--key) SNMP IfIndex value\n";
339 printf " -d (--descr) SNMP ifDescr value\n";
340 printf " -p (--port) SNMP port (default 161)\n";
341 printf " -I (--ifmib) Agent supports IFMIB ifXTable. Do not use if\n";
342 printf " you don't know what this is. \n";
343 printf " -n (--name) the value should match the returned ifName\n";
344 printf " (Implies the use of -I)\n";
345 printf " -w (--warn =i|w|c) ignore|warn|crit if the interface is dormant (default critical)\n";
346 printf " -V (--version) Plugin version\n";
347 printf " -h (--help) usage help \n\n";
348 printf " -k or -d must be specified\n\n";
349 printf "Note: either -k or -d must be specified and -d is much more network \n";
350 printf "intensive. Use it sparingly or not at all. -n is used to match against\n";
351 printf "a much more descriptive ifName value in the IfXTable to verify that the\n";
352 printf "snmpkey has not changed to some other network interface after a reboot.\n\n";
353 print_revision($PROGNAME, '$Revision$');
355 }