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
29 # Patches from Guy Van Den Bergh to warn on ifadminstatus down interfaces
30 # instead of critical.
32 # Primary MIB reference - RFC 2863
38 use utils
qw($TIMEOUT %ERRORS &print_revision &support);
42 &Getopt::Long::config('bundling');
44 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, $privproto, $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 $snmpIfType = '1.3.6.1.2.1.2.2.1.3';
71 my $snmpIfAdminStatus = '1.3.6.1.2.1.2.2.1.7';
72 my $snmpIfOperStatus = '1.3.6.1.2.1.2.2.1.8';
73 my $snmpIfName = '1.3.6.1.2.1.31.1.1.1.1';
74 my $snmpIfLastChange = '1.3.6.1.2.1.2.2.1.9';
75 my $snmpIfAlias = '1.3.6.1.2.1.31.1.1.1.18';
76 my $snmpLocIfDescr = '1.3.6.1.4.1.9.2.2.1.1.28';
82 my $snmp_version = 1 ;
95 ### Validate Arguments
97 $status = process_arguments();
100 # Just in case of problems, let's not hang Nagios
102 print ("ERROR: No snmp response from $hostname (alarm)\n");
103 exit $ERRORS{"UNKNOWN"};
108 ($session, $error) = Net::SNMP->session(%session_opts);
111 if (!defined($session)) {
114 print ("$state: $answer\n");
115 exit $ERRORS{$state};
118 ## map ifdescr to ifindex - should look at being able to cache this value
120 if (defined $ifdescr || defined $iftype) {
121 # escape "/" in ifdescr - very common in the Cisco world
122 if (defined $iftype) {
123 $status=fetch_ifindex($snmpIfType, $iftype);
125 $ifdescr =~ s/\//\\\//g;
126 $status=fetch_ifindex($snmpIfDescr, $ifdescr); # if using on device with large number of interfaces
127 # recommend use of SNMP v2 (get-bulk)
131 printf "$state: could not retrive ifdescr/iftype snmpkey - $status-$snmpkey\n";
133 exit $ERRORS{$state};
140 $snmpIfAdminStatus = $snmpIfAdminStatus . "." . $snmpkey;
141 $snmpIfOperStatus = $snmpIfOperStatus . "." . $snmpkey;
142 $snmpIfDescr = $snmpIfDescr . "." . $snmpkey;
143 $snmpIfName = $snmpIfName . "." . $snmpkey ;
144 $snmpIfAlias = $snmpIfAlias . "." . $snmpkey ;
146 push(@snmpoids,$snmpIfAdminStatus);
147 push(@snmpoids,$snmpIfOperStatus);
148 push(@snmpoids,$snmpIfDescr);
149 push(@snmpoids,$snmpIfName) if (defined $ifXTable) ;
150 push(@snmpoids,$snmpIfAlias) if (defined $ifXTable) ;
152 if (!defined($response = $session->get_request(@snmpoids))) {
153 $answer=$session->error;
156 print ("$state: SNMP error: $answer\n");
157 exit $ERRORS{$state};
160 $answer = sprintf("host '%s', %s(%s) is %s\n",
162 $response->{$snmpIfDescr},
164 $ifOperStatus{$response->{$snmpIfOperStatus}}
168 ## Check to see if ifName match is requested and it matches - exit if no match
169 ## not the interface we want to monitor
170 if ( defined $ifName && not ($response->{$snmpIfName} eq $ifName) ) {
172 $answer = "Interface name ($ifName) doesn't match snmp value ($response->{$snmpIfName}) (index $snmpkey)";
173 print ("$state: $answer\n");
174 exit $ERRORS{$state};
177 ## define the interface name
178 if (defined $ifXTable) {
179 $name = $response->{$snmpIfName} ." - " .$response->{$snmpIfAlias} ;
181 $name = $response->{$snmpIfDescr} ;
184 ## if AdminStatus is down - some one made a consious effort to change config
186 if ( not ($response->{$snmpIfAdminStatus} == 1) ) {
187 $answer = "Interface $name (index $snmpkey) is administratively down.";
188 if ( not defined $adminWarn or $adminWarn eq "w" ) {
190 } elsif ( $adminWarn eq "i" ) {
192 } elsif ( $adminWarn eq "c" ) {
194 } else { # If wrong value for -a, say warning
198 ## Check operational status
199 elsif ( $response->{$snmpIfOperStatus} == 2 ) {
201 $answer = "Interface $name (index $snmpkey) is down.";
202 } elsif ( $response->{$snmpIfOperStatus} == 5 ) {
203 if (defined $dormantWarn ) {
204 if ($dormantWarn eq "w") {
206 $answer = "Interface $name (index $snmpkey) is dormant.";
207 }elsif($dormantWarn eq "c") {
209 $answer = "Interface $name (index $snmpkey) is dormant.";
210 }elsif($dormantWarn eq "i") {
212 $answer = "Interface $name (index $snmpkey) is dormant.";
215 # dormant interface - but warning/critical/ignore not requested
217 $answer = "Interface $name (index $snmpkey) is dormant.";
219 } elsif ( $response->{$snmpIfOperStatus} == 6 ) {
221 $answer = "Interface $name (index $snmpkey) notPresent - possible hotswap in progress.";
222 } elsif ( $response->{$snmpIfOperStatus} == 7 ) {
224 $answer = "Interface $name (index $snmpkey) down due to lower layer being down.";
225 } elsif ( $response->{$snmpIfOperStatus} == 3 || $response->{$snmpIfOperStatus} == 4 ) {
227 $answer = "Interface $name (index $snmpkey) down (testing/unknown).";
230 $answer = "Interface $name (index $snmpkey) is up.";
235 print ("$state: $answer\n");
236 exit $ERRORS{$state};
245 if (!defined ($response = $session->get_table($oid))) {
246 $answer=$session->error;
249 printf ("$state: SNMP error with snmp version $snmp_version ($answer)\n");
251 exit $ERRORS{$state};
254 foreach $key ( keys %{$response}) {
255 if ($response->{$key} =~ /^$lookup$/) {
256 $key =~ /.*\.(\d+)$/;
258 #print "$lookup = $key / $snmpkey \n"; #debug
261 unless (defined $snmpkey) {
264 printf "$state: Could not match $ifdescr on $hostname\n";
265 exit $ERRORS{$state};
274 exit $ERRORS{"UNKNOWN"};
280 printf "check_ifoperstatus -k <IF_KEY> -H <HOSTNAME> [-C <community>]\n";
281 printf "Copyright (C) 2000 Christoph Kron\n";
282 printf "check_ifoperstatus.pl comes with ABSOLUTELY NO WARRANTY\n";
283 printf "This programm is licensed under the terms of the ";
284 printf "GNU General Public License\n(check source code for details)\n";
289 print_revision($PROGNAME, '@NP_VERSION@');
291 printf "check_ifoperstatus plugin for Nagios monitors operational \n";
292 printf "status of a particular network interface on the target host\n";
294 printf " -H (--hostname) Hostname to query - (required)\n";
295 printf " -C (--community) SNMP read community (defaults to public,\n";
296 printf " used with SNMP v1 and v2c\n";
297 printf " -v (--snmp_version) 1 for SNMP v1 (default)\n";
298 printf " 2 for SNMP v2c\n";
299 printf " SNMP v2c will use get_bulk for less overhead\n";
300 printf " if monitoring with -d\n";
301 printf " -L (--seclevel) choice of \"noAuthNoPriv\", \"authNoPriv\", or \"authPriv\"\n";
302 printf " -U (--secname) username for SNMPv3 context\n";
303 printf " -c (--context) SNMPv3 context name (default is empty string)\n";
304 printf " -A (--authpass) authentication password (cleartext ascii or localized key\n";
305 printf " in hex with 0x prefix generated by using \"snmpkey\" utility\n";
306 printf " auth password and authEngineID\n";
307 printf " -a (--authproto) Authentication protocol (MD5 or SHA1)\n";
308 printf " -X (--privpass) privacy password (cleartext ascii or localized key\n";
309 printf " in hex with 0x prefix generated by using \"snmpkey\" utility\n";
310 printf " privacy password and authEngineID\n";
311 printf " -P (--privproto) privacy protocol (DES or AES; default: DES)\n";
312 printf " -k (--key) SNMP IfIndex value\n";
313 printf " -d (--descr) SNMP ifDescr value\n";
314 printf " -T (--type) SNMP ifType integer value (see http://www.iana.org/assignments/ianaiftype-mib)\n";
315 printf " -p (--port) SNMP port (default 161)\n";
316 printf " -I (--ifmib) Agent supports IFMIB ifXTable. Do not use if\n";
317 printf " you don't know what this is. \n";
318 printf " -n (--name) the value should match the returned ifName\n";
319 printf " (Implies the use of -I)\n";
320 printf " -w (--warn =i|w|c) ignore|warn|crit if the interface is dormant (default critical)\n";
321 printf " -D (--admin-down =i|w|c) same for administratively down interfaces (default warning)\n";
322 printf " -M (--maxmsgsize) Max message size - usefull only for v1 or v2c\n";
323 printf " -t (--timeout) seconds before the plugin times out (default=$TIMEOUT)\n";
324 printf " -V (--version) Plugin version\n";
325 printf " -h (--help) usage help \n\n";
326 printf " -k or -d or -T must be specified\n\n";
327 printf "Note: either -k or -d or -T must be specified and -d and -T are much more network \n";
328 printf "intensive. Use it sparingly or not at all. -n is used to match against\n";
329 printf "a much more descriptive ifName value in the IfXTable to verify that the\n";
330 printf "snmpkey has not changed to some other network interface after a reboot.\n\n";
334 sub process_arguments() {
335 $status = GetOptions(
336 "V" => \$opt_V, "version" => \$opt_V,
337 "h" => \$opt_h, "help" => \$opt_h,
338 "v=i" => \$snmp_version, "snmp_version=i" => \$snmp_version,
339 "C=s" => \$community, "community=s" => \$community,
340 "L=s" => \$seclevel, "seclevel=s" => \$seclevel,
341 "a=s" => \$authproto, "authproto=s" => \$authproto,
342 "U=s" => \$secname, "secname=s" => \$secname,
343 "A=s" => \$authpass, "authpass=s" => \$authpass,
344 "X=s" => \$privpass, "privpass=s" => \$privpass,
345 "P=s" => \$privproto, "privproto=s" => \$privproto,
346 "c=s" => \$context, "context=s" => \$context,
347 "k=i" => \$snmpkey, "key=i",\$snmpkey,
348 "d=s" => \$ifdescr, "descr=s" => \$ifdescr,
349 "l=s" => \$lastc, "lastchange=s" => \$lastc,
350 "p=i" => \$port, "port=i" =>\$port,
351 "H=s" => \$hostname, "hostname=s" => \$hostname,
352 "I" => \$ifXTable, "ifmib" => \$ifXTable,
353 "n=s" => \$ifName, "name=s" => \$ifName,
354 "w=s" => \$dormantWarn, "warn=s" => \$dormantWarn,
355 "D=s" => \$adminWarn, "admin-down=s" => \$adminWarn,
356 "M=i" => \$maxmsgsize, "maxmsgsize=i" => \$maxmsgsize,
357 "t=i" => \$timeout, "timeout=i" => \$timeout,
358 "T=i" => \$iftype, "type=i" => \$iftype,
368 print_revision($PROGNAME,'@NP_VERSION@');
377 if (! utils::is_hostname($hostname)){
378 usage("Hostname invalid or not given");
381 unless ($snmpkey > 0 || defined $ifdescr || defined $iftype){
382 usage("Either a valid snmp key (-k) or a ifDescr (-d) must be provided");
385 if (defined $ifName) {
389 if (defined $dormantWarn) {
390 unless ($dormantWarn =~ /^(w|c|i)$/ ) {
391 printf "Dormant alerts must be one of w|c|i \n";
392 exit $ERRORS{'UNKNOWN'};
396 unless (defined $timeout) {
400 if ($snmp_version !~ /[123]/){
402 print ("$state: No support for SNMP v$snmp_version yet\n");
403 exit $ERRORS{$state};
407 -hostname => $hostname,
409 -version => $snmp_version,
410 -maxmsgsize => $maxmsgsize
413 $session_opts{'-community'} = $community if (defined $community && $snmp_version =~ /[12]/);
415 if ($snmp_version =~ /3/ ) {
416 # Must define a security level even though default is noAuthNoPriv
417 # v3 requires a security username
418 if (defined $seclevel && defined $secname) {
419 $session_opts{'-username'} = $secname;
421 # Must define a security level even though defualt is noAuthNoPriv
422 unless ( grep /^$seclevel$/, qw(noAuthNoPriv authNoPriv authPriv) ) {
423 usage
("Must define a valid security level even though default is noAuthNoPriv");
426 # Authentication wanted
427 if ( $seclevel eq 'authNoPriv' || $seclevel eq 'authPriv' ) {
428 if (defined $authproto && $authproto ne 'MD5' && $authproto ne 'SHA1') {
429 usage
("Auth protocol can be either MD5 or SHA1");
431 $session_opts{'-authprotocol'} = $authproto if(defined $authproto);
433 if ( !defined $authpass) {
434 usage
("Auth password/key is not defined");
436 if ($authpass =~ /^0x/ ) {
437 $session_opts{'-authkey'} = $authpass ;
439 $session_opts{'-authpassword'} = $authpass ;
444 # Privacy (DES encryption) wanted
445 if ($seclevel eq 'authPriv' ) {
446 if (! defined $privpass) {
447 usage
("Privacy passphrase/key is not defined");
449 if ($privpass =~ /^0x/){
450 $session_opts{'-privkey'} = $privpass;
452 $session_opts{'-privpassword'} = $privpass;
456 $session_opts{'-privprotocol'} = $privproto if(defined $privproto);
459 # Context name defined or default
460 unless ( defined $context) {
465 usage
("Security level or name is not defined");