Code

Exporting ENV vars from Makefile is supported only with GNU Make. Use the shell instead.
[nagiosplug.git] / plugins-scripts / check_ifoperstatus.pl
old mode 100644 (file)
new mode 100755 (executable)
index 8a23ca5..d0a1655
@@ -25,7 +25,6 @@
 # Report bugs to:  nagiosplug-help@lists.sourceforge.net
 #
 # 11.01.2000 Version 1.0
-# $Id$
 #
 # Patches from Guy Van Den Bergh to warn on ifadminstatus down interfaces
 # instead of critical.
@@ -47,6 +46,7 @@ sub print_help ();
 sub usage ();
 sub process_arguments ();
 
+my $timeout;
 my $status;
 my %ifOperStatus =     ('1','up',
                         '2','down',
@@ -66,6 +66,7 @@ my $port = 161;
 my @snmpoids;
 my $sysUptime        = '1.3.6.1.2.1.1.3.0';
 my $snmpIfDescr      = '1.3.6.1.2.1.2.2.1.2';
+my $snmpIfType       = '1.3.6.1.2.1.2.2.1.3';
 my $snmpIfAdminStatus = '1.3.6.1.2.1.2.2.1.7';
 my $snmpIfOperStatus = '1.3.6.1.2.1.2.2.1.8';
 my $snmpIfName       = '1.3.6.1.2.1.31.1.1.1.1';
@@ -82,11 +83,16 @@ my $ifXTable;
 my $opt_h ;
 my $opt_V ;
 my $ifdescr;
+my $iftype;
 my $key;
 my $lastc;
 my $dormantWarn;
+my $adminWarn;
 my $name;
 
+### Validate Arguments
+
+$status = process_arguments();
 
 
 # Just in case of problems, let's not hang Nagios
@@ -94,25 +100,24 @@ $SIG{'ALRM'} = sub {
      print ("ERROR: No snmp response from $hostname (alarm)\n");
      exit $ERRORS{"UNKNOWN"};
 };
-alarm($TIMEOUT);
-
 
-### Validate Arguments
-
-$status = process_arguments();
+alarm($timeout);
 
 
 ## map ifdescr to ifindex - should look at being able to cache this value
 
-if (defined $ifdescr) {
+if (defined $ifdescr || defined $iftype) {
        # escape "/" in ifdescr - very common in the Cisco world
-       $ifdescr =~ s/\//\\\//g;
-
-       $status=fetch_ifdescr();  # if using on device with large number of interfaces
-                                                         # recommend use of SNMP v2 (get-bulk)
+       if (defined $iftype) {
+               $status=fetch_ifindex($snmpIfType, $iftype);  
+       } else {
+               $ifdescr =~ s/\//\\\//g;
+               $status=fetch_ifindex($snmpIfDescr, $ifdescr);  # if using on device with large number of interfaces
+                                                               # recommend use of SNMP v2 (get-bulk)
+       }
        if ($status==0) {
                $state = "UNKNOWN";
-               printf "$state: could not retrive ifdescr snmpkey - $status-$snmpkey\n";
+               printf "$state: could not retrive ifdescr/iftype snmpkey - $status-$snmpkey\n";
                $session->close;
                exit $ERRORS{$state};
        }
@@ -151,10 +156,10 @@ push(@snmpoids,$snmpIfAlias) if (defined $ifXTable) ;
 
    ## Check to see if ifName match is requested and it matches - exit if no match
    ## not the interface we want to monitor
-   if ( defined $name && not ($response->{$snmpIfName} eq $name) ) {
+   if ( defined $ifName && not ($response->{$snmpIfName} eq $ifName) ) {
       $state = 'UNKNOWN';
-      $answer = "Interface name ($name) doesn't match snmp value ($response->{$snmpIfName}) (index $snmpkey)";
-      print ("$state: $answer");
+      $answer = "Interface name ($ifName) doesn't match snmp value ($response->{$snmpIfName}) (index $snmpkey)";
+      print ("$state: $answer\n");
       exit $ERRORS{$state};
    } 
 
@@ -168,9 +173,16 @@ push(@snmpoids,$snmpIfAlias) if (defined $ifXTable) ;
    ## if AdminStatus is down - some one made a consious effort to change config
    ##
    if ( not ($response->{$snmpIfAdminStatus} == 1) ) {
-      $state = 'WARNING';
-      $answer = "Interface $name (index $snmpkey) is administratively down.";
-
+     $answer = "Interface $name (index $snmpkey) is administratively down.";
+     if ( not defined $adminWarn or $adminWarn eq "w" ) {
+        $state = 'WARNING';
+     } elsif ( $adminWarn eq "i" ) {
+        $state = 'OK';
+     } elsif ( $adminWarn eq "c" ) {
+        $state = 'CRITICAL';
+     } else { # If wrong value for -a, say warning
+        $state = 'WARNING';
+     }
    } 
    ## Check operational status
    elsif ( $response->{$snmpIfOperStatus} == 2 ) {
@@ -179,7 +191,7 @@ push(@snmpoids,$snmpIfAlias) if (defined $ifXTable) ;
    } elsif ( $response->{$snmpIfOperStatus} == 5 ) {
       if (defined $dormantWarn ) {
                                if ($dormantWarn eq "w") {
-                         $state = 'WARNNG';
+                         $state = 'WARNING';
                                  $answer = "Interface $name (index $snmpkey) is dormant.";
                  }elsif($dormantWarn eq "c") {
                        $state = 'CRITICAL';
@@ -211,14 +223,17 @@ push(@snmpoids,$snmpIfAlias) if (defined $ifXTable) ;
 
 
 
-print ("$state: $answer");
+print ("$state: $answer\n");
 exit $ERRORS{$state};
 
 
 ### subroutines
 
-sub fetch_ifdescr {
-       if (!defined ($response = $session->get_table($snmpIfDescr))) {
+sub fetch_ifindex {
+       my $oid = shift;
+       my $lookup = shift;
+
+       if (!defined ($response = $session->get_table($oid))) {
                $answer=$session->error;
                $session->close;
                $state = 'CRITICAL';
@@ -228,10 +243,10 @@ sub fetch_ifdescr {
        }
        
        foreach $key ( keys %{$response}) {
-               if ($response->{$key} =~ /^$ifdescr$/) {
+               if ($response->{$key} =~ /^$lookup$/) {
                        $key =~ /.*\.(\d+)$/;
                        $snmpkey = $1;
-                       #print "$ifdescr = $key / $snmpkey \n";  #debug
+                       #print "$lookup = $key / $snmpkey \n";  #debug
                }
        }
        unless (defined $snmpkey) {
@@ -280,21 +295,24 @@ sub print_help() {
        printf "                     privacy password and authEngineID\n";
        printf "   -k (--key)        SNMP IfIndex value\n";
        printf "   -d (--descr)      SNMP ifDescr value\n";
+       printf "   -T (--type)       SNMP ifType integer value (see http://www.iana.org/assignments/ianaiftype-mib)\n";
        printf "   -p (--port)       SNMP port (default 161)\n";
        printf "   -I (--ifmib)      Agent supports IFMIB ifXTable.  Do not use if\n";
        printf "                     you don't know what this is. \n";
        printf "   -n (--name)       the value should match the returned ifName\n";
        printf "                     (Implies the use of -I)\n";
        printf "   -w (--warn =i|w|c) ignore|warn|crit if the interface is dormant (default critical)\n";
+       printf "   -D (--admin-down =i|w|c) same for administratively down interfaces (default warning)\n";
        printf "   -M (--maxmsgsize) Max message size - usefull only for v1 or v2c\n";
+       printf "   -t (--timeout)    seconds before the plugin times out (default=$TIMEOUT)\n";
        printf "   -V (--version)    Plugin version\n";
        printf "   -h (--help)       usage help \n\n";
-       printf " -k or -d must be specified\n\n";
-       printf "Note: either -k or -d must be specified and -d is much more network \n";
+       printf " -k or -d or -T must be specified\n\n";
+       printf "Note: either -k or -d or -T must be specified and -d and -T are much more network \n";
        printf "intensive.  Use it sparingly or not at all.  -n is used to match against\n";
        printf "a much more descriptive ifName value in the IfXTable to verify that the\n";
        printf "snmpkey has not changed to some other network interface after a reboot.\n\n";
-       print_revision($PROGNAME, '$Revision$');
+       print_revision($PROGNAME, '@NP_VERSION@');
        
 }
 
@@ -313,12 +331,16 @@ sub process_arguments() {
                        "k=i" => \$snmpkey, "key=i",\$snmpkey,
                        "d=s" => \$ifdescr, "descr=s" => \$ifdescr,
                        "l=s" => \$lastc,  "lastchange=s" => \$lastc,
-                       "p=i" = >\$port,  "port=i" =>\$port,
+                       "p=i" =\$port,  "port=i" =>\$port,
                        "H=s" => \$hostname, "hostname=s" => \$hostname,
-                       "I"       => \$ifXTable, "ifmib" => \$ifXTable,
+                       "I"   => \$ifXTable, "ifmib" => \$ifXTable,
                        "n=s" => \$ifName, "name=s" => \$ifName,
                        "w=s" => \$dormantWarn, "warn=s" => \$dormantWarn,
-                       "M=i" => \$maxmsgsize, "maxmsgsize=i" => \$maxmsgsize);
+                       "D=s" => \$adminWarn, "admin-down=s" => \$adminWarn,
+                       "M=i" => \$maxmsgsize, "maxmsgsize=i" => \$maxmsgsize,
+                       "t=i" => \$timeout,    "timeout=i" => \$timeout,
+                       "T=i" => \$iftype,    "type=i" => \$iftype,
+                       );
 
 
                                
@@ -328,7 +350,7 @@ sub process_arguments() {
        }
   
        if ($opt_V) {
-               print_revision($PROGNAME,'$Revision$ ');
+               print_revision($PROGNAME,'@NP_VERSION@');
                exit $ERRORS{'OK'};
        }
 
@@ -343,7 +365,7 @@ sub process_arguments() {
        }
 
 
-       unless ($snmpkey > 0 || defined $ifdescr){
+       unless ($snmpkey > 0 || defined $ifdescr || defined $iftype){
                printf "Either a valid snmpkey key (-k) or a ifDescr (-d) must be provided)\n";
                usage();
                exit $ERRORS{"UNKNOWN"};
@@ -361,21 +383,25 @@ sub process_arguments() {
                }
        }
        
+       unless (defined $timeout) {
+               $timeout = $TIMEOUT;
+       }
+
        if ($snmp_version =~ /3/ ) {
                # Must define a security level even though default is noAuthNoPriv
                # v3 requires a security username
                if (defined $seclevel  && defined $secname) {
                
                        # Must define a security level even though defualt is noAuthNoPriv
-                       unless ($seclevel eq ('noAuthNoPriv' || 'authNoPriv' || 'authPriv' ) ) {
+                       unless ( grep /^$seclevel$/, qw(noAuthNoPriv authNoPriv authPriv) ) {
                                usage();
                                exit $ERRORS{"UNKNOWN"};
                        }
                        
                        # Authentication wanted
-                       if ($seclevel eq ('authNoPriv' || 'authPriv') ) {
+                       if ( $seclevel eq 'authNoPriv' || $seclevel eq 'authPriv' ) {
                
-                               unless ($authproto eq ('MD5' || 'SHA1') ) {
+                               unless ( $authproto eq 'MD5' || $authproto eq 'SHA1' ) {
                                        usage();
                                        exit $ERRORS{"UNKNOWN"};
                                }
@@ -434,7 +460,7 @@ sub process_arguments() {
                if (!defined($session)) {
                        $state='UNKNOWN';
                        $answer=$error;
-                       print ("$state: $answer");
+                       print ("$state: $answer\n");
                        exit $ERRORS{$state};
                }
        
@@ -473,7 +499,7 @@ sub process_arguments() {
                if (!defined($session)) {
                                        $state='UNKNOWN';
                                        $answer=$error;
-                                       print ("$state: $answer");
+                                       print ("$state: $answer\n");
                                        exit $ERRORS{$state};
                }