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 # Added SNMPv3 support (10/2003)
9 #
10 # This program is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License
12 # as published by the Free Software Foundation; either version 2
13 # of the License, or (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #
24 #
25 # Report bugs to: nagiosplug-help@lists.sourceforge.net
26 #
27 # 11.01.2000 Version 1.0
28 # $Id$
29 #
30 # Patches from Guy Van Den Bergh to warn on ifadminstatus down interfaces
31 # instead of critical.
32 #
33 # Primary MIB reference - RFC 2863
36 use POSIX;
37 use strict;
38 use lib utils.pm ;
39 use utils qw($TIMEOUT %ERRORS &print_revision &support);
41 use Net::SNMP;
42 use Getopt::Long;
43 &Getopt::Long::config('bundling');
45 my $PROGNAME = "check_ifoperstatus";
46 sub print_help ();
47 sub usage ();
48 sub process_arguments ();
50 my $timeout;
51 my $status;
52 my %ifOperStatus = ('1','up',
53 '2','down',
54 '3','testing',
55 '4','unknown',
56 '5','dormant',
57 '6','notPresent',
58 '7','lowerLayerDown'); # down due to the state of lower layer interface(s)
60 my $state = "UNKNOWN";
61 my $answer = "";
62 my $snmpkey = 0;
63 my $community = "public";
64 my $maxmsgsize = 1472 ; # Net::SNMP default is 1472
65 my ($seclevel, $authproto, $secname, $authpass, $privpass, $auth, $priv, $context);
66 my $port = 161;
67 my @snmpoids;
68 my $sysUptime = '1.3.6.1.2.1.1.3.0';
69 my $snmpIfDescr = '1.3.6.1.2.1.2.2.1.2';
70 my $snmpIfAdminStatus = '1.3.6.1.2.1.2.2.1.7';
71 my $snmpIfOperStatus = '1.3.6.1.2.1.2.2.1.8';
72 my $snmpIfName = '1.3.6.1.2.1.31.1.1.1.1';
73 my $snmpIfLastChange = '1.3.6.1.2.1.2.2.1.9';
74 my $snmpIfAlias = '1.3.6.1.2.1.31.1.1.1.18';
75 my $snmpLocIfDescr = '1.3.6.1.4.1.9.2.2.1.1.28';
76 my $hostname;
77 my $ifName;
78 my $session;
79 my $error;
80 my $response;
81 my $snmp_version = 1 ;
82 my $ifXTable;
83 my $opt_h ;
84 my $opt_V ;
85 my $ifdescr;
86 my $key;
87 my $lastc;
88 my $dormantWarn;
89 my $name;
91 ### Validate Arguments
93 $status = process_arguments();
96 # Just in case of problems, let's not hang Nagios
97 $SIG{'ALRM'} = sub {
98 print ("ERROR: No snmp response from $hostname (alarm)\n");
99 exit $ERRORS{"UNKNOWN"};
100 };
102 alarm($timeout);
105 ## map ifdescr to ifindex - should look at being able to cache this value
107 if (defined $ifdescr) {
108 # escape "/" in ifdescr - very common in the Cisco world
109 $ifdescr =~ s/\//\\\//g;
111 $status=fetch_ifdescr(); # if using on device with large number of interfaces
112 # recommend use of SNMP v2 (get-bulk)
113 if ($status==0) {
114 $state = "UNKNOWN";
115 printf "$state: could not retrive ifdescr snmpkey - $status-$snmpkey\n";
116 $session->close;
117 exit $ERRORS{$state};
118 }
119 }
122 ## Main function
124 $snmpIfAdminStatus = $snmpIfAdminStatus . "." . $snmpkey;
125 $snmpIfOperStatus = $snmpIfOperStatus . "." . $snmpkey;
126 $snmpIfDescr = $snmpIfDescr . "." . $snmpkey;
127 $snmpIfName = $snmpIfName . "." . $snmpkey ;
128 $snmpIfAlias = $snmpIfAlias . "." . $snmpkey ;
130 push(@snmpoids,$snmpIfAdminStatus);
131 push(@snmpoids,$snmpIfOperStatus);
132 push(@snmpoids,$snmpIfDescr);
133 push(@snmpoids,$snmpIfName) if (defined $ifXTable) ;
134 push(@snmpoids,$snmpIfAlias) if (defined $ifXTable) ;
136 if (!defined($response = $session->get_request(@snmpoids))) {
137 $answer=$session->error;
138 $session->close;
139 $state = 'WARNING';
140 print ("$state: SNMP error: $answer\n");
141 exit $ERRORS{$state};
142 }
144 $answer = sprintf("host '%s', %s(%s) is %s\n",
145 $hostname,
146 $response->{$snmpIfDescr},
147 $snmpkey,
148 $ifOperStatus{$response->{$snmpIfOperStatus}}
149 );
152 ## Check to see if ifName match is requested and it matches - exit if no match
153 ## not the interface we want to monitor
154 if ( defined $name && not ($response->{$snmpIfName} eq $name) ) {
155 $state = 'UNKNOWN';
156 $answer = "Interface name ($name) doesn't match snmp value ($response->{$snmpIfName}) (index $snmpkey)";
157 print ("$state: $answer");
158 exit $ERRORS{$state};
159 }
161 ## define the interface name
162 if (defined $ifXTable) {
163 $name = $response->{$snmpIfName} ." - " .$response->{$snmpIfAlias} ;
164 }else{
165 $name = $response->{$snmpIfDescr} ;
166 }
168 ## if AdminStatus is down - some one made a consious effort to change config
169 ##
170 if ( not ($response->{$snmpIfAdminStatus} == 1) ) {
171 $state = 'WARNING';
172 $answer = "Interface $name (index $snmpkey) is administratively down.";
174 }
175 ## Check operational status
176 elsif ( $response->{$snmpIfOperStatus} == 2 ) {
177 $state = 'CRITICAL';
178 $answer = "Interface $name (index $snmpkey) is down.";
179 } elsif ( $response->{$snmpIfOperStatus} == 5 ) {
180 if (defined $dormantWarn ) {
181 if ($dormantWarn eq "w") {
182 $state = 'WARNNG';
183 $answer = "Interface $name (index $snmpkey) is dormant.";
184 }elsif($dormantWarn eq "c") {
185 $state = 'CRITICAL';
186 $answer = "Interface $name (index $snmpkey) is dormant.";
187 }elsif($dormantWarn eq "i") {
188 $state = 'OK';
189 $answer = "Interface $name (index $snmpkey) is dormant.";
190 }
191 }else{
192 # dormant interface - but warning/critical/ignore not requested
193 $state = 'CRITICAL';
194 $answer = "Interface $name (index $snmpkey) is dormant.";
195 }
196 } elsif ( $response->{$snmpIfOperStatus} == 6 ) {
197 $state = 'CRITICAL';
198 $answer = "Interface $name (index $snmpkey) notPresent - possible hotswap in progress.";
199 } elsif ( $response->{$snmpIfOperStatus} == 7 ) {
200 $state = 'CRITICAL';
201 $answer = "Interface $name (index $snmpkey) down due to lower layer being down.";
203 } elsif ( $response->{$snmpIfOperStatus} == 3 || $response->{$snmpIfOperStatus} == 4 ) {
204 $state = 'CRITICAL';
205 $answer = "Interface $name (index $snmpkey) down (testing/unknown).";
207 } else {
208 $state = 'OK';
209 $answer = "Interface $name (index $snmpkey) is up.";
210 }
214 print ("$state: $answer");
215 exit $ERRORS{$state};
218 ### subroutines
220 sub fetch_ifdescr {
221 if (!defined ($response = $session->get_table($snmpIfDescr))) {
222 $answer=$session->error;
223 $session->close;
224 $state = 'CRITICAL';
225 printf ("$state: SNMP error with snmp version $snmp_version ($answer)\n");
226 $session->close;
227 exit $ERRORS{$state};
228 }
230 foreach $key ( keys %{$response}) {
231 if ($response->{$key} =~ /^$ifdescr$/) {
232 $key =~ /.*\.(\d+)$/;
233 $snmpkey = $1;
234 #print "$ifdescr = $key / $snmpkey \n"; #debug
235 }
236 }
237 unless (defined $snmpkey) {
238 $session->close;
239 $state = 'CRITICAL';
240 printf "$state: Could not match $ifdescr on $hostname\n";
241 exit $ERRORS{$state};
242 }
244 return $snmpkey;
245 }
247 sub usage() {
248 printf "\nMissing arguments!\n";
249 printf "\n";
250 printf "usage: \n";
251 printf "check_ifoperstatus -k <IF_KEY> -H <HOSTNAME> [-C <community>]\n";
252 printf "Copyright (C) 2000 Christoph Kron\n";
253 printf "check_ifoperstatus.pl comes with ABSOLUTELY NO WARRANTY\n";
254 printf "This programm is licensed under the terms of the ";
255 printf "GNU General Public License\n(check source code for details)\n";
256 printf "\n\n";
257 exit $ERRORS{"UNKNOWN"};
258 }
260 sub print_help() {
261 printf "check_ifoperstatus plugin for Nagios monitors operational \n";
262 printf "status of a particular network interface on the target host\n";
263 printf "\nUsage:\n";
264 printf " -H (--hostname) Hostname to query - (required)\n";
265 printf " -C (--community) SNMP read community (defaults to public,\n";
266 printf " used with SNMP v1 and v2c\n";
267 printf " -v (--snmp_version) 1 for SNMP v1 (default)\n";
268 printf " 2 for SNMP v2c\n";
269 printf " SNMP v2c will use get_bulk for less overhead\n";
270 printf " if monitoring with -d\n";
271 printf " -L (--seclevel) choice of \"noAuthNoPriv\", \"authNoPriv\", or \"authPriv\"\n";
272 printf " -U (--secname) username for SNMPv3 context\n";
273 printf " -c (--context) SNMPv3 context name (default is empty string)";
274 printf " -A (--authpass) authentication password (cleartext ascii or localized key\n";
275 printf " in hex with 0x prefix generated by using \"snmpkey\" utility\n";
276 printf " auth password and authEngineID\n";
277 printf " -a (--authproto) Authentication protocol ( MD5 or SHA1)\n";
278 printf " -X (--privpass) privacy password (cleartext ascii or localized key\n";
279 printf " in hex with 0x prefix generated by using \"snmpkey\" utility\n";
280 printf " privacy password and authEngineID\n";
281 printf " -k (--key) SNMP IfIndex value\n";
282 printf " -d (--descr) SNMP ifDescr value\n";
283 printf " -p (--port) SNMP port (default 161)\n";
284 printf " -I (--ifmib) Agent supports IFMIB ifXTable. Do not use if\n";
285 printf " you don't know what this is. \n";
286 printf " -n (--name) the value should match the returned ifName\n";
287 printf " (Implies the use of -I)\n";
288 printf " -w (--warn =i|w|c) ignore|warn|crit if the interface is dormant (default critical)\n";
289 printf " -M (--maxmsgsize) Max message size - usefull only for v1 or v2c\n";
290 printf " -t (--timeout) seconds before the plugin times out (default=$TIMEOUT)\n";
291 printf " -V (--version) Plugin version\n";
292 printf " -h (--help) usage help \n\n";
293 printf " -k or -d must be specified\n\n";
294 printf "Note: either -k or -d must be specified and -d is much more network \n";
295 printf "intensive. Use it sparingly or not at all. -n is used to match against\n";
296 printf "a much more descriptive ifName value in the IfXTable to verify that the\n";
297 printf "snmpkey has not changed to some other network interface after a reboot.\n\n";
298 print_revision($PROGNAME, '$Revision$');
300 }
302 sub process_arguments() {
303 $status = GetOptions(
304 "V" => \$opt_V, "version" => \$opt_V,
305 "h" => \$opt_h, "help" => \$opt_h,
306 "v=i" => \$snmp_version, "snmp_version=i" => \$snmp_version,
307 "C=s" => \$community, "community=s" => \$community,
308 "L=s" => \$seclevel, "seclevel=s" => \$seclevel,
309 "a=s" => \$authproto, "authproto=s" => \$authproto,
310 "U=s" => \$secname, "secname=s" => \$secname,
311 "A=s" => \$authpass, "authpass=s" => \$authpass,
312 "X=s" => \$privpass, "privpass=s" => \$privpass,
313 "c=s" => \$context, "context=s" => \$context,
314 "k=i" => \$snmpkey, "key=i",\$snmpkey,
315 "d=s" => \$ifdescr, "descr=s" => \$ifdescr,
316 "l=s" => \$lastc, "lastchange=s" => \$lastc,
317 "p=i" => \$port, "port=i" =>\$port,
318 "H=s" => \$hostname, "hostname=s" => \$hostname,
319 "I" => \$ifXTable, "ifmib" => \$ifXTable,
320 "n=s" => \$ifName, "name=s" => \$ifName,
321 "w=s" => \$dormantWarn, "warn=s" => \$dormantWarn,
322 "M=i" => \$maxmsgsize, "maxmsgsize=i" => \$maxmsgsize,
323 "t=i" => \$timeout, "timeout=i" => \$timeout,
324 );
328 if ($status == 0){
329 print_help();
330 exit $ERRORS{'OK'};
331 }
333 if ($opt_V) {
334 print_revision($PROGNAME,'$Revision$ ');
335 exit $ERRORS{'OK'};
336 }
338 if ($opt_h) {
339 print_help();
340 exit $ERRORS{'OK'};
341 }
343 if (! utils::is_hostname($hostname)){
344 usage();
345 exit $ERRORS{"UNKNOWN"};
346 }
349 unless ($snmpkey > 0 || defined $ifdescr){
350 printf "Either a valid snmpkey key (-k) or a ifDescr (-d) must be provided)\n";
351 usage();
352 exit $ERRORS{"UNKNOWN"};
353 }
356 if (defined $name) {
357 $ifXTable=1;
358 }
360 if (defined $dormantWarn) {
361 unless ($dormantWarn =~ /^(w|c|i)$/ ) {
362 printf "Dormant alerts must be one of w|c|i \n";
363 exit $ERRORS{'UNKNOWN'};
364 }
365 }
367 unless (defined $timeout) {
368 $timeout = $TIMEOUT;
369 }
371 if ($snmp_version =~ /3/ ) {
372 # Must define a security level even though default is noAuthNoPriv
373 # v3 requires a security username
374 if (defined $seclevel && defined $secname) {
376 # Must define a security level even though defualt is noAuthNoPriv
377 unless ($seclevel eq ('noAuthNoPriv' || 'authNoPriv' || 'authPriv' ) ) {
378 usage();
379 exit $ERRORS{"UNKNOWN"};
380 }
382 # Authentication wanted
383 if ($seclevel eq ('authNoPriv' || 'authPriv') ) {
385 unless ($authproto eq ('MD5' || 'SHA1') ) {
386 usage();
387 exit $ERRORS{"UNKNOWN"};
388 }
390 if ( !defined $authpass) {
391 usage();
392 exit $ERRORS{"UNKNOWN"};
393 }else{
394 if ($authpass =~ /^0x/ ) {
395 $auth = "-authkey => $authpass" ;
396 }else{
397 $auth = "-authpassword => $authpass";
398 }
399 }
401 }
403 # Privacy (DES encryption) wanted
404 if ($seclevel eq 'authPriv' ) {
405 if (! defined $privpass) {
406 usage();
407 exit $ERRORS{"UNKNOWN"};
408 }else{
409 if ($privpass =~ /^0x/){
410 $priv = "-privkey => $privpass";
411 }else{
412 $priv = "-privpassword => $privpass";
413 }
414 }
415 }
417 # Context name defined or default
419 unless ( defined $context) {
420 $context = "";
421 }
425 }else {
426 usage();
427 exit $ERRORS{'UNKNOWN'}; ;
428 }
429 } # end snmpv3
432 if ( $snmp_version =~ /[12]/ ) {
433 ($session, $error) = Net::SNMP->session(
434 -hostname => $hostname,
435 -community => $community,
436 -port => $port,
437 -version => $snmp_version,
438 -maxmsgsize => $maxmsgsize
439 );
441 if (!defined($session)) {
442 $state='UNKNOWN';
443 $answer=$error;
444 print ("$state: $answer");
445 exit $ERRORS{$state};
446 }
448 }elsif ( $snmp_version =~ /3/ ) {
450 if ($seclevel eq 'noAuthNoPriv') {
451 ($session, $error) = Net::SNMP->session(
452 -hostname => $hostname,
453 -port => $port,
454 -version => $snmp_version,
455 -username => $secname,
456 );
458 }elsif ( $seclevel eq 'authNoPriv' ) {
459 ($session, $error) = Net::SNMP->session(
460 -hostname => $hostname,
461 -port => $port,
462 -version => $snmp_version,
463 -username => $secname,
464 $auth,
465 -authprotocol => $authproto,
466 );
467 }elsif ($seclevel eq 'authPriv' ) {
468 ($session, $error) = Net::SNMP->session(
469 -hostname => $hostname,
470 -port => $port,
471 -version => $snmp_version,
472 -username => $secname,
473 $auth,
474 -authprotocol => $authproto,
475 $priv
476 );
477 }
480 if (!defined($session)) {
481 $state='UNKNOWN';
482 $answer=$error;
483 print ("$state: $answer");
484 exit $ERRORS{$state};
485 }
487 }else{
488 $state='UNKNOWN';
489 print ("$state: No support for SNMP v$snmp_version yet\n");
490 exit $ERRORS{$state};
491 }
493 }
494 ## End validation