Code

check_ifoperstatus -n flag now works as expected (sf.net #1569488)
[nagiosplug.git] / plugins-scripts / check_ifoperstatus.pl
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 $snmpIfType       = '1.3.6.1.2.1.2.2.1.3';
71 my $snmpIfAdminStatus = '1.3.6.1.2.1.2.2.1.7';
72 my $snmpIfOperStatus = '1.3.6.1.2.1.2.2.1.8';
73 my $snmpIfName       = '1.3.6.1.2.1.31.1.1.1.1';
74 my $snmpIfLastChange = '1.3.6.1.2.1.2.2.1.9';
75 my $snmpIfAlias      = '1.3.6.1.2.1.31.1.1.1.18';
76 my $snmpLocIfDescr   = '1.3.6.1.4.1.9.2.2.1.1.28';
77 my $hostname;
78 my $ifName;
79 my $session;
80 my $error;
81 my $response;
82 my $snmp_version = 1 ;
83 my $ifXTable;
84 my $opt_h ;
85 my $opt_V ;
86 my $ifdescr;
87 my $iftype;
88 my $key;
89 my $lastc;
90 my $dormantWarn;
91 my $adminWarn;
92 my $name;
94 ### Validate Arguments
96 $status = process_arguments();
99 # Just in case of problems, let's not hang Nagios
100 $SIG{'ALRM'} = sub {
101      print ("ERROR: No snmp response from $hostname (alarm)\n");
102      exit $ERRORS{"UNKNOWN"};
103 };
105 alarm($timeout);
108 ## map ifdescr to ifindex - should look at being able to cache this value
110 if (defined $ifdescr || defined $iftype) {
111         # escape "/" in ifdescr - very common in the Cisco world
112         if (defined $iftype) {
113                 $status=fetch_ifindex($snmpIfType, $iftype);  
114         } else {
115                 $ifdescr =~ s/\//\\\//g;
116                 $status=fetch_ifindex($snmpIfDescr, $ifdescr);  # if using on device with large number of interfaces
117                                                                 # recommend use of SNMP v2 (get-bulk)
118         }
119         if ($status==0) {
120                 $state = "UNKNOWN";
121                 printf "$state: could not retrive ifdescr/iftype snmpkey - $status-$snmpkey\n";
122                 $session->close;
123                 exit $ERRORS{$state};
124         }
128 ## Main function
130 $snmpIfAdminStatus = $snmpIfAdminStatus . "." . $snmpkey;
131 $snmpIfOperStatus = $snmpIfOperStatus . "." . $snmpkey;
132 $snmpIfDescr = $snmpIfDescr . "." . $snmpkey;
133 $snmpIfName     = $snmpIfName . "." . $snmpkey ;
134 $snmpIfAlias = $snmpIfAlias . "." . $snmpkey ; 
136 push(@snmpoids,$snmpIfAdminStatus);
137 push(@snmpoids,$snmpIfOperStatus);
138 push(@snmpoids,$snmpIfDescr);
139 push(@snmpoids,$snmpIfName) if (defined $ifXTable) ;
140 push(@snmpoids,$snmpIfAlias) if (defined $ifXTable) ;
142    if (!defined($response = $session->get_request(@snmpoids))) {
143       $answer=$session->error;
144       $session->close;
145       $state = 'WARNING';
146       print ("$state: SNMP error: $answer\n");
147       exit $ERRORS{$state};
148    }
150    $answer = sprintf("host '%s', %s(%s) is %s\n", 
151       $hostname, 
152       $response->{$snmpIfDescr},
153       $snmpkey, 
154       $ifOperStatus{$response->{$snmpIfOperStatus}}
155    );
158    ## Check to see if ifName match is requested and it matches - exit if no match
159    ## not the interface we want to monitor
160    if ( defined $ifName && not ($response->{$snmpIfName} eq $ifName) ) {
161       $state = 'UNKNOWN';
162       $answer = "Interface name ($ifName) doesn't match snmp value ($response->{$snmpIfName}) (index $snmpkey)";
163       print ("$state: $answer\n");
164       exit $ERRORS{$state};
165    } 
167    ## define the interface name
168    if (defined $ifXTable) {
169      $name = $response->{$snmpIfName} ." - " .$response->{$snmpIfAlias} ; 
170    }else{
171      $name = $response->{$snmpIfDescr} ;
172    }
173    
174    ## if AdminStatus is down - some one made a consious effort to change config
175    ##
176    if ( not ($response->{$snmpIfAdminStatus} == 1) ) {
177      $answer = "Interface $name (index $snmpkey) is administratively down.";
178      if ( not defined $adminWarn or $adminWarn eq "w" ) {
179         $state = 'WARNING';
180      } elsif ( $adminWarn eq "i" ) {
181         $state = 'OK';
182      } elsif ( $adminWarn eq "c" ) {
183         $state = 'CRITICAL';
184      } else { # If wrong value for -a, say warning
185         $state = 'WARNING';
186      }
187    } 
188    ## Check operational status
189    elsif ( $response->{$snmpIfOperStatus} == 2 ) {
190       $state = 'CRITICAL';
191       $answer = "Interface $name (index $snmpkey) is down.";
192    } elsif ( $response->{$snmpIfOperStatus} == 5 ) {
193       if (defined $dormantWarn ) {
194                                 if ($dormantWarn eq "w") {
195                           $state = 'WARNING';
196                                   $answer = "Interface $name (index $snmpkey) is dormant.";
197                   }elsif($dormantWarn eq "c") {
198                         $state = 'CRITICAL';
199                                   $answer = "Interface $name (index $snmpkey) is dormant.";
200         }elsif($dormantWarn eq "i") {
201                           $state = 'OK';
202                                 $answer = "Interface $name (index $snmpkey) is dormant.";
203         }
204                         }else{
205                 # dormant interface  - but warning/critical/ignore not requested
206                    $state = 'CRITICAL';
207                    $answer = "Interface $name (index $snmpkey) is dormant.";
208                         }
209    } elsif ( $response->{$snmpIfOperStatus} == 6 ) {
210            $state = 'CRITICAL';
211            $answer = "Interface $name (index $snmpkey) notPresent - possible hotswap in progress.";
212    } elsif ( $response->{$snmpIfOperStatus} == 7 ) {
213            $state = 'CRITICAL';
214            $answer = "Interface $name (index $snmpkey) down due to lower layer being down.";
216    } elsif ( $response->{$snmpIfOperStatus} == 3 || $response->{$snmpIfOperStatus} == 4  ) {
217            $state = 'CRITICAL';
218            $answer = "Interface $name (index $snmpkey) down (testing/unknown).";
220    } else {
221       $state = 'OK';
222       $answer = "Interface $name (index $snmpkey) is up.";
223    }
227 print ("$state: $answer\n");
228 exit $ERRORS{$state};
231 ### subroutines
233 sub fetch_ifindex {
234         my $oid = shift;
235         my $lookup = shift;
237         if (!defined ($response = $session->get_table($oid))) {
238                 $answer=$session->error;
239                 $session->close;
240                 $state = 'CRITICAL';
241                 printf ("$state: SNMP error with snmp version $snmp_version ($answer)\n");
242                 $session->close;
243                 exit $ERRORS{$state};
244         }
245         
246         foreach $key ( keys %{$response}) {
247                 if ($response->{$key} =~ /^$lookup$/) {
248                         $key =~ /.*\.(\d+)$/;
249                         $snmpkey = $1;
250                         #print "$lookup = $key / $snmpkey \n";  #debug
251                 }
252         }
253         unless (defined $snmpkey) {
254                 $session->close;
255                 $state = 'CRITICAL';
256                 printf "$state: Could not match $ifdescr on $hostname\n";
257                 exit $ERRORS{$state};
258         }
259         
260         return $snmpkey;
263 sub usage() {
264   printf "\nMissing arguments!\n";
265   printf "\n";
266   printf "usage: \n";
267   printf "check_ifoperstatus -k <IF_KEY> -H <HOSTNAME> [-C <community>]\n";
268   printf "Copyright (C) 2000 Christoph Kron\n";
269   printf "check_ifoperstatus.pl comes with ABSOLUTELY NO WARRANTY\n";
270   printf "This programm is licensed under the terms of the ";
271   printf "GNU General Public License\n(check source code for details)\n";
272   printf "\n\n";
273   exit $ERRORS{"UNKNOWN"};
276 sub print_help() {
277         printf "check_ifoperstatus plugin for Nagios monitors operational \n";
278         printf "status of a particular network interface on the target host\n";
279         printf "\nUsage:\n";
280         printf "   -H (--hostname)   Hostname to query - (required)\n";
281         printf "   -C (--community)  SNMP read community (defaults to public,\n";
282         printf "                     used with SNMP v1 and v2c\n";
283         printf "   -v (--snmp_version)  1 for SNMP v1 (default)\n";
284         printf "                        2 for SNMP v2c\n";
285         printf "                        SNMP v2c will use get_bulk for less overhead\n";
286         printf "                        if monitoring with -d\n";
287         printf "   -L (--seclevel)   choice of \"noAuthNoPriv\", \"authNoPriv\", or     \"authPriv\"\n";
288         printf "   -U (--secname)    username for SNMPv3 context\n";
289         printf "   -c (--context)    SNMPv3 context name (default is empty      string)";
290         printf "   -A (--authpass)   authentication password (cleartext ascii or localized key\n";
291         printf "                     in hex with 0x prefix generated by using   \"snmpkey\" utility\n"; 
292         printf "                     auth password and authEngineID\n";
293         printf "   -a (--authproto)  Authentication protocol ( MD5 or SHA1)\n";
294         printf "   -X (--privpass)   privacy password (cleartext ascii or localized key\n";
295         printf "                     in hex with 0x prefix generated by using   \"snmpkey\" utility\n"; 
296         printf "                     privacy password and authEngineID\n";
297         printf "   -k (--key)        SNMP IfIndex value\n";
298         printf "   -d (--descr)      SNMP ifDescr value\n";
299         printf "   -T (--type)       SNMP ifType integer value (see http://www.iana.org/assignments/ianaiftype-mib)\n";
300         printf "   -p (--port)       SNMP port (default 161)\n";
301         printf "   -I (--ifmib)      Agent supports IFMIB ifXTable.  Do not use if\n";
302         printf "                     you don't know what this is. \n";
303         printf "   -n (--name)       the value should match the returned ifName\n";
304         printf "                     (Implies the use of -I)\n";
305         printf "   -w (--warn =i|w|c) ignore|warn|crit if the interface is dormant (default critical)\n";
306         printf "   -D (--admin-down =i|w|c) same for administratively down interfaces (default warning)\n";
307         printf "   -M (--maxmsgsize) Max message size - usefull only for v1 or v2c\n";
308         printf "   -t (--timeout)    seconds before the plugin times out (default=$TIMEOUT)\n";
309         printf "   -V (--version)    Plugin version\n";
310         printf "   -h (--help)       usage help \n\n";
311         printf " -k or -d or -T must be specified\n\n";
312         printf "Note: either -k or -d or -T must be specified and -d and -T are much more network \n";
313         printf "intensive.  Use it sparingly or not at all.  -n is used to match against\n";
314         printf "a much more descriptive ifName value in the IfXTable to verify that the\n";
315         printf "snmpkey has not changed to some other network interface after a reboot.\n\n";
316         print_revision($PROGNAME, '$Revision$');
317         
320 sub process_arguments() {
321         $status = GetOptions(
322                         "V"   => \$opt_V, "version"    => \$opt_V,
323                         "h"   => \$opt_h, "help"       => \$opt_h,
324                         "v=i" => \$snmp_version, "snmp_version=i"  => \$snmp_version,
325                         "C=s" => \$community, "community=s" => \$community,
326                         "L=s" => \$seclevel, "seclevel=s" => \$seclevel,
327                         "a=s" => \$authproto, "authproto=s" => \$authproto,
328                         "U=s" => \$secname,   "secname=s"   => \$secname,
329                         "A=s" => \$authpass,  "authpass=s"  => \$authpass,
330                         "X=s" => \$privpass,  "privpass=s"  => \$privpass,
331                         "c=s" => \$context,   "context=s"   => \$context,
332                         "k=i" => \$snmpkey, "key=i",\$snmpkey,
333                         "d=s" => \$ifdescr, "descr=s" => \$ifdescr,
334                         "l=s" => \$lastc,  "lastchange=s" => \$lastc,
335                         "p=i" => \$port,  "port=i" =>\$port,
336                         "H=s" => \$hostname, "hostname=s" => \$hostname,
337                         "I"   => \$ifXTable, "ifmib" => \$ifXTable,
338                         "n=s" => \$ifName, "name=s" => \$ifName,
339                         "w=s" => \$dormantWarn, "warn=s" => \$dormantWarn,
340                         "D=s" => \$adminWarn, "admin-down=s" => \$adminWarn,
341                         "M=i" => \$maxmsgsize, "maxmsgsize=i" => \$maxmsgsize,
342                         "t=i" => \$timeout,    "timeout=i" => \$timeout,
343                         "T=i" => \$iftype,    "type=i" => \$iftype,
344                         );
347                                 
348         if ($status == 0){
349                 print_help();
350                 exit $ERRORS{'OK'};
351         }
352   
353         if ($opt_V) {
354                 print_revision($PROGNAME,'$Revision$ ');
355                 exit $ERRORS{'OK'};
356         }
358         if ($opt_h) {
359                 print_help();
360                 exit $ERRORS{'OK'};
361         }
363         if (! utils::is_hostname($hostname)){
364                 usage();
365                 exit $ERRORS{"UNKNOWN"};
366         }
369         unless ($snmpkey > 0 || defined $ifdescr || defined $iftype){
370                 printf "Either a valid snmpkey key (-k) or a ifDescr (-d) must be provided)\n";
371                 usage();
372                 exit $ERRORS{"UNKNOWN"};
373         }
376         if (defined $name) {
377                 $ifXTable=1;
378         }       
380         if (defined $dormantWarn) {
381                 unless ($dormantWarn =~ /^(w|c|i)$/ ) {
382                         printf "Dormant alerts must be one of w|c|i \n";
383                         exit $ERRORS{'UNKNOWN'};
384                 }
385         }
386         
387         unless (defined $timeout) {
388                 $timeout = $TIMEOUT;
389         }
391         if ($snmp_version =~ /3/ ) {
392                 # Must define a security level even though default is noAuthNoPriv
393                 # v3 requires a security username
394                 if (defined $seclevel  && defined $secname) {
395                 
396                         # Must define a security level even though defualt is noAuthNoPriv
397                         unless ( grep /^$seclevel$/, qw(noAuthNoPriv authNoPriv authPriv) ) {
398                                 usage();
399                                 exit $ERRORS{"UNKNOWN"};
400                         }
401                         
402                         # Authentication wanted
403                         if ( $seclevel eq 'authNoPriv' || $seclevel eq 'authPriv' ) {
404                 
405                                 unless ( $authproto eq 'MD5' || $authproto eq 'SHA1' ) {
406                                         usage();
407                                         exit $ERRORS{"UNKNOWN"};
408                                 }
410                                 if ( !defined $authpass) {
411                                         usage();
412                                         exit $ERRORS{"UNKNOWN"};
413                                 }else{
414                                         if ($authpass =~ /^0x/ ) {
415                                                 $auth = "-authkey => $authpass" ;
416                                         }else{
417                                                 $auth = "-authpassword => $authpass";
418                                         }
419                                 }
420                                         
421                         }
422                         
423                         # Privacy (DES encryption) wanted
424                         if ($seclevel eq  'authPriv' ) {
425                                 if (! defined $privpass) {
426                                         usage();
427                                         exit $ERRORS{"UNKNOWN"};
428                                 }else{
429                                         if ($privpass =~ /^0x/){
430                                                 $priv = "-privkey => $privpass";
431                                         }else{
432                                                 $priv = "-privpassword => $privpass";
433                                         }
434                                 }
435                         }
437                         # Context name defined or default
439                         unless ( defined $context) {
440                                 $context = "";
441                         }
442                 
443                 
444                 
445                 }else {
446                                         usage();
447                                         exit $ERRORS{'UNKNOWN'}; ;
448                 }
449         } # end snmpv3
452         if ( $snmp_version =~ /[12]/ ) {
453         ($session, $error) = Net::SNMP->session(
454                         -hostname  => $hostname,
455                         -community => $community,
456                         -port      => $port,
457                         -version        => $snmp_version,
458                         -maxmsgsize => $maxmsgsize
459                 );
461                 if (!defined($session)) {
462                         $state='UNKNOWN';
463                         $answer=$error;
464                         print ("$state: $answer\n");
465                         exit $ERRORS{$state};
466                 }
467         
468         }elsif ( $snmp_version =~ /3/ ) {
470                 if ($seclevel eq 'noAuthNoPriv') {
471                         ($session, $error) = Net::SNMP->session(
472                                 -hostname  => $hostname,
473                                 -port      => $port,
474                                 -version  => $snmp_version,
475                                 -username => $secname,
476                         );
478                 }elsif ( $seclevel eq 'authNoPriv' ) {
479                         ($session, $error) = Net::SNMP->session(
480                                 -hostname  => $hostname,
481                                 -port      => $port,
482                                 -version  => $snmp_version,
483                                 -username => $secname,
484                                 $auth,
485                                 -authprotocol => $authproto,
486                         );      
487                 }elsif ($seclevel eq 'authPriv' ) {
488                         ($session, $error) = Net::SNMP->session(
489                                 -hostname  => $hostname,
490                                 -port      => $port,
491                                 -version  => $snmp_version,
492                                 -username => $secname,
493                                 $auth,
494                                 -authprotocol => $authproto,
495                                 $priv
496                         );
497                 }
498                                         
499                                         
500                 if (!defined($session)) {
501                                         $state='UNKNOWN';
502                                         $answer=$error;
503                                         print ("$state: $answer\n");
504                                         exit $ERRORS{$state};
505                 }
507         }else{
508                 $state='UNKNOWN';
509                 print ("$state: No support for SNMP v$snmp_version yet\n");
510                 exit $ERRORS{$state};
511         }
514 ## End validation