Code

4f17711680770bdaaf4fdaaa1367ca3a72e8d632
[nagiosplug.git] / plugins-scripts / check_ifstatus.pl
1 #!/usr/local/bin/perl -w
2 #
3 # check_ifstatus.pl - nagios plugin 
4
5 #
6 # Copyright (C) 2000 Christoph Kron
7 # Modified 5/2002 to conform to updated Nagios Plugin Guidelines (S. Ghosh)
8 #  Added -x option (4/2003)
9 #  Added -u option (4/2003)
10 #  Added -M option (10/2003)
11 #  Added SNMPv3 support (10/2003)
12 #
13 # This program is free software; you can redistribute it and/or
14 # modify it under the terms of the GNU General Public License
15 # as published by the Free Software Foundation; either version 2
16 # of the License, or (at your option) any later version.
17 #
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26 #
27 #
28 # Report bugs to: ck@zet.net, nagiosplug-help@lists.sf.net
29
30 # 11.01.2000 Version 1.0
31 #
32 # $Id$
34 use POSIX;
35 use strict;
36 use lib utils.pm ;
37 use utils qw($TIMEOUT %ERRORS &print_revision &support);
39 use Net::SNMP;
40 use Getopt::Long;
41 Getopt::Long::Configure('bundling');
43 my $PROGNAME = "check_ifstatus";
45 sub print_help ();
46 sub usage ();
47 sub process_arguments ();
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 $snmpoid=0;
63 my $key=0;
64 my $community = "public";
65 my $maxmsgsize = 1472 ; # Net::SNMP default is 1472
66 my ($seclevel, $authproto, $secname, $authpass, $privpass, $auth, $priv, $context);
67 my $port = 161;
68 my @snmpoids;
69 my $snmpIfAdminStatus = '1.3.6.1.2.1.2.2.1.7';
70 my $snmpIfDescr = '1.3.6.1.2.1.2.2.1.2';
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 $snmpIfAlias = '1.3.6.1.2.1.31.1.1.1.18';
74 my $snmpLocIfDescr = '1.3.6.1.4.1.9.2.2.1.1.28';
75 my $snmpIfType = '1.3.6.1.2.1.2.2.1.3';
76 my $hostname;
77 my $session;
78 my $error;
79 my $response;
80 my %ifStatus;
81 my $ifup =0 ;
82 my $ifdown =0;
83 my $ifdormant = 0;
84 my $ifexclude = 0 ;
85 my $ifunused = 0;
86 my $ifmessage = "";
87 my $snmp_version = 1;
88 my $ifXTable;
89 my $opt_h ;
90 my $opt_V ;
91 my $opt_u;
92 my $opt_x ;
93 my %excluded ;
94 my @unused_ports ;
100 # Just in case of problems, let's not hang Nagios
101 $SIG{'ALRM'} = sub {
102      print ("ERROR: No snmp response from $hostname (alarm timeout)\n");
103      exit $ERRORS{"UNKNOWN"};
104 };
107 #Option checking
108 $status = process_arguments();
110 if ($status != 0)
112         print_help() ;
113         exit $ERRORS{'OK'};
117 alarm($TIMEOUT);
119 push(@snmpoids,$snmpIfOperStatus);
120 push(@snmpoids,$snmpIfAdminStatus);
121 push(@snmpoids,$snmpIfDescr);
122 push(@snmpoids,$snmpIfType);
123 push(@snmpoids,$snmpIfName) if ( defined $ifXTable);
124 push(@snmpoids,$snmpIfAlias) if ( defined $ifXTable);
129 foreach $snmpoid (@snmpoids) {
131    if (!defined($response = $session->get_table($snmpoid))) {
132       $answer=$session->error;
133       $session->close;
134       $state = 'CRITICAL';
135                         if ( ( $snmpoid =~ $snmpIfName ) && defined $ifXTable ) {
136                                 print ("$state: Device does not support ifTable - try without -I option\n");
137                         }else{
138                                 print ("$state: $answer for $snmpoid  with snmp version $snmp_version\n");
139                         }
140       exit $ERRORS{$state};
141    }
143    foreach $snmpkey (keys %{$response}) {
144       $snmpkey =~ /.*\.(\d+)$/;
145       $key = $1;
146       $ifStatus{$key}{$snmpoid} = $response->{$snmpkey};
147    }
151 $session->close;
153 alarm(0);
155 foreach $key (keys %ifStatus) {
157         # skip unused interfaces
158         if (!defined($ifStatus{$key}{'notInUse'})) {
159                 # check only if interface is administratively up
160     if ($ifStatus{$key}{$snmpIfAdminStatus} == 1 ) {
161     
162                         # check only if interface type is not listed in %excluded
163                         if (!defined $excluded{$ifStatus{$key}{$snmpIfType}} ) {
164                                 if ($ifStatus{$key}{$snmpIfOperStatus} == 1 ) { $ifup++ ;}
165                                 if ($ifStatus{$key}{$snmpIfOperStatus} == 2 ) {
166                                                                 $ifdown++ ;
167                                                                 if (defined $ifXTable) {
168                                                                         $ifmessage .= sprintf("%s: down -> %s<BR>",
169                                 $ifStatus{$key}{$snmpIfName},
170                                                                                                                                 $ifStatus{$key}{$snmpIfAlias});
171                                                                 }else{
172                                                                         $ifmessage .= sprintf("%s: down <BR>",
173                                                                                                                                 $ifStatus{$key}{$snmpIfDescr});
174                                                                 }
175                                 }
176                                 if ($ifStatus{$key}{$snmpIfOperStatus} == 5 ) { $ifdormant++ ;}
177                         }else{
178                                 $ifexclude++;
179                         }
180                 
181                 }
182         }else{
183                 $ifunused++;
184         }
187    if ($ifdown > 0) {
188       $state = 'CRITICAL';
189       $answer = sprintf("host '%s', interfaces up: %d, down: %d, dormant: %d, excluded: %d, unused: %d<BR>",
190                         $hostname,
191                         $ifup,
192                         $ifdown,
193                         $ifdormant,
194                         $ifexclude,
195                         $ifunused);
196       $answer = $answer . $ifmessage . "\n";
197    }
198    else {
199       $state = 'OK';
200       $answer = sprintf("host '%s', interfaces up: %d, down: %d, dormant: %d, excluded: %d, unused: %d",
201                         $hostname,
202                         $ifup,
203                         $ifdown,
204                         $ifdormant,
205                         $ifexclude,
206                         $ifunused);
207    }
208 my $perfdata = sprintf("up:%d,down:%d,dormant:%d,excluded:%d,unused:%d",$ifup,$ifdown,$ifdormant,$ifexclude,$ifunused);
209 print ("$state: $answer |$perfdata\n");
210 exit $ERRORS{$state};
213 sub usage (){
214         printf "\nMissing arguments!\n";
215         printf "\n";
216         printf "check_ifstatus -C <READCOMMUNITY> -p <PORT> -H <HOSTNAME>\n";
217         printf "Copyright (C) 2000 Christoph Kron\n";
218         printf "Updates 5/2002 Subhendu Ghosh\n";
219         printf "\n\n";
220         support();
221         exit $ERRORS{"UNKNOWN"};
224 sub print_help (){
225         printf "check_ifstatus plugin for Nagios monitors operational \n";
226         printf "status of each network interface on the target host\n";
227         printf "\nUsage:\n";
228         printf "   -H (--hostname)   Hostname to query - (required)\n";
229         printf "   -C (--community)  SNMP read community (defaults to public,\n";
230         printf "                     used with SNMP v1 and v2c\n";
231         printf "   -v (--snmp_version)  1 for SNMP v1 (default)\n";
232         printf "                        2 for SNMP v2c\n";
233         printf "                          SNMP v2c will use get_bulk for less overhead\n";
234         printf "                        3 for SNMPv3 (requires -U option)";
235         printf "   -p (--port)       SNMP port (default 161)\n";
236         printf "   -I (--ifmib)      Agent supports IFMIB ifXTable.  For Cisco - this will provide\n";
237         printf "                     the descriptive name.  Do not use if you don't know what this is. \n";
238         printf "   -x (--exclude)    A comma separated list of ifType values that should be excluded \n";
239         printf "                     from the report (default for an empty list is PPP(23).\n";
240         printf "   -u (--unused_ports) A comma separated list of ifIndex values that should be excluded \n";
241         printf "                     from the report (default is an empty exclusion list).\n";
242         printf "                     See the IANAifType-MIB for a list of interface types.\n";
243         printf "   -L (--seclevel)   choice of \"noAuthNoPriv\", \"authNoPriv\", or     \"authPriv\"\n";
244         printf "   -U (--secname)    username for SNMPv3 context\n";
245         printf "   -c (--context)    SNMPv3 context name (default is empty      string)";
246         printf "   -A (--authpass)   authentication password (cleartext ascii or localized key\n";
247         printf "                     in hex with 0x prefix generated by using   \"snmpkey\" utility\n"; 
248         printf "                     auth password and authEngineID\n";
249         printf "   -a (--authproto)  Authentication protocol ( MD5 or SHA1)\n";
250         printf "   -X (--privpass)   privacy password (cleartext ascii or localized key\n";
251         printf "                     in hex with 0x prefix generated by using   \"snmpkey\" utility\n"; 
252         printf "                     privacy password and authEngineID\n";
253         printf "   -M (--maxmsgsize) Max message size - usefull only for v1 or v2c\n";
254         printf "   -V (--version)    Plugin version\n";
255         printf "   -h (--help)       usage help \n\n";
256         print_revision($PROGNAME, '$Revision$');
257         
260 sub process_arguments() {
261         $status = GetOptions(
262                 "V"   => \$opt_V, "version"    => \$opt_V,
263                 "h"   => \$opt_h, "help"       => \$opt_h,
264                 "v=i" => \$snmp_version, "snmp_version=i"  => \$snmp_version,
265                 "C=s" => \$community,"community=s" => \$community,
266                 "L=s" => \$seclevel, "seclevel=s" => \$seclevel,
267                 "a=s" => \$authproto, "authproto=s" => \$authproto,
268                 "U=s" => \$secname,   "secname=s"   => \$secname,
269                 "A=s" => \$authpass,  "authpass=s"  => \$authpass,
270                 "X=s" => \$privpass,  "privpass=s"  => \$privpass,
271                 "c=s" => \$context,   "context=s"   => \$context,
272                 "p=i" =>\$port, "port=i" => \$port,
273                 "H=s" => \$hostname, "hostname=s" => \$hostname,
274                 "I"             => \$ifXTable, "ifmib" => \$ifXTable,
275                 "x:s"           =>      \$opt_x,   "exclude:s" => \$opt_x,
276                 "u=s" => \$opt_u,  "unused_ports=s" => \$opt_u,
277                 "M=i" => \$maxmsgsize, "maxmsgsize=i" => \$maxmsgsize);
278                 
279         if ($status == 0){
280                 print_help() ;
281                 exit $ERRORS{'OK'};
282         }
283         if ($opt_V) {
284                 print_revision($PROGNAME,'$Revision$ ');
285                 exit $ERRORS{'OK'};
286         }
288         if ($opt_h) {
289                 print_help();
290                 exit $ERRORS{'OK'};
291         }
294         if ($snmp_version =~ /3/ ) {
295                 # Must define a security level even though default is noAuthNoPriv
296                 # v3 requires a security username
297                 if (defined $seclevel  && defined $secname) {
298                 
299                         # Must define a security level even though defualt is noAuthNoPriv
300                         unless ($seclevel eq ('noAuthNoPriv' || 'authNoPriv' || 'authPriv' ) ) {
301                                 usage();
302                                 exit $ERRORS{"UNKNOWN"};
303                         }
304                         
305                         # Authentication wanted
306                         if ($seclevel eq ('authNoPriv' || 'authPriv') ) {
307                 
308                                 unless ($authproto eq ('MD5' || 'SHA1') ) {
309                                         usage();
310                                         exit $ERRORS{"UNKNOWN"};
311                                 }
313                                 if ( !defined $authpass) {
314                                         usage();
315                                         exit $ERRORS{"UNKNOWN"};
316                                 }else{
317                                         if ($authpass =~ /^0x/ ) {
318                                                 $auth = "-authkey => $authpass" ;
319                                         }else{
320                                                 $auth = "-authpassword => $authpass";
321                                         }
322                                 }
323                                         
324                         }
325                         
326                         # Privacy (DES encryption) wanted
327                         if ($seclevel eq  'authPriv' ) {
328                                 if (! defined $privpass) {
329                                         usage();
330                                         exit $ERRORS{"UNKNOWN"};
331                                 }else{
332                                         if ($privpass =~ /^0x/){
333                                                 $priv = "-privkey => $privpass";
334                                         }else{
335                                                 $priv = "-privpassword => $privpass";
336                                         }
337                                 }
338                         }
340                         # Context name defined or default
342                         unless ( defined $context) {
343                                 $context = "";
344                         }
345                 
346                 
347                 
348                 }else {
349                                         usage();
350                                         exit $ERRORS{'UNKNOWN'}; ;
351                 }
352         } # end snmpv3
354         # for snmp v1 & v2c we default to community = "public"
355         
356         # Excluded interfaces types (ifType) (backup interfaces, dial-on demand interfaces, PPP interfaces
357         if (defined $opt_x) {
358                 my @x = split(/,/, $opt_x);
359                 if ( @x) {
360                         foreach $key (@x){
361                                 $excluded{$key} = 1;
362                         }
363                 }else{
364                         $excluded{23} = 1; # default PPP(23) if empty list - note (AIX seems to think PPP is 22 according to a post)
365                 }
366         }
367         
368         # Excluded interface ports (ifIndex) - management reasons
369         if ($opt_u) {
370                 @unused_ports = split(/,/,$opt_u);
371                 foreach $key (@unused_ports) { 
372                         $ifStatus{$key}{'notInUse'}++ ;
373                 }
374         }
376         if (! utils::is_hostname($hostname)){
377                 usage();
378                 exit $ERRORS{"UNKNOWN"};
379         }
381         # create SNMP session handle based on options passed.
383         if ( ! $snmp_version ) {
384                 $snmp_version =1 ;
385         }else{
386                 if ( $snmp_version =~ /[12]/ ) {
387         
388                         ($session, $error) = Net::SNMP->session(
389                                                 -hostname  => $hostname,
390                                                 -community => $community,
391                                                 -port      => $port,
392                                                 -version        => $snmp_version,
393                                                 -maxmsgsize => $maxmsgsize
394                          );
396                 if (!defined($session)) {
397                                         $state='UNKNOWN';
398                                         $answer=$error;
399                                         print ("$state: $answer");
400                                         exit $ERRORS{$state};
401                 }
403                 
404         }elsif ( $snmp_version =~ /3/ ) {
406                 if ($seclevel eq 'noAuthNoPriv') {
407                         ($session, $error) = Net::SNMP->session(
408                                 -hostname  => $hostname,
409                                 -port      => $port,
410                                 -version  => $snmp_version,
411                                 -username => $secname,
412                         );
414                 }elsif ( $seclevel eq 'authNoPriv' ) {
415                         ($session, $error) = Net::SNMP->session(
416                                 -hostname  => $hostname,
417                                 -port      => $port,
418                                 -version  => $snmp_version,
419                                 -username => $secname,
420                                 -authprotocol => $authproto,
421                                 $auth
422                         );      
423                 }elsif ($seclevel eq 'authPriv' ) {
424                         ($session, $error) = Net::SNMP->session(
425                                 -hostname  => $hostname,
426                                 -port      => $port,
427                                 -version  => $snmp_version,
428                                 -username => $secname,
429                                 -authprotocol => $authproto,
430                                 $auth,
431                                 $priv
432                         );
433                 }
434                                         
435                                         
436                 if (!defined($session)) {
437                                         $state='UNKNOWN';
438                                         $answer=$error;
439                                         print ("$state: $answer");
440                                         exit $ERRORS{$state};
441                 }
442                         
443         }else{
444                 $state='UNKNOWN';
445                 print ("$state: No support for SNMP v$snmp_version yet\n");
446                 exit $ERRORS{$state};
447         }
450 return $ERRORS{"OK"};
451