Code

corrected output units
[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 #
11 # This program is free software; you can redistribute it and/or
12 # modify it under the terms of the GNU General Public License
13 # as published by the Free Software Foundation; either version 2
14 # of the License, or (at your option) any later version.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24 #
25 #
26 # Report bugs to: ck@zet.net, nagiosplug-help@lists.sf.net
27
28 # 11.01.2000 Version 1.0
29 #
30 # $Id$
32 use POSIX;
33 use strict;
34 use lib utils.pm ;
35 use utils qw($TIMEOUT %ERRORS &print_revision &support);
37 use Net::SNMP;
38 use Getopt::Long;
39 Getopt::Long::Configure('bundling');
41 my $PROGNAME = "check_ifstatus";
44 my $status;
45 my %ifOperStatus =      ('1','up',
46                          '2','down',
47                          '3','testing',
48                          '4','unknown',
49                          '5','dormant',
50                          '6','notPresent');
52 my $state = "UNKNOWN";
53 my $answer = "";
54 my $snmpkey=0;
55 my $snmpoid=0;
56 my $key=0;
57 my $community = "public";
58 my $port = 161;
59 my @snmpoids;
60 my $snmpIfAdminStatus = '1.3.6.1.2.1.2.2.1.7';
61 my $snmpIfDescr = '1.3.6.1.2.1.2.2.1.2';
62 my $snmpIfOperStatus = '1.3.6.1.2.1.2.2.1.8';
63 my $snmpIfName = '1.3.6.1.2.1.31.1.1.1.1';
64 my $snmpIfAlias = '1.3.6.1.2.1.31.1.1.1.18';
65 my $snmpLocIfDescr = '1.3.6.1.4.1.9.2.2.1.1.28';
66 my $snmpIfType = '1.3.6.1.2.1.2.2.1.3';
67 my $hostname;
68 my $session;
69 my $error;
70 my $response;
71 my %ifStatus;
72 my $ifup =0 ;
73 my $ifdown =0;
74 my $ifdormant = 0;
75 my $ifexclude = 0 ;
76 my $ifunused = 0;
77 my $ifmessage = "";
78 my $snmp_version = 1;
79 my $ifXTable;
80 my $opt_h ;
81 my $opt_V ;
82 my $opt_u;
83 my $opt_x ;
84 my %excluded ;
85 my @unused_ports ;
91 # Just in case of problems, let's not hang Nagios
92 $SIG{'ALRM'} = sub {
93      print ("ERROR: No snmp response from $hostname (alarm timeout)\n");
94      exit $ERRORS{"UNKNOWN"};
95 };
96 alarm($TIMEOUT);
100 #Option checking
101 $status = GetOptions(
102                 "V"   => \$opt_V, "version"    => \$opt_V,
103                 "h"   => \$opt_h, "help"       => \$opt_h,
104                 "v=i" => \$snmp_version, "snmp_version=i"  => \$snmp_version,
105                 "C=s" =>\$community,"community=s" => \$community,
106                 "p=i" =>\$port, "port=i" => \$port,
107                 "H=s" => \$hostname, "hostname=s" => \$hostname,
108                 "I"       => \$ifXTable, "ifmib" => \$ifXTable,
109                 "x:s"           =>      \$opt_x,   "exclude:s" => \$opt_x,
110                 "u=s" => \$opt_u,  "unused_ports=s" => \$opt_u);
111                 
112 if ($status == 0)
114         print_help() ;
115         exit $ERRORS{'OK'};
119 if ($opt_V) {
120         print_revision($PROGNAME,'$Revision$ ');
121         exit $ERRORS{'OK'};
124 if ($opt_h) {
125         print_help();
126         exit $ERRORS{'OK'};
130 if (defined $opt_x) {
131         my @x = split(/,/, $opt_x);
132         if ( @x) {
133                 foreach $key (@x){
134                         $excluded{$key} = 1;
135                 }
136         }else{
137                 $excluded{23} = 1; # default PPP(23) if empty list - note (AIX seems to think PPP is 22 according to a post)
138         }
139         #debugging
140         #foreach $x (keys %excluded) 
141         #       { print "key = $x  val = $excluded{$x}\n";}
144 if ($opt_u) {
145         @unused_ports = split(/,/,$opt_u);
146         foreach $key (@unused_ports) { 
147                 $ifStatus{$key}{'notInUse'}++ ;
148         }
151 if (! utils::is_hostname($hostname)){
152         usage();
153         exit $ERRORS{"UNKNOWN"};
156 if ( ! $snmp_version ) {
157         $snmp_version =1 ;
158 }else{
159         if ( $snmp_version =~ /[12]/ ) {
160                         
161                 ($session, $error) = Net::SNMP->session(
162                       -hostname  => $hostname,
163                       -community => $community,
164                       -port      => $port,
165                           -version      => $snmp_version
166                            );
168                 if (!defined($session)) {
169                       $state='UNKNOWN';
170                       $answer=$error;
171                       print ("$state: $answer");
172                       exit $ERRORS{$state};
173                 }
175                 
176         }elsif ( $snmp_version =~ /3/ ) {
177                 $state='UNKNOWN';
178                 print ("$state: No support for SNMP v3 yet\n");
179                 exit $ERRORS{$state};
180         }else{
181                 $state='UNKNOWN';
182                 print ("$state: No support for SNMP v$snmp_version yet\n");
183                 exit $ERRORS{$state};
184         }
189 push(@snmpoids,$snmpIfOperStatus);
190 push(@snmpoids,$snmpIfAdminStatus);
191 push(@snmpoids,$snmpIfDescr);
192 push(@snmpoids,$snmpIfType);
193 push(@snmpoids,$snmpIfName) if ( defined $ifXTable);
194 push(@snmpoids,$snmpIfAlias) if ( defined $ifXTable);
199 foreach $snmpoid (@snmpoids) {
201    if (!defined($response = $session->get_table($snmpoid))) {
202       $answer=$session->error;
203       $session->close;
204       $state = 'CRITICAL';
205                         if ( ( $snmpoid =~ $snmpIfName ) && defined $ifXTable ) {
206                                 print ("$state: Device does not support ifTable - try without -I option\n");
207                         }else{
208               print ("$state: $answer for $snmpoid  with snmp version $snmp_version\n");
209                         }
210       exit $ERRORS{$state};
211    }
213    foreach $snmpkey (keys %{$response}) {
214       $snmpkey =~ /.*\.(\d+)$/;
215       $key = $1;
216       $ifStatus{$key}{$snmpoid} = $response->{$snmpkey};
217    }
221 $session->close;
223 foreach $key (keys %ifStatus) {
225         # skip unused interfaces
226         if (!defined($ifStatus{$key}{'notInUse'})) {
227                 # check only if interface is administratively up
228     if ($ifStatus{$key}{$snmpIfAdminStatus} == 1 ) {
229     
230                         # check only if interface type is not listed in %excluded
231                         if (!defined $excluded{$ifStatus{$key}{$snmpIfType}} ) {
232                                 if ($ifStatus{$key}{$snmpIfOperStatus} == 1 ) { $ifup++ ;}
233                 if ($ifStatus{$key}{$snmpIfOperStatus} == 2 ) {
234                         $ifdown++ ;
235                                                                 if (defined $ifXTable) {
236                                                                         $ifmessage .= sprintf("%s: down -> %s<BR>",
237                                 $ifStatus{$key}{$snmpIfName},
238                                                                                                                                 $ifStatus{$key}{$snmpIfAlias});
239                                                                 }else{
240                                                                         $ifmessage .= sprintf("%s: down <BR>",
241                                                                                                                                 $ifStatus{$key}{$snmpIfDescr});
242                                                                 }
243                                 }
244         if ($ifStatus{$key}{$snmpIfOperStatus} == 5 ) { $ifdormant++ ;}
245                         }else{
246                                 $ifexclude++;
247                         }
248                 
249                 }
250         }else{
251                 $ifunused++;
252         }
255    if ($ifdown > 0) {
256       $state = 'CRITICAL';
257       $answer = sprintf("host '%s', interfaces up: %d, down: %d, dormant: %d, excluded: %d, unused: %d<BR>",
258                         $hostname,
259                         $ifup,
260                         $ifdown,
261                         $ifdormant,
262                         $ifexclude,
263                         $ifunused);
264       $answer = $answer . $ifmessage . "\n";
265    }
266    else {
267       $state = 'OK';
268       $answer = sprintf("host '%s', interfaces up: %d, down: %d, dormant: %d, excluded: %d, unused: %d",
269                         $hostname,
270                         $ifup,
271                         $ifdown,
272                         $ifdormant,
273                         $ifexclude,
274                         $ifunused);
275    }
276 my $perfdata = sprintf("up:%d,down:%d,dormant:%d,excluded:%d,unused:%d",$ifup,$ifdown,$ifdormant,$ifexclude,$ifunused);
277 print ("$state: $answer |$perfdata\n");
278 exit $ERRORS{$state};
281 sub usage {
282         printf "\nMissing arguments!\n";
283         printf "\n";
284         printf "check_ifstatus -C <READCOMMUNITY> -p <PORT> -H <HOSTNAME>\n";
285         printf "Copyright (C) 2000 Christoph Kron\n";
286         printf "Updates 5/2002 Subhendu Ghosh\n";
287         printf "\n\n";
288         support();
289         exit $ERRORS{"UNKNOWN"};
292 sub print_help {
293         printf "check_ifstatus plugin for Nagios monitors operational \n";
294         printf "status of each network interface on the target host\n";
295         printf "\nUsage:\n";
296         printf "   -H (--hostname)   Hostname to query - (required)\n";
297         printf "   -C (--community)  SNMP read community (defaults to public,\n";
298         printf "                     used with SNMP v1 and v2c\n";
299         printf "   -v (--snmp_version)  1 for SNMP v1 (default)\n";
300         printf "                        2 for SNMP v2c\n";
301         printf "                        SNMP v2c will use get_bulk for less overhead\n";
302         printf "   -p (--port)       SNMP port (default 161)\n";
303         printf "   -I (--ifmib)      Agent supports IFMIB ifXTable.  For Cisco - this will provide\n";
304         printf "                     the descriptive name.  Do not use if you don't know what this is. \n";
305         printf "   -x (--exclude)    A comma separated list of ifType values that should be excluded \n";
306         printf "                     from the report (default for an empty list is PPP(23).\n";
307         printf "                     See the IANAifType-MIB for a list of interface types.\n";
308         printf "   -V (--version)    Plugin version\n";
309         printf "   -h (--help)       usage help \n\n";
310         print_revision($PROGNAME, '$Revision$');
311