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 #
29 # Patches from Guy Van Den Bergh to warn on ifadminstatus down interfaces
30 # instead of critical.
31 #
32 # Primary MIB reference - RFC 2863
35 use POSIX;
36 use strict;
37 use lib utils.pm ;
38 use utils qw($TIMEOUT %ERRORS &print_revision &support);
40 use Net::SNMP;
41 use Getopt::Long;
42 &Getopt::Long::config('bundling');
44 my $PROGNAME = "check_ifoperstatus";
45 sub print_help ();
46 sub usage ();
47 sub process_arguments ();
49 my $timeout;
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, $privproto, $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 $snmpIfType = '1.3.6.1.2.1.2.2.1.3';
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';
76 my $hostname;
77 my $ifName;
78 my $session;
79 my $error;
80 my $response;
81 my $snmp_version = 1 ;
82 my $ifXTable;
83 my $opt_h ;
84 my $opt_V ;
85 my $ifdescr;
86 my $iftype;
87 my $key;
88 my $lastc;
89 my $dormantWarn;
90 my $adminWarn;
91 my $name;
92 my %session_opts;
94 ### Validate Arguments
96 $status = process_arguments();
99 use Data::Dumper;
100 # Just in case of problems, let's not hang Nagios
101 $SIG{'ALRM'} = sub {
102 print ("ERROR: No snmp response from $hostname (alarm)\n");
103 exit $ERRORS{"UNKNOWN"};
104 };
106 alarm($timeout);
108 print Dumper(\%session_opts);
109 ($session, $error) = Net::SNMP->session(%session_opts);
112 if (!defined($session)) {
113 $state='UNKNOWN';
114 $answer=$error;
115 print ("$state: $answer\n");
116 exit $ERRORS{$state};
117 }
119 ## map ifdescr to ifindex - should look at being able to cache this value
121 if (defined $ifdescr || defined $iftype) {
122 # escape "/" in ifdescr - very common in the Cisco world
123 if (defined $iftype) {
124 $status=fetch_ifindex($snmpIfType, $iftype);
125 } else {
126 $ifdescr =~ s/\//\\\//g;
127 $status=fetch_ifindex($snmpIfDescr, $ifdescr); # if using on device with large number of interfaces
128 # recommend use of SNMP v2 (get-bulk)
129 }
130 if ($status==0) {
131 $state = "UNKNOWN";
132 printf "$state: could not retrive ifdescr/iftype snmpkey - $status-$snmpkey\n";
133 $session->close;
134 exit $ERRORS{$state};
135 }
136 }
139 ## Main function
141 $snmpIfAdminStatus = $snmpIfAdminStatus . "." . $snmpkey;
142 $snmpIfOperStatus = $snmpIfOperStatus . "." . $snmpkey;
143 $snmpIfDescr = $snmpIfDescr . "." . $snmpkey;
144 $snmpIfName = $snmpIfName . "." . $snmpkey ;
145 $snmpIfAlias = $snmpIfAlias . "." . $snmpkey ;
147 push(@snmpoids,$snmpIfAdminStatus);
148 push(@snmpoids,$snmpIfOperStatus);
149 push(@snmpoids,$snmpIfDescr);
150 push(@snmpoids,$snmpIfName) if (defined $ifXTable) ;
151 push(@snmpoids,$snmpIfAlias) if (defined $ifXTable) ;
153 if (!defined($response = $session->get_request(@snmpoids))) {
154 $answer=$session->error;
155 $session->close;
156 $state = 'WARNING';
157 print ("$state: SNMP error: $answer\n");
158 exit $ERRORS{$state};
159 }
161 $answer = sprintf("host '%s', %s(%s) is %s\n",
162 $hostname,
163 $response->{$snmpIfDescr},
164 $snmpkey,
165 $ifOperStatus{$response->{$snmpIfOperStatus}}
166 );
169 ## Check to see if ifName match is requested and it matches - exit if no match
170 ## not the interface we want to monitor
171 if ( defined $ifName && not ($response->{$snmpIfName} eq $ifName) ) {
172 $state = 'UNKNOWN';
173 $answer = "Interface name ($ifName) doesn't match snmp value ($response->{$snmpIfName}) (index $snmpkey)";
174 print ("$state: $answer\n");
175 exit $ERRORS{$state};
176 }
178 ## define the interface name
179 if (defined $ifXTable) {
180 $name = $response->{$snmpIfName} ." - " .$response->{$snmpIfAlias} ;
181 }else{
182 $name = $response->{$snmpIfDescr} ;
183 }
185 ## if AdminStatus is down - some one made a consious effort to change config
186 ##
187 if ( not ($response->{$snmpIfAdminStatus} == 1) ) {
188 $answer = "Interface $name (index $snmpkey) is administratively down.";
189 if ( not defined $adminWarn or $adminWarn eq "w" ) {
190 $state = 'WARNING';
191 } elsif ( $adminWarn eq "i" ) {
192 $state = 'OK';
193 } elsif ( $adminWarn eq "c" ) {
194 $state = 'CRITICAL';
195 } else { # If wrong value for -a, say warning
196 $state = 'WARNING';
197 }
198 }
199 ## Check operational status
200 elsif ( $response->{$snmpIfOperStatus} == 2 ) {
201 $state = 'CRITICAL';
202 $answer = "Interface $name (index $snmpkey) is down.";
203 } elsif ( $response->{$snmpIfOperStatus} == 5 ) {
204 if (defined $dormantWarn ) {
205 if ($dormantWarn eq "w") {
206 $state = 'WARNING';
207 $answer = "Interface $name (index $snmpkey) is dormant.";
208 }elsif($dormantWarn eq "c") {
209 $state = 'CRITICAL';
210 $answer = "Interface $name (index $snmpkey) is dormant.";
211 }elsif($dormantWarn eq "i") {
212 $state = 'OK';
213 $answer = "Interface $name (index $snmpkey) is dormant.";
214 }
215 }else{
216 # dormant interface - but warning/critical/ignore not requested
217 $state = 'CRITICAL';
218 $answer = "Interface $name (index $snmpkey) is dormant.";
219 }
220 } elsif ( $response->{$snmpIfOperStatus} == 6 ) {
221 $state = 'CRITICAL';
222 $answer = "Interface $name (index $snmpkey) notPresent - possible hotswap in progress.";
223 } elsif ( $response->{$snmpIfOperStatus} == 7 ) {
224 $state = 'CRITICAL';
225 $answer = "Interface $name (index $snmpkey) down due to lower layer being down.";
226 } elsif ( $response->{$snmpIfOperStatus} == 3 || $response->{$snmpIfOperStatus} == 4 ) {
227 $state = 'CRITICAL';
228 $answer = "Interface $name (index $snmpkey) down (testing/unknown).";
229 } else {
230 $state = 'OK';
231 $answer = "Interface $name (index $snmpkey) is up.";
232 }
236 print ("$state: $answer\n");
237 exit $ERRORS{$state};
240 ### subroutines
242 sub fetch_ifindex {
243 my $oid = shift;
244 my $lookup = shift;
246 if (!defined ($response = $session->get_table($oid))) {
247 $answer=$session->error;
248 $session->close;
249 $state = 'CRITICAL';
250 printf ("$state: SNMP error with snmp version $snmp_version ($answer)\n");
251 $session->close;
252 exit $ERRORS{$state};
253 }
255 foreach $key ( keys %{$response}) {
256 if ($response->{$key} =~ /^$lookup$/) {
257 $key =~ /.*\.(\d+)$/;
258 $snmpkey = $1;
259 #print "$lookup = $key / $snmpkey \n"; #debug
260 }
261 }
262 unless (defined $snmpkey) {
263 $session->close;
264 $state = 'CRITICAL';
265 printf "$state: Could not match $ifdescr on $hostname\n";
266 exit $ERRORS{$state};
267 }
269 return $snmpkey;
270 }
272 sub usage() {
273 printf "\nMissing arguments!\n";
274 printf "\n";
275 printf "usage: \n";
276 printf "check_ifoperstatus -k <IF_KEY> -H <HOSTNAME> [-C <community>]\n";
277 printf "Copyright (C) 2000 Christoph Kron\n";
278 printf "check_ifoperstatus.pl comes with ABSOLUTELY NO WARRANTY\n";
279 printf "This programm is licensed under the terms of the ";
280 printf "GNU General Public License\n(check source code for details)\n";
281 printf "\n\n";
282 exit $ERRORS{"UNKNOWN"};
283 }
285 sub print_help() {
286 printf "check_ifoperstatus plugin for Nagios monitors operational \n";
287 printf "status of a particular network interface on the target host\n";
288 printf "\nUsage:\n";
289 printf " -H (--hostname) Hostname to query - (required)\n";
290 printf " -C (--community) SNMP read community (defaults to public,\n";
291 printf " used with SNMP v1 and v2c\n";
292 printf " -v (--snmp_version) 1 for SNMP v1 (default)\n";
293 printf " 2 for SNMP v2c\n";
294 printf " SNMP v2c will use get_bulk for less overhead\n";
295 printf " if monitoring with -d\n";
296 printf " -L (--seclevel) choice of \"noAuthNoPriv\", \"authNoPriv\", or \"authPriv\"\n";
297 printf " -U (--secname) username for SNMPv3 context\n";
298 printf " -c (--context) SNMPv3 context name (default is empty string)\n";
299 printf " -A (--authpass) authentication password (cleartext ascii or localized key\n";
300 printf " in hex with 0x prefix generated by using \"snmpkey\" utility\n";
301 printf " auth password and authEngineID\n";
302 printf " -a (--authproto) Authentication protocol (MD5 or SHA1)\n";
303 printf " -X (--privpass) privacy password (cleartext ascii or localized key\n";
304 printf " in hex with 0x prefix generated by using \"snmpkey\" utility\n";
305 printf " privacy password and authEngineID\n";
306 printf " -x (--privproto) privacy protocol (DES or AES; default: DES)\n";
307 printf " -k (--key) SNMP IfIndex value\n";
308 printf " -d (--descr) SNMP ifDescr value\n";
309 printf " -T (--type) SNMP ifType integer value (see http://www.iana.org/assignments/ianaiftype-mib)\n";
310 printf " -p (--port) SNMP port (default 161)\n";
311 printf " -I (--ifmib) Agent supports IFMIB ifXTable. Do not use if\n";
312 printf " you don't know what this is. \n";
313 printf " -n (--name) the value should match the returned ifName\n";
314 printf " (Implies the use of -I)\n";
315 printf " -w (--warn =i|w|c) ignore|warn|crit if the interface is dormant (default critical)\n";
316 printf " -D (--admin-down =i|w|c) same for administratively down interfaces (default warning)\n";
317 printf " -M (--maxmsgsize) Max message size - usefull only for v1 or v2c\n";
318 printf " -t (--timeout) seconds before the plugin times out (default=$TIMEOUT)\n";
319 printf " -V (--version) Plugin version\n";
320 printf " -h (--help) usage help \n\n";
321 printf " -k or -d or -T must be specified\n\n";
322 printf "Note: either -k or -d or -T must be specified and -d and -T are much more network \n";
323 printf "intensive. Use it sparingly or not at all. -n is used to match against\n";
324 printf "a much more descriptive ifName value in the IfXTable to verify that the\n";
325 printf "snmpkey has not changed to some other network interface after a reboot.\n\n";
326 print_revision($PROGNAME, '@NP_VERSION@');
328 }
330 sub process_arguments() {
331 $status = GetOptions(
332 "V" => \$opt_V, "version" => \$opt_V,
333 "h" => \$opt_h, "help" => \$opt_h,
334 "v=i" => \$snmp_version, "snmp_version=i" => \$snmp_version,
335 "C=s" => \$community, "community=s" => \$community,
336 "L=s" => \$seclevel, "seclevel=s" => \$seclevel,
337 "a=s" => \$authproto, "authproto=s" => \$authproto,
338 "U=s" => \$secname, "secname=s" => \$secname,
339 "A=s" => \$authpass, "authpass=s" => \$authpass,
340 "X=s" => \$privpass, "privpass=s" => \$privpass,
341 "x=s" => \$privproto, "privproto=s" => \$privproto,
342 "c=s" => \$context, "context=s" => \$context,
343 "k=i" => \$snmpkey, "key=i",\$snmpkey,
344 "d=s" => \$ifdescr, "descr=s" => \$ifdescr,
345 "l=s" => \$lastc, "lastchange=s" => \$lastc,
346 "p=i" => \$port, "port=i" =>\$port,
347 "H=s" => \$hostname, "hostname=s" => \$hostname,
348 "I" => \$ifXTable, "ifmib" => \$ifXTable,
349 "n=s" => \$ifName, "name=s" => \$ifName,
350 "w=s" => \$dormantWarn, "warn=s" => \$dormantWarn,
351 "D=s" => \$adminWarn, "admin-down=s" => \$adminWarn,
352 "M=i" => \$maxmsgsize, "maxmsgsize=i" => \$maxmsgsize,
353 "t=i" => \$timeout, "timeout=i" => \$timeout,
354 "T=i" => \$iftype, "type=i" => \$iftype,
355 );
358 if ($status == 0){
359 print_help();
360 exit $ERRORS{'OK'};
361 }
363 if ($opt_V) {
364 print_revision($PROGNAME,'@NP_VERSION@');
365 exit $ERRORS{'OK'};
366 }
368 if ($opt_h) {
369 print_help();
370 exit $ERRORS{'OK'};
371 }
373 if (! utils::is_hostname($hostname)){
374 usage();
375 exit $ERRORS{"UNKNOWN"};
376 }
378 unless ($snmpkey > 0 || defined $ifdescr || defined $iftype){
379 printf "Either a valid snmpkey key (-k) or a ifDescr (-d) must be provided)\n";
380 usage();
381 exit $ERRORS{"UNKNOWN"};
382 }
384 if (defined $name) {
385 $ifXTable=1;
386 }
388 if (defined $dormantWarn) {
389 unless ($dormantWarn =~ /^(w|c|i)$/ ) {
390 printf "Dormant alerts must be one of w|c|i \n";
391 exit $ERRORS{'UNKNOWN'};
392 }
393 }
395 unless (defined $timeout) {
396 $timeout = $TIMEOUT;
397 }
399 if ($snmp_version !~ /[123]/){
400 $state='UNKNOWN';
401 print ("$state: No support for SNMP v$snmp_version yet\n");
402 exit $ERRORS{$state};
403 }
405 %session_opts = (
406 -hostname => $hostname,
407 -port => $port,
408 -version => $snmp_version,
409 -maxmsgsize => $maxmsgsize
410 );
412 $session_opts{'-community'} = $community if (defined $community && $snmp_version =~ /[12]/);
414 if ($snmp_version =~ /3/ ) {
415 # Must define a security level even though default is noAuthNoPriv
416 # v3 requires a security username
417 if (defined $seclevel && defined $secname) {
418 $session_opts{'-username'} = $secname;
420 # Must define a security level even though defualt is noAuthNoPriv
421 unless ( grep /^$seclevel$/, qw(noAuthNoPriv authNoPriv authPriv) ) {
422 usage();
423 exit $ERRORS{"UNKNOWN"};
424 }
426 # Authentication wanted
427 if ( $seclevel eq 'authNoPriv' || $seclevel eq 'authPriv' ) {
428 unless ( $authproto eq 'MD5' || $authproto eq 'SHA1' ) {
429 usage();
430 exit $ERRORS{"UNKNOWN"};
431 }
432 $session_opts{'-authprotocol'} = $authproto if(defined $authproto);
434 if ( !defined $authpass) {
435 usage();
436 exit $ERRORS{"UNKNOWN"};
437 }else{
438 if ($authpass =~ /^0x/ ) {
439 $session_opts{'-authkey'} = $authpass ;
440 }else{
441 $session_opts{'-authpassword'} = $authpass ;
442 }
443 }
444 }
446 # Privacy (DES encryption) wanted
447 if ($seclevel eq 'authPriv' ) {
448 if (! defined $privpass) {
449 usage();
450 exit $ERRORS{"UNKNOWN"};
451 }else{
452 if ($privpass =~ /^0x/){
453 $session_opts{'-privkey'} = $privpass;
454 }else{
455 $session_opts{'-privpassword'} = $privpass;
456 }
457 }
459 $session_opts{'-privprotocol'} = $privproto if(defined $privproto);
460 }
462 # Context name defined or default
463 unless ( defined $context) {
464 $context = "";
465 }
467 }else {
468 usage();
469 exit $ERRORS{'UNKNOWN'}; ;
470 }
471 } # end snmpv3
474 }
475 ## End validation