Code

Fixed SNMPv3 behaviour of check_ifoperstatus. Added -x to define privprotocol (#23434...
[nagiosplug.git] / plugins-scripts / check_ifoperstatus.pl
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);
111                 
112 if (!defined($session)) {
113                         $state='UNKNOWN';
114                         $answer=$error;
115                         print ("$state: $answer\n");
116                         exit $ERRORS{$state};
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         }
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    }
184    
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.";
227    } elsif ( $response->{$snmpIfOperStatus} == 3 || $response->{$snmpIfOperStatus} == 4  ) {
228            $state = 'CRITICAL';
229            $answer = "Interface $name (index $snmpkey) down (testing/unknown).";
231    } else {
232       $state = 'OK';
233       $answer = "Interface $name (index $snmpkey) is up.";
234    }
238 print ("$state: $answer\n");
239 exit $ERRORS{$state};
242 ### subroutines
244 sub fetch_ifindex {
245         my $oid = shift;
246         my $lookup = shift;
248         if (!defined ($response = $session->get_table($oid))) {
249                 $answer=$session->error;
250                 $session->close;
251                 $state = 'CRITICAL';
252                 printf ("$state: SNMP error with snmp version $snmp_version ($answer)\n");
253                 $session->close;
254                 exit $ERRORS{$state};
255         }
256         
257         foreach $key ( keys %{$response}) {
258                 if ($response->{$key} =~ /^$lookup$/) {
259                         $key =~ /.*\.(\d+)$/;
260                         $snmpkey = $1;
261                         #print "$lookup = $key / $snmpkey \n";  #debug
262                 }
263         }
264         unless (defined $snmpkey) {
265                 $session->close;
266                 $state = 'CRITICAL';
267                 printf "$state: Could not match $ifdescr on $hostname\n";
268                 exit $ERRORS{$state};
269         }
270         
271         return $snmpkey;
274 sub usage() {
275   printf "\nMissing arguments!\n";
276   printf "\n";
277   printf "usage: \n";
278   printf "check_ifoperstatus -k <IF_KEY> -H <HOSTNAME> [-C <community>]\n";
279   printf "Copyright (C) 2000 Christoph Kron\n";
280   printf "check_ifoperstatus.pl comes with ABSOLUTELY NO WARRANTY\n";
281   printf "This programm is licensed under the terms of the ";
282   printf "GNU General Public License\n(check source code for details)\n";
283   printf "\n\n";
284   exit $ERRORS{"UNKNOWN"};
287 sub print_help() {
288         printf "check_ifoperstatus plugin for Nagios monitors operational \n";
289         printf "status of a particular network interface on the target host\n";
290         printf "\nUsage:\n";
291         printf "   -H (--hostname)   Hostname to query - (required)\n";
292         printf "   -C (--community)  SNMP read community (defaults to public,\n";
293         printf "                     used with SNMP v1 and v2c\n";
294         printf "   -v (--snmp_version)  1 for SNMP v1 (default)\n";
295         printf "                        2 for SNMP v2c\n";
296         printf "                        SNMP v2c will use get_bulk for less overhead\n";
297         printf "                        if monitoring with -d\n";
298         printf "   -L (--seclevel)   choice of \"noAuthNoPriv\", \"authNoPriv\", or     \"authPriv\"\n";
299         printf "   -U (--secname)    username for SNMPv3 context\n";
300         printf "   -c (--context)    SNMPv3 context name (default is empty      string)";
301         printf "   -A (--authpass)   authentication password (cleartext ascii or localized key\n";
302         printf "                     in hex with 0x prefix generated by using   \"snmpkey\" utility\n"; 
303         printf "                     auth password and authEngineID\n";
304         printf "   -a (--authproto)  Authentication protocol ( MD5 or SHA1)\n";
305         printf "   -X (--privpass)   privacy password (cleartext ascii or localized key\n";
306         printf "                     in hex with 0x prefix generated by using   \"snmpkey\" utility\n"; 
307         printf "                     privacy password and authEngineID\n";
308         printf "   -x (--privproto)  privacy protocol (DES or AES; default: DES)\n";
309         printf "   -k (--key)        SNMP IfIndex value\n";
310         printf "   -d (--descr)      SNMP ifDescr value\n";
311         printf "   -T (--type)       SNMP ifType integer value (see http://www.iana.org/assignments/ianaiftype-mib)\n";
312         printf "   -p (--port)       SNMP port (default 161)\n";
313         printf "   -I (--ifmib)      Agent supports IFMIB ifXTable.  Do not use if\n";
314         printf "                     you don't know what this is. \n";
315         printf "   -n (--name)       the value should match the returned ifName\n";
316         printf "                     (Implies the use of -I)\n";
317         printf "   -w (--warn =i|w|c) ignore|warn|crit if the interface is dormant (default critical)\n";
318         printf "   -D (--admin-down =i|w|c) same for administratively down interfaces (default warning)\n";
319         printf "   -M (--maxmsgsize) Max message size - usefull only for v1 or v2c\n";
320         printf "   -t (--timeout)    seconds before the plugin times out (default=$TIMEOUT)\n";
321         printf "   -V (--version)    Plugin version\n";
322         printf "   -h (--help)       usage help \n\n";
323         printf " -k or -d or -T must be specified\n\n";
324         printf "Note: either -k or -d or -T must be specified and -d and -T are much more network \n";
325         printf "intensive.  Use it sparingly or not at all.  -n is used to match against\n";
326         printf "a much more descriptive ifName value in the IfXTable to verify that the\n";
327         printf "snmpkey has not changed to some other network interface after a reboot.\n\n";
328         print_revision($PROGNAME, '@NP_VERSION@');
329         
332 sub process_arguments() {
333         $status = GetOptions(
334                         "V"   => \$opt_V, "version"    => \$opt_V,
335                         "h"   => \$opt_h, "help"       => \$opt_h,
336                         "v=i" => \$snmp_version, "snmp_version=i"  => \$snmp_version,
337                         "C=s" => \$community, "community=s" => \$community,
338                         "L=s" => \$seclevel, "seclevel=s" => \$seclevel,
339                         "a=s" => \$authproto, "authproto=s" => \$authproto,
340                         "U=s" => \$secname,   "secname=s"   => \$secname,
341                         "A=s" => \$authpass,  "authpass=s"  => \$authpass,
342                         "X=s" => \$privpass,  "privpass=s"  => \$privpass,
343                         "x=s" => \$privproto,  "privproto=s"  => \$privproto,
344                         "c=s" => \$context,   "context=s"   => \$context,
345                         "k=i" => \$snmpkey, "key=i",\$snmpkey,
346                         "d=s" => \$ifdescr, "descr=s" => \$ifdescr,
347                         "l=s" => \$lastc,  "lastchange=s" => \$lastc,
348                         "p=i" => \$port,  "port=i" =>\$port,
349                         "H=s" => \$hostname, "hostname=s" => \$hostname,
350                         "I"   => \$ifXTable, "ifmib" => \$ifXTable,
351                         "n=s" => \$ifName, "name=s" => \$ifName,
352                         "w=s" => \$dormantWarn, "warn=s" => \$dormantWarn,
353                         "D=s" => \$adminWarn, "admin-down=s" => \$adminWarn,
354                         "M=i" => \$maxmsgsize, "maxmsgsize=i" => \$maxmsgsize,
355                         "t=i" => \$timeout,    "timeout=i" => \$timeout,
356                         "T=i" => \$iftype,    "type=i" => \$iftype,
357                         );
360                                 
361         if ($status == 0){
362                 print_help();
363                 exit $ERRORS{'OK'};
364         }
365   
366         if ($opt_V) {
367                 print_revision($PROGNAME,'@NP_VERSION@');
368                 exit $ERRORS{'OK'};
369         }
371         if ($opt_h) {
372                 print_help();
373                 exit $ERRORS{'OK'};
374         }
376         if (! utils::is_hostname($hostname)){
377                 usage();
378                 exit $ERRORS{"UNKNOWN"};
379         }
382         unless ($snmpkey > 0 || defined $ifdescr || defined $iftype){
383                 printf "Either a valid snmpkey key (-k) or a ifDescr (-d) must be provided)\n";
384                 usage();
385                 exit $ERRORS{"UNKNOWN"};
386         }
389         if (defined $name) {
390                 $ifXTable=1;
391         }       
393         if (defined $dormantWarn) {
394                 unless ($dormantWarn =~ /^(w|c|i)$/ ) {
395                         printf "Dormant alerts must be one of w|c|i \n";
396                         exit $ERRORS{'UNKNOWN'};
397                 }
398         }
399         
400         unless (defined $timeout) {
401                 $timeout = $TIMEOUT;
402         }
404                 
405         if ($snmp_version !~ /[123]/){
406                 $state='UNKNOWN';
407                 print ("$state: No support for SNMP v$snmp_version yet\n");
408                 exit $ERRORS{$state};
409         }
411         %session_opts = (
412                 -hostname  => $hostname,
413                 -port      => $port,
414                 -version        => $snmp_version,
415                 -maxmsgsize => $maxmsgsize
416         );
418         $session_opts{'-community'} = $community if (defined $community && $snmp_version =~ /[12]/);
420         if ($snmp_version =~ /3/ ) {
421                 # Must define a security level even though default is noAuthNoPriv
422                 # v3 requires a security username
423                 if (defined $seclevel  && defined $secname) {
424                         $session_opts{'-username'} = $secname;
425                 
426                         # Must define a security level even though defualt is noAuthNoPriv
427                         unless ( grep /^$seclevel$/, qw(noAuthNoPriv authNoPriv authPriv) ) {
428                                 usage();
429                                 exit $ERRORS{"UNKNOWN"};
430                         }
431                         
432                         # Authentication wanted
433                         if ( $seclevel eq 'authNoPriv' || $seclevel eq 'authPriv' ) {
434                                 unless ( $authproto eq 'MD5' || $authproto eq 'SHA1' ) {
435                                         usage();
436                                         exit $ERRORS{"UNKNOWN"};
437                                 }
438                                 $session_opts{'-authprotocol'} = $authproto if(defined $authproto);
440                                 if ( !defined $authpass) {
441                                         usage();
442                                         exit $ERRORS{"UNKNOWN"};
443                                 }else{
444                                         if ($authpass =~ /^0x/ ) {
445                                                 $session_opts{'-authkey'} = $authpass ;
446                                         }else{
447                                                 $session_opts{'-authpassword'} = $authpass ;
448                                         }
449                                 }
450                         }
451                         
452                         # Privacy (DES encryption) wanted
453                         if ($seclevel eq  'authPriv' ) {
454                                 if (! defined $privpass) {
455                                         usage();
456                                         exit $ERRORS{"UNKNOWN"};
457                                 }else{
458                                         if ($privpass =~ /^0x/){
459                                                 $session_opts{'-privkey'} = $privpass;
460                                         }else{
461                                                 $session_opts{'-privpassword'} = $privpass;
462                                         }
463                                 }
465                                 $session_opts{'-privprotocol'} = $privproto if(defined $privproto);
466                         }
468                         # Context name defined or default
469                         unless ( defined $context) {
470                                 $context = "";
471                         }
472                 
473                 }else {
474                                         usage();
475                                         exit $ERRORS{'UNKNOWN'}; ;
476                 }
477         } # end snmpv3
481 ## End validation