Code

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