1 #!/usr/local/bin/perl -w
3 # check_ifoperstatus.pl - nagios plugin
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)
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.
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.
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.
25 # Report bugs to: nagiosplug-help@lists.sourceforge.net
27 # 11.01.2000 Version 1.0
30 # Patches from Guy Van Den Bergh to warn on ifadminstatus down interfaces
31 # instead of critical.
33 # Primary MIB reference - RFC 2863
39 use utils
qw($TIMEOUT %ERRORS &print_revision &support);
43 &Getopt::Long::config('bundling');
45 my $PROGNAME = "check_ifoperstatus";
48 sub process_arguments ();
52 my %ifOperStatus = ('1','up',
58 '7','lowerLayerDown'); # down due to the state of lower layer interface(s)
60 my $state = "UNKNOWN";
63 my $community = "public";
64 my $maxmsgsize = 1472 ; # Net::SNMP default is 1472
65 my ($seclevel, $authproto, $secname, $authpass, $privpass, $auth, $priv, $context);
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 $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';
81 my $snmp_version = 1 ;
92 ### Validate Arguments
94 $status = process_arguments();
97 # Just in case of problems, let's not hang Nagios
99 print ("ERROR: No snmp response from $hostname (alarm)\n");
100 exit $ERRORS{"UNKNOWN"};
106 ## map ifdescr to ifindex - should look at being able to cache this value
108 if (defined $ifdescr) {
109 # escape "/" in ifdescr - very common in the Cisco world
110 $ifdescr =~ s/\//\\\//g;
112 $status=fetch_ifdescr(); # if using on device with large number of interfaces
113 # recommend use of SNMP v2 (get-bulk)
116 printf "$state: could not retrive ifdescr snmpkey - $status-$snmpkey\n";
118 exit $ERRORS{$state};
125 $snmpIfAdminStatus = $snmpIfAdminStatus . "." . $snmpkey;
126 $snmpIfOperStatus = $snmpIfOperStatus . "." . $snmpkey;
127 $snmpIfDescr = $snmpIfDescr . "." . $snmpkey;
128 $snmpIfName = $snmpIfName . "." . $snmpkey ;
129 $snmpIfAlias = $snmpIfAlias . "." . $snmpkey ;
131 push(@snmpoids,$snmpIfAdminStatus);
132 push(@snmpoids,$snmpIfOperStatus);
133 push(@snmpoids,$snmpIfDescr);
134 push(@snmpoids,$snmpIfName) if (defined $ifXTable) ;
135 push(@snmpoids,$snmpIfAlias) if (defined $ifXTable) ;
137 if (!defined($response = $session->get_request(@snmpoids))) {
138 $answer=$session->error;
141 print ("$state: SNMP error: $answer\n");
142 exit $ERRORS{$state};
145 $answer = sprintf("host '%s', %s(%s) is %s\n",
147 $response->{$snmpIfDescr},
149 $ifOperStatus{$response->{$snmpIfOperStatus}}
153 ## Check to see if ifName match is requested and it matches - exit if no match
154 ## not the interface we want to monitor
155 if ( defined $name && not ($response->{$snmpIfName} eq $name) ) {
157 $answer = "Interface name ($name) doesn't match snmp value ($response->{$snmpIfName}) (index $snmpkey)";
158 print ("$state: $answer");
159 exit $ERRORS{$state};
162 ## define the interface name
163 if (defined $ifXTable) {
164 $name = $response->{$snmpIfName} ." - " .$response->{$snmpIfAlias} ;
166 $name = $response->{$snmpIfDescr} ;
169 ## if AdminStatus is down - some one made a consious effort to change config
171 if ( not ($response->{$snmpIfAdminStatus} == 1) ) {
172 $answer = "Interface $name (index $snmpkey) is administratively down.";
173 if ( not defined $adminWarn or $adminWarn eq "w" ) {
175 } elsif ( $adminWarn eq "i" ) {
177 } elsif ( $adminWarn eq "c" ) {
179 } else { # If wrong value for -a, say warning
183 ## Check operational status
184 elsif ( $response->{$snmpIfOperStatus} == 2 ) {
186 $answer = "Interface $name (index $snmpkey) is down.";
187 } elsif ( $response->{$snmpIfOperStatus} == 5 ) {
188 if (defined $dormantWarn ) {
189 if ($dormantWarn eq "w") {
191 $answer = "Interface $name (index $snmpkey) is dormant.";
192 }elsif($dormantWarn eq "c") {
194 $answer = "Interface $name (index $snmpkey) is dormant.";
195 }elsif($dormantWarn eq "i") {
197 $answer = "Interface $name (index $snmpkey) is dormant.";
200 # dormant interface - but warning/critical/ignore not requested
202 $answer = "Interface $name (index $snmpkey) is dormant.";
204 } elsif ( $response->{$snmpIfOperStatus} == 6 ) {
206 $answer = "Interface $name (index $snmpkey) notPresent - possible hotswap in progress.";
207 } elsif ( $response->{$snmpIfOperStatus} == 7 ) {
209 $answer = "Interface $name (index $snmpkey) down due to lower layer being down.";
211 } elsif ( $response->{$snmpIfOperStatus} == 3 || $response->{$snmpIfOperStatus} == 4 ) {
213 $answer = "Interface $name (index $snmpkey) down (testing/unknown).";
217 $answer = "Interface $name (index $snmpkey) is up.";
222 print ("$state: $answer");
223 exit $ERRORS{$state};
229 if (!defined ($response = $session->get_table($snmpIfDescr))) {
230 $answer=$session->error;
233 printf ("$state: SNMP error with snmp version $snmp_version ($answer)\n");
235 exit $ERRORS{$state};
238 foreach $key ( keys %{$response}) {
239 if ($response->{$key} =~ /^$ifdescr$/) {
240 $key =~ /.*\.(\d+)$/;
242 #print "$ifdescr = $key / $snmpkey \n"; #debug
245 unless (defined $snmpkey) {
248 printf "$state: Could not match $ifdescr on $hostname\n";
249 exit $ERRORS{$state};
256 printf "\nMissing arguments!\n";
259 printf "check_ifoperstatus -k <IF_KEY> -H <HOSTNAME> [-C <community>]\n";
260 printf "Copyright (C) 2000 Christoph Kron\n";
261 printf "check_ifoperstatus.pl comes with ABSOLUTELY NO WARRANTY\n";
262 printf "This programm is licensed under the terms of the ";
263 printf "GNU General Public License\n(check source code for details)\n";
265 exit $ERRORS{"UNKNOWN"};
269 printf "check_ifoperstatus plugin for Nagios monitors operational \n";
270 printf "status of a particular network interface on the target host\n";
272 printf " -H (--hostname) Hostname to query - (required)\n";
273 printf " -C (--community) SNMP read community (defaults to public,\n";
274 printf " used with SNMP v1 and v2c\n";
275 printf " -v (--snmp_version) 1 for SNMP v1 (default)\n";
276 printf " 2 for SNMP v2c\n";
277 printf " SNMP v2c will use get_bulk for less overhead\n";
278 printf " if monitoring with -d\n";
279 printf " -L (--seclevel) choice of \"noAuthNoPriv\", \"authNoPriv\", or \"authPriv\"\n";
280 printf " -U (--secname) username for SNMPv3 context\n";
281 printf " -c (--context) SNMPv3 context name (default is empty string)";
282 printf " -A (--authpass) authentication password (cleartext ascii or localized key\n";
283 printf " in hex with 0x prefix generated by using \"snmpkey\" utility\n";
284 printf " auth password and authEngineID\n";
285 printf " -a (--authproto) Authentication protocol ( MD5 or SHA1)\n";
286 printf " -X (--privpass) privacy password (cleartext ascii or localized key\n";
287 printf " in hex with 0x prefix generated by using \"snmpkey\" utility\n";
288 printf " privacy password and authEngineID\n";
289 printf " -k (--key) SNMP IfIndex value\n";
290 printf " -d (--descr) SNMP ifDescr value\n";
291 printf " -p (--port) SNMP port (default 161)\n";
292 printf " -I (--ifmib) Agent supports IFMIB ifXTable. Do not use if\n";
293 printf " you don't know what this is. \n";
294 printf " -n (--name) the value should match the returned ifName\n";
295 printf " (Implies the use of -I)\n";
296 printf " -w (--warn =i|w|c) ignore|warn|crit if the interface is dormant (default critical)\n";
297 printf " -D (--admin-down =i|w|c) same for administratively down interfaces (default warning)\n";
298 printf " -M (--maxmsgsize) Max message size - usefull only for v1 or v2c\n";
299 printf " -t (--timeout) seconds before the plugin times out (default=$TIMEOUT)\n";
300 printf " -V (--version) Plugin version\n";
301 printf " -h (--help) usage help \n\n";
302 printf " -k or -d must be specified\n\n";
303 printf "Note: either -k or -d must be specified and -d is much more network \n";
304 printf "intensive. Use it sparingly or not at all. -n is used to match against\n";
305 printf "a much more descriptive ifName value in the IfXTable to verify that the\n";
306 printf "snmpkey has not changed to some other network interface after a reboot.\n\n";
307 print_revision($PROGNAME, '$Revision$');
311 sub process_arguments() {
312 $status = GetOptions(
313 "V" => \$opt_V, "version" => \$opt_V,
314 "h" => \$opt_h, "help" => \$opt_h,
315 "v=i" => \$snmp_version, "snmp_version=i" => \$snmp_version,
316 "C=s" => \$community, "community=s" => \$community,
317 "L=s" => \$seclevel, "seclevel=s" => \$seclevel,
318 "a=s" => \$authproto, "authproto=s" => \$authproto,
319 "U=s" => \$secname, "secname=s" => \$secname,
320 "A=s" => \$authpass, "authpass=s" => \$authpass,
321 "X=s" => \$privpass, "privpass=s" => \$privpass,
322 "c=s" => \$context, "context=s" => \$context,
323 "k=i" => \$snmpkey, "key=i",\$snmpkey,
324 "d=s" => \$ifdescr, "descr=s" => \$ifdescr,
325 "l=s" => \$lastc, "lastchange=s" => \$lastc,
326 "p=i" => \$port, "port=i" =>\$port,
327 "H=s" => \$hostname, "hostname=s" => \$hostname,
328 "I" => \$ifXTable, "ifmib" => \$ifXTable,
329 "n=s" => \$ifName, "name=s" => \$ifName,
330 "w=s" => \$dormantWarn, "warn=s" => \$dormantWarn,
331 "D=s" => \$adminWarn, "admin-down=s" => \$adminWarn,
332 "M=i" => \$maxmsgsize, "maxmsgsize=i" => \$maxmsgsize,
333 "t=i" => \$timeout, "timeout=i" => \$timeout,
344 print_revision($PROGNAME,'$Revision$ ');
353 if (! utils::is_hostname($hostname)){
355 exit $ERRORS{"UNKNOWN"};
359 unless ($snmpkey > 0 || defined $ifdescr){
360 printf "Either a valid snmpkey key (-k) or a ifDescr (-d) must be provided)\n";
362 exit $ERRORS{"UNKNOWN"};
370 if (defined $dormantWarn) {
371 unless ($dormantWarn =~ /^(w|c|i)$/ ) {
372 printf "Dormant alerts must be one of w|c|i \n";
373 exit $ERRORS{'UNKNOWN'};
377 unless (defined $timeout) {
381 if ($snmp_version =~ /3/ ) {
382 # Must define a security level even though default is noAuthNoPriv
383 # v3 requires a security username
384 if (defined $seclevel && defined $secname) {
386 # Must define a security level even though defualt is noAuthNoPriv
387 unless ( grep /^$seclevel$/, qw(noAuthNoPriv authNoPriv authPriv) ) {
389 exit $ERRORS{"UNKNOWN"};
392 # Authentication wanted
393 if ( $seclevel eq 'authNoPriv' || $seclevel eq 'authPriv' ) {
395 unless ( $authproto eq 'MD5' || $authproto eq 'SHA1' ) {
397 exit $ERRORS{"UNKNOWN"};
400 if ( !defined $authpass) {
402 exit $ERRORS{"UNKNOWN"};
404 if ($authpass =~ /^0x/ ) {
405 $auth = "-authkey => $authpass" ;
407 $auth = "-authpassword => $authpass";
413 # Privacy (DES encryption) wanted
414 if ($seclevel eq 'authPriv' ) {
415 if (! defined $privpass) {
417 exit $ERRORS{"UNKNOWN"};
419 if ($privpass =~ /^0x/){
420 $priv = "-privkey => $privpass";
422 $priv = "-privpassword => $privpass";
427 # Context name defined or default
429 unless ( defined $context) {
437 exit $ERRORS{'UNKNOWN'}; ;
442 if ( $snmp_version =~ /[12]/ ) {
443 ($session, $error) = Net
::SNMP
->session(
444 -hostname
=> $hostname,
445 -community
=> $community,
447 -version
=> $snmp_version,
448 -maxmsgsize
=> $maxmsgsize
451 if (!defined($session)) {
454 print ("$state: $answer");
455 exit $ERRORS{$state};
458 }elsif ( $snmp_version =~ /3/ ) {
460 if ($seclevel eq 'noAuthNoPriv') {
461 ($session, $error) = Net
::SNMP
->session(
462 -hostname
=> $hostname,
464 -version
=> $snmp_version,
465 -username
=> $secname,
468 }elsif ( $seclevel eq 'authNoPriv' ) {
469 ($session, $error) = Net
::SNMP
->session(
470 -hostname
=> $hostname,
472 -version
=> $snmp_version,
473 -username
=> $secname,
475 -authprotocol
=> $authproto,
477 }elsif ($seclevel eq 'authPriv' ) {
478 ($session, $error) = Net
::SNMP
->session(
479 -hostname
=> $hostname,
481 -version
=> $snmp_version,
482 -username
=> $secname,
484 -authprotocol
=> $authproto,
490 if (!defined($session)) {
493 print ("$state: $answer");
494 exit $ERRORS{$state};
499 print ("$state: No support for SNMP v$snmp_version yet\n");
500 exit $ERRORS{$state};