Code

check_host: Allocate a large-enough buffer for the host table.
[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 print_usage ();
48 sub process_arguments ();
50 my $timeout;
51 my $status;
52 my %ifOperStatus =      ('1','up',
53                      '2','down',
54                      '3','testing',
55                      '4','unknown',
56                      '5','dormant',
57                      '6','notPresent',
58                      '7','lowerLayerDown');  # down due to the state of lower layer interface(s)
60 my $state = "UNKNOWN";
61 my $answer = "";
62 my $snmpkey = 0;
63 my $community = "public";
64 my $maxmsgsize = 1472 ; # Net::SNMP default is 1472
65 my ($seclevel, $authproto, $secname, $authpass, $privpass, $privproto, $auth, $priv, $context);
66 my $port = 161;
67 my @snmpoids;
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';
77 my $hostname;
78 my $ifName;
79 my $session;
80 my $error;
81 my $response;
82 my $snmp_version = 1 ;
83 my $ifXTable;
84 my $opt_h ;
85 my $opt_V ;
86 my $ifdescr;
87 my $iftype;
88 my $key;
89 my $lastc;
90 my $dormantWarn;
91 my $adminWarn;
92 my $name;
93 my %session_opts;
95 ### Validate Arguments
97 $status = process_arguments();
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 ($session, $error) = Net::SNMP->session(%session_opts);
110                 
111 if (!defined($session)) {
112                         $state='UNKNOWN';
113                         $answer=$error;
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);
124         } else {
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)
128         }
129         if ($status==0) {
130                 $state = "UNKNOWN";
131                 printf "$state: could not retrive ifdescr/iftype snmpkey - $status-$snmpkey\n";
132                 $session->close;
133                 exit $ERRORS{$state};
134         }
138 ## Main function
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;
154         $session->close;
155         $state = 'WARNING';
156         print ("$state: SNMP error: $answer\n");
157         exit $ERRORS{$state};
160 $answer = sprintf("host '%s', %s(%s) is %s\n", 
161         $hostname, 
162         $response->{$snmpIfDescr},
163         $snmpkey, 
164         $ifOperStatus{$response->{$snmpIfOperStatus}}
165 );
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) ) {
171         $state = 'UNKNOWN';
172         $answer = "Interface name ($ifName) doesn't match snmp value ($response->{$snmpIfName}) (index $snmpkey)";
173         print ("$state: $answer\n");
174         exit $ERRORS{$state};
175
177 ## define the interface name
178 if (defined $ifXTable) {
179          $name = $response->{$snmpIfName} ." - " .$response->{$snmpIfAlias} ; 
180 }else{
181          $name = $response->{$snmpIfDescr} ;
184 ## if AdminStatus is down - some one made a consious effort to change config
185 ##
186 if ( not ($response->{$snmpIfAdminStatus} == 1) ) {
187         $answer = "Interface $name (index $snmpkey) is administratively down.";
188         if ( not defined $adminWarn or $adminWarn eq "w" ) {
189                 $state = 'WARNING';
190         } elsif ( $adminWarn eq "i" ) {
191                 $state = 'OK';
192         } elsif ( $adminWarn eq "c" ) {
193                 $state = 'CRITICAL';
194         } else { # If wrong value for -a, say warning
195                 $state = 'WARNING';
196         }
197
198 ## Check operational status
199 elsif ( $response->{$snmpIfOperStatus} == 2 ) {
200         $state = 'CRITICAL';
201         $answer = "Interface $name (index $snmpkey) is down.";
202 } elsif ( $response->{$snmpIfOperStatus} == 5 ) {
203         if (defined $dormantWarn ) {
204                 if ($dormantWarn eq "w") {
205                         $state = 'WARNING';
206                         $answer = "Interface $name (index $snmpkey) is dormant.";
207                 }elsif($dormantWarn eq "c") {
208                         $state = 'CRITICAL';
209                         $answer = "Interface $name (index $snmpkey) is dormant.";
210                 }elsif($dormantWarn eq "i") {
211                         $state = 'OK';
212                         $answer = "Interface $name (index $snmpkey) is dormant.";
213                 }
214         }else{
215                 # dormant interface - but warning/critical/ignore not requested
216                 $state = 'CRITICAL';
217                 $answer = "Interface $name (index $snmpkey) is dormant.";
218         }
219 } elsif ( $response->{$snmpIfOperStatus} == 6 ) {
220         $state = 'CRITICAL';
221         $answer = "Interface $name (index $snmpkey) notPresent - possible hotswap in progress.";
222 } elsif ( $response->{$snmpIfOperStatus} == 7 ) {
223         $state = 'CRITICAL';
224         $answer = "Interface $name (index $snmpkey) down due to lower layer being down.";
225 } elsif ( $response->{$snmpIfOperStatus} == 3 || $response->{$snmpIfOperStatus} == 4  ) {
226         $state = 'CRITICAL';
227         $answer = "Interface $name (index $snmpkey) down (testing/unknown).";
228 } else {
229         $state = 'OK';
230         $answer = "Interface $name (index $snmpkey) is up.";
235 print ("$state: $answer\n");
236 exit $ERRORS{$state};
239 ### subroutines
241 sub fetch_ifindex {
242         my $oid = shift;
243         my $lookup = shift;
245         if (!defined ($response = $session->get_table($oid))) {
246                 $answer=$session->error;
247                 $session->close;
248                 $state = 'CRITICAL';
249                 printf ("$state: SNMP error with snmp version $snmp_version ($answer)\n");
250                 $session->close;
251                 exit $ERRORS{$state};
252         }
253         
254         foreach $key ( keys %{$response}) {
255                 if ($response->{$key} =~ /^$lookup$/) {
256                         $key =~ /.*\.(\d+)$/;
257                         $snmpkey = $1;
258                         #print "$lookup = $key / $snmpkey \n";  #debug
259                 }
260         }
261         unless (defined $snmpkey) {
262                 $session->close;
263                 $state = 'CRITICAL';
264                 printf "$state: Could not match $ifdescr on $hostname\n";
265                 exit $ERRORS{$state};
266         }
267         
268         return $snmpkey;
271 sub usage($) {
272         print "$_[0]\n";
273         print_usage();
274         exit $ERRORS{"UNKNOWN"};
277 sub print_usage() {
278         printf "\n";
279         printf "usage: \n";
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";
285         printf "\n\n";
288 sub print_help() {
289         print_revision($PROGNAME, '@NP_VERSION@');
290         print_usage();
291         printf "check_ifoperstatus plugin for Nagios monitors operational \n";
292         printf "status of a particular network interface on the target host\n";
293         printf "\nUsage:\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";
331         
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,
359                         );
362         if ($status == 0){
363                 print_help();
364                 exit $ERRORS{'OK'};
365         }
367         if ($opt_V) {
368                 print_revision($PROGNAME,'@NP_VERSION@');
369                 exit $ERRORS{'OK'};
370         }
372         if ($opt_h) {
373                 print_help();
374                 exit $ERRORS{'OK'};
375         }
377         if (! utils::is_hostname($hostname)){
378                 usage("Hostname invalid or not given");
379         }
381         unless ($snmpkey > 0 || defined $ifdescr || defined $iftype){
382                 usage("Either a valid snmp key (-k) or a ifDescr (-d) must be provided");
383         }
385         if (defined $ifName) {
386                 $ifXTable=1;
387         }       
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'};
393                 }
394         }
395         
396         unless (defined $timeout) {
397                 $timeout = $TIMEOUT;
398         }
400         if ($snmp_version !~ /[123]/){
401                 $state='UNKNOWN';
402                 print ("$state: No support for SNMP v$snmp_version yet\n");
403                 exit $ERRORS{$state};
404         }
406         %session_opts = (
407                 -hostname   => $hostname,
408                 -port       => $port,
409                 -version    => $snmp_version,
410                 -maxmsgsize => $maxmsgsize
411         );
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;
420                 
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");
424                         }
425                         
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");
430                                 }
431                                 $session_opts{'-authprotocol'} = $authproto if(defined $authproto);
433                                 if ( !defined $authpass) {
434                                         usage("Auth password/key is not defined");
435                                 }else{
436                                         if ($authpass =~ /^0x/ ) {
437                                                 $session_opts{'-authkey'} = $authpass ;
438                                         }else{
439                                                 $session_opts{'-authpassword'} = $authpass ;
440                                         }
441                                 }
442                         }
443                         
444                         # Privacy (DES encryption) wanted
445                         if ($seclevel eq 'authPriv' ) {
446                                 if (! defined $privpass) {
447                                         usage("Privacy passphrase/key is not defined");
448                                 }else{
449                                         if ($privpass =~ /^0x/){
450                                                 $session_opts{'-privkey'} = $privpass;
451                                         }else{
452                                                 $session_opts{'-privpassword'} = $privpass;
453                                         }
454                                 }
456                                 $session_opts{'-privprotocol'} = $privproto if(defined $privproto);
457                         }
459                         # Context name defined or default
460                         unless ( defined $context) {
461                                 $context = "";
462                         }
463                 
464                 }else {
465                         usage("Security level or name is not defined");
466                 }
467         } # end snmpv3
471 ## End validation