diff --git a/plugins-scripts/check_ifstatus.pl b/plugins-scripts/check_ifstatus.pl index 709ad1741..9cfe1496e 100755 --- a/plugins-scripts/check_ifstatus.pl +++ b/plugins-scripts/check_ifstatus.pl @@ -1,14 +1,17 @@ #!@PERL@ -w # # check_ifstatus.pl - monitoring plugin -# +# # # Copyright (C) 2000 Christoph Kron -# Modified 5/2002 to conform to updated Monitoring Plugins Guidelines (S. Ghosh) +# Modified 5/2002 to conform to updated Nagios Plugin Guidelines (S. Ghosh) +# Modified 7/2014 to allow ignoring by interface descriptor (N. Peelman) +# # Added -x option (4/2003) # Added -u option (4/2003) # Added -M option (10/2003) # Added SNMPv3 support (10/2003) +# Added -n option (07/2014) # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License @@ -26,7 +29,7 @@ # # # Report bugs to: ck@zet.net, help@monitoring-plugins.org -# +# # 11.01.2000 Version 1.0 # @@ -49,7 +52,7 @@ sub process_arguments (); $ENV{'PATH'}='@TRUSTED_PATH@'; -$ENV{'BASH_ENV'}=''; +$ENV{'BASH_ENV'}=''; $ENV{'ENV'}=''; my $status; @@ -95,8 +98,10 @@ my $opt_h ; my $opt_V ; my $opt_u; +my $opt_n; my $opt_x ; my %excluded ; +my %unused_names ; my @unused_ports ; my %session_opts; @@ -123,7 +128,7 @@ alarm($timeout); ($session, $error) = Net::SNMP->session(%session_opts); - + if (!defined($session)) { $state='UNKNOWN'; $answer=$error; @@ -171,29 +176,32 @@ foreach $key (keys %ifStatus) { # skip unused interfaces - if (!defined($ifStatus{$key}{'notInUse'})) { + my $ifName = $ifStatus{$key}{$snmpIfDescr}; + + if (!defined($ifStatus{$key}{'notInUse'}) || !grep(/^${ifName}/, @unused_ports )) { # check only if interface is administratively up - if ($ifStatus{$key}{$snmpIfAdminStatus} == 1 ) { - - # check only if interface type is not listed in %excluded - if (!defined $excluded{$ifStatus{$key}{$snmpIfType}} ) { - if ($ifStatus{$key}{$snmpIfOperStatus} == 1 ) { $ifup++ ;} - if ($ifStatus{$key}{$snmpIfOperStatus} == 2 ) { - $ifdown++ ; - if (defined $ifXTable) { - $ifmessage .= sprintf("%s: down -> %s
", - $ifStatus{$key}{$snmpIfName}, - $ifStatus{$key}{$snmpIfAlias}); - }else{ - $ifmessage .= sprintf("%s: down
", - $ifStatus{$key}{$snmpIfDescr}); - } + if ($ifStatus{$key}{$snmpIfAdminStatus} == 1 ) { + #check only if interface is not excluded + if (!defined $unused_names{$ifStatus{$key}{$snmpIfDescr}} ) { + # check only if interface type is not listed in %excluded + if (!defined $excluded{$ifStatus{$key}{$snmpIfType}} ) { + if ($ifStatus{$key}{$snmpIfOperStatus} == 1 ) { $ifup++ ; } + if ($ifStatus{$key}{$snmpIfOperStatus} == 2 ) { + $ifdown++ ; + if (defined $ifXTable) { + $ifmessage .= sprintf("%s: down -> %s
\n", $ifStatus{$key}{$snmpIfName}, $ifStatus{$key}{$snmpIfAlias}); + }else{ + $ifmessage .= sprintf("%s: down
\n",$ifStatus{$key}{$snmpIfDescr}); + } + } + if ($ifStatus{$key}{$snmpIfOperStatus} == 5 ) { $ifdormant++ ;} + } else { + $ifexclude++; } - if ($ifStatus{$key}{$snmpIfOperStatus} == 5 ) { $ifdormant++ ;} - }else{ - $ifexclude++; + } else { + $ifunused++; } - + } }else{ $ifunused++; @@ -259,6 +267,8 @@ () printf " the descriptive name. Do not use if you don't know what this is. \n"; printf " -x (--exclude) A comma separated list of ifType values that should be excluded \n"; printf " from the report (default for an empty list is PPP(23).\n"; + printf " -n (--unused_ports_by_name) A comma separated list of ifDescr values that should be excluded \n"; + printf " from the report (default is an empty exclusion list).\n"; printf " -u (--unused_ports) A comma separated list of ifIndex values that should be excluded \n"; printf " from the report (default is an empty exclusion list).\n"; printf " See the IANAifType-MIB for a list of interface types.\n"; @@ -266,11 +276,11 @@ () printf " -U (--secname) username for SNMPv3 context\n"; printf " -c (--context) SNMPv3 context name (default is empty string)\n"; printf " -A (--authpass) authentication password (cleartext ascii or localized key\n"; - printf " in hex with 0x prefix generated by using \"snmpkey\" utility\n"; + printf " in hex with 0x prefix generated by using \"snmpkey\" utility\n"; printf " auth password and authEngineID\n"; printf " -a (--authproto) Authentication protocol (MD5 or SHA1)\n"; printf " -X (--privpass) privacy password (cleartext ascii or localized key\n"; - printf " in hex with 0x prefix generated by using \"snmpkey\" utility\n"; + printf " in hex with 0x prefix generated by using \"snmpkey\" utility\n"; printf " privacy password and authEngineID\n"; printf " -P (--privproto) privacy protocol (DES or AES; default: DES)\n"; printf " -M (--maxmsgsize) Max message size - usefull only for v1 or v2c\n"; @@ -278,7 +288,7 @@ () printf " -V (--version) Plugin version\n"; printf " -h (--help) usage help \n\n"; print_revision($PROGNAME, '@NP_VERSION@'); - + } sub process_arguments() { @@ -299,10 +309,11 @@ () "I" => \$ifXTable, "ifmib" => \$ifXTable, "x:s" => \$opt_x, "exclude:s" => \$opt_x, "u=s" => \$opt_u, "unused_ports=s" => \$opt_u, + "n=s" => \$opt_n, "unused_ports_by_name=s" => \$opt_n, "M=i" => \$maxmsgsize, "maxmsgsize=i" => \$maxmsgsize, "t=i" => \$timeout, "timeout=i" => \$timeout, ); - + if ($status == 0){ print_help(); exit $ERRORS{'OK'}; @@ -345,12 +356,12 @@ () # v3 requires a security username if (defined $seclevel && defined $secname) { $session_opts{'-username'} = $secname; - + # Must define a security level even though defualt is noAuthNoPriv unless ( grep /^$seclevel$/, qw(noAuthNoPriv authNoPriv authPriv) ) { usage("Must define a valid security level even though default is noAuthNoPriv"); } - + # Authentication wanted if ( $seclevel eq 'authNoPriv' || $seclevel eq 'authPriv' ) { if (defined $authproto && $authproto ne 'MD5' && $authproto ne 'SHA1') { @@ -368,7 +379,7 @@ () } } } - + # Privacy (DES encryption) wanted if ($seclevel eq 'authPriv' ) { if (! defined $privpass) { @@ -388,7 +399,7 @@ () unless ( defined $context) { $context = ""; } - + }else { usage("Security level or name is not defined"); } @@ -405,11 +416,21 @@ () $excluded{23} = 1; # default PPP(23) if empty list - note (AIX seems to think PPP is 22 according to a post) } } - + + # Excluded interface descriptors + if (defined $opt_n) { + my @unused = split(/,/,$opt_n); + if ( @unused ) { + foreach $key (@unused) { + $unused_names{$key} = 1; + } + } + } + # Excluded interface ports (ifIndex) - management reasons if ($opt_u) { @unused_ports = split(/,/,$opt_u); - foreach $key (@unused_ports) { + foreach $key (@unused_ports) { $ifStatus{$key}{'notInUse'}++ ; } } @@ -419,7 +440,7 @@ () exit $ERRORS{"UNKNOWN"}; } - + if ($snmp_version !~ /[123]/) { $state='UNKNOWN'; print ("$state: No support for SNMP v$snmp_version yet\n");