Code

8a23ca5eab902d1372ab6bae0d639028d83717a6
[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 $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 $snmpIfAdminStatus = '1.3.6.1.2.1.2.2.1.7';
70 my $snmpIfOperStatus = '1.3.6.1.2.1.2.2.1.8';
71 my $snmpIfName       = '1.3.6.1.2.1.31.1.1.1.1';
72 my $snmpIfLastChange = '1.3.6.1.2.1.2.2.1.9';
73 my $snmpIfAlias      = '1.3.6.1.2.1.31.1.1.1.18';
74 my $snmpLocIfDescr   = '1.3.6.1.4.1.9.2.2.1.1.28';
75 my $hostname;
76 my $ifName;
77 my $session;
78 my $error;
79 my $response;
80 my $snmp_version = 1 ;
81 my $ifXTable;
82 my $opt_h ;
83 my $opt_V ;
84 my $ifdescr;
85 my $key;
86 my $lastc;
87 my $dormantWarn;
88 my $name;
92 # Just in case of problems, let's not hang Nagios
93 $SIG{'ALRM'} = sub {
94      print ("ERROR: No snmp response from $hostname (alarm)\n");
95      exit $ERRORS{"UNKNOWN"};
96 };
97 alarm($TIMEOUT);
100 ### Validate Arguments
102 $status = process_arguments();
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         }
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    }
167    
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         }
229         
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         }
243         
244         return $snmpkey;
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"};
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 "   -V (--version)    Plugin version\n";
291         printf "   -h (--help)       usage help \n\n";
292         printf " -k or -d must be specified\n\n";
293         printf "Note: either -k or -d must be specified and -d is much more network \n";
294         printf "intensive.  Use it sparingly or not at all.  -n is used to match against\n";
295         printf "a much more descriptive ifName value in the IfXTable to verify that the\n";
296         printf "snmpkey has not changed to some other network interface after a reboot.\n\n";
297         print_revision($PROGNAME, '$Revision$');
298         
301 sub process_arguments() {
302         $status = GetOptions(
303                         "V"   => \$opt_V, "version"    => \$opt_V,
304                         "h"   => \$opt_h, "help"       => \$opt_h,
305                         "v=i" => \$snmp_version, "snmp_version=i"  => \$snmp_version,
306                         "C=s" => \$community, "community=s" => \$community,
307                         "L=s" => \$seclevel, "seclevel=s" => \$seclevel,
308                         "a=s" => \$authproto, "authproto=s" => \$authproto,
309                         "U=s" => \$secname,   "secname=s"   => \$secname,
310                         "A=s" => \$authpass,  "authpass=s"  => \$authpass,
311                         "X=s" => \$privpass,  "privpass=s"  => \$privpass,
312                         "c=s" => \$context,   "context=s"   => \$context,
313                         "k=i" => \$snmpkey, "key=i",\$snmpkey,
314                         "d=s" => \$ifdescr, "descr=s" => \$ifdescr,
315                         "l=s" => \$lastc,  "lastchange=s" => \$lastc,
316                         "p=i" = >\$port,  "port=i" =>\$port,
317                         "H=s" => \$hostname, "hostname=s" => \$hostname,
318                         "I"       => \$ifXTable, "ifmib" => \$ifXTable,
319                         "n=s" => \$ifName, "name=s" => \$ifName,
320                         "w=s" => \$dormantWarn, "warn=s" => \$dormantWarn,
321                         "M=i" => \$maxmsgsize, "maxmsgsize=i" => \$maxmsgsize);
324                                 
325         if ($status == 0){
326                 print_help();
327                 exit $ERRORS{'OK'};
328         }
329   
330         if ($opt_V) {
331                 print_revision($PROGNAME,'$Revision$ ');
332                 exit $ERRORS{'OK'};
333         }
335         if ($opt_h) {
336                 print_help();
337                 exit $ERRORS{'OK'};
338         }
340         if (! utils::is_hostname($hostname)){
341                 usage();
342                 exit $ERRORS{"UNKNOWN"};
343         }
346         unless ($snmpkey > 0 || defined $ifdescr){
347                 printf "Either a valid snmpkey key (-k) or a ifDescr (-d) must be provided)\n";
348                 usage();
349                 exit $ERRORS{"UNKNOWN"};
350         }
353         if (defined $name) {
354                 $ifXTable=1;
355         }       
357         if (defined $dormantWarn) {
358                 unless ($dormantWarn =~ /^(w|c|i)$/ ) {
359                         printf "Dormant alerts must be one of w|c|i \n";
360                         exit $ERRORS{'UNKNOWN'};
361                 }
362         }
363         
364         if ($snmp_version =~ /3/ ) {
365                 # Must define a security level even though default is noAuthNoPriv
366                 # v3 requires a security username
367                 if (defined $seclevel  && defined $secname) {
368                 
369                         # Must define a security level even though defualt is noAuthNoPriv
370                         unless ($seclevel eq ('noAuthNoPriv' || 'authNoPriv' || 'authPriv' ) ) {
371                                 usage();
372                                 exit $ERRORS{"UNKNOWN"};
373                         }
374                         
375                         # Authentication wanted
376                         if ($seclevel eq ('authNoPriv' || 'authPriv') ) {
377                 
378                                 unless ($authproto eq ('MD5' || 'SHA1') ) {
379                                         usage();
380                                         exit $ERRORS{"UNKNOWN"};
381                                 }
383                                 if ( !defined $authpass) {
384                                         usage();
385                                         exit $ERRORS{"UNKNOWN"};
386                                 }else{
387                                         if ($authpass =~ /^0x/ ) {
388                                                 $auth = "-authkey => $authpass" ;
389                                         }else{
390                                                 $auth = "-authpassword => $authpass";
391                                         }
392                                 }
393                                         
394                         }
395                         
396                         # Privacy (DES encryption) wanted
397                         if ($seclevel eq  'authPriv' ) {
398                                 if (! defined $privpass) {
399                                         usage();
400                                         exit $ERRORS{"UNKNOWN"};
401                                 }else{
402                                         if ($privpass =~ /^0x/){
403                                                 $priv = "-privkey => $privpass";
404                                         }else{
405                                                 $priv = "-privpassword => $privpass";
406                                         }
407                                 }
408                         }
410                         # Context name defined or default
412                         unless ( defined $context) {
413                                 $context = "";
414                         }
415                 
416                 
417                 
418                 }else {
419                                         usage();
420                                         exit $ERRORS{'UNKNOWN'}; ;
421                 }
422         } # end snmpv3
425         if ( $snmp_version =~ /[12]/ ) {
426         ($session, $error) = Net::SNMP->session(
427                         -hostname  => $hostname,
428                         -community => $community,
429                         -port      => $port,
430                         -version        => $snmp_version,
431                         -maxmsgsize => $maxmsgsize
432                 );
434                 if (!defined($session)) {
435                         $state='UNKNOWN';
436                         $answer=$error;
437                         print ("$state: $answer");
438                         exit $ERRORS{$state};
439                 }
440         
441         }elsif ( $snmp_version =~ /3/ ) {
443                 if ($seclevel eq 'noAuthNoPriv') {
444                         ($session, $error) = Net::SNMP->session(
445                                 -hostname  => $hostname,
446                                 -port      => $port,
447                                 -version  => $snmp_version,
448                                 -username => $secname,
449                         );
451                 }elsif ( $seclevel eq 'authNoPriv' ) {
452                         ($session, $error) = Net::SNMP->session(
453                                 -hostname  => $hostname,
454                                 -port      => $port,
455                                 -version  => $snmp_version,
456                                 -username => $secname,
457                                 $auth,
458                                 -authprotocol => $authproto,
459                         );      
460                 }elsif ($seclevel eq 'authPriv' ) {
461                         ($session, $error) = Net::SNMP->session(
462                                 -hostname  => $hostname,
463                                 -port      => $port,
464                                 -version  => $snmp_version,
465                                 -username => $secname,
466                                 $auth,
467                                 -authprotocol => $authproto,
468                                 $priv
469                         );
470                 }
471                                         
472                                         
473                 if (!defined($session)) {
474                                         $state='UNKNOWN';
475                                         $answer=$error;
476                                         print ("$state: $answer");
477                                         exit $ERRORS{$state};
478                 }
480         }else{
481                 $state='UNKNOWN';
482                 print ("$state: No support for SNMP v$snmp_version yet\n");
483                 exit $ERRORS{$state};
484         }
487 ## End validation