Code

Cleaned up cache output
[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 #
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License
11 # as published by the Free Software Foundation; either version 2
12 # of the License, or (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22 #
23 #
24 # Report bugs to: ck@zet.net, nagiosplug-help@lists.sf.net
25
26 # 11.01.2000 Version 1.0
27 #
28 # $Id$
30 use POSIX;
31 use strict;
32 use lib utils.pm ;
33 use utils qw($TIMEOUT %ERRORS &print_revision &support);
35 use Net::SNMP;
36 use Getopt::Long;
37 Getopt::Long::Configure('bundling');
39 my $PROGNAME = "check_ifstatus";
42 my $status;
43 my %ifOperStatus =      ('1','up',
44                          '2','down',
45                          '3','testing',
46                          '4','unknown',
47                          '5','dormant',
48                          '6','notPresent');
50 my $state = "UNKNOWN";
51 my $answer = "";
52 my $snmpkey=0;
53 my $snmpoid=0;
54 my $key=0;
55 my $community = "public";
56 my $port = 161;
57 my @snmpoids;
58 my $snmpIfAdminStatus = '1.3.6.1.2.1.2.2.1.7';
59 my $snmpIfDescr = '1.3.6.1.2.1.2.2.1.2';
60 my $snmpIfOperStatus = '1.3.6.1.2.1.2.2.1.8';
61 my $snmpIfName = '1.3.6.1.2.1.31.1.1.1.1';
62 my $snmpIfAlias = '1.3.6.1.2.1.31.1.1.1.18';
63 my $snmpLocIfDescr = '1.3.6.1.4.1.9.2.2.1.1.28';
64 my $snmpIfType = '1.3.6.1.2.1.2.2.1.3';
65 my $hostname;
66 my $session;
67 my $error;
68 my $response;
69 my %ifStatus;
70 my $ifup =0 ;
71 my $ifdown =0;
72 my $ifdormant = 0;
73 my $ifmessage = "";
74 my $snmp_version = 1;
75 my $ifXTable;
76 my $opt_h ;
77 my $opt_V ;
83 # Just in case of problems, let's not hang Nagios
84 $SIG{'ALRM'} = sub {
85      print ("ERROR: No snmp response from $hostname (alarm timeout)\n");
86      exit $ERRORS{"UNKNOWN"};
87 };
88 alarm($TIMEOUT);
92 #Option checking
93 $status = GetOptions(
94                 "V"   => \$opt_V, "version"    => \$opt_V,
95                 "h"   => \$opt_h, "help"       => \$opt_h,
96                 "v=i" => \$snmp_version, "snmp_version=i"  => \$snmp_version,
97                 "C=s" =>\$community,"community=s" => \$community,
98                 "p=i" =>\$port, "port=i" => \$port,
99                 "H=s" => \$hostname, "hostname=s" => \$hostname,
100                 "I"       => \$ifXTable, "ifmib" => \$ifXTable );
101                 
102 if ($status == 0)
104         print_help() ;
105         exit $ERRORS{'OK'};
109 if ($opt_V) {
110         print_revision($PROGNAME,'$Revision$ ');
111         exit $ERRORS{'OK'};
114 if ($opt_h) {
115         print_help();
116         exit $ERRORS{'OK'};
119 if (! utils::is_hostname($hostname)){
120         usage();
121         exit $ERRORS{"UNKNOWN"};
124 if ( ! $snmp_version ) {
125         $snmp_version =1 ;
126 }else{
127         if ( $snmp_version =~ /[12]/ ) {
128                         
129                 ($session, $error) = Net::SNMP->session(
130                       -hostname  => $hostname,
131                       -community => $community,
132                       -port      => $port,
133                           -version      => $snmp_version
134                            );
136                 if (!defined($session)) {
137                       $state='UNKNOWN';
138                       $answer=$error;
139                       print ("$state: $answer");
140                       exit $ERRORS{$state};
141                 }
143                 
144         }elsif ( $snmp_version =~ /3/ ) {
145                 $state='UNKNOWN';
146                 print ("$state: No support for SNMP v3 yet\n");
147                 exit $ERRORS{$state};
148         }else{
149                 $state='UNKNOWN';
150                 print ("$state: No support for SNMP v$snmp_version yet\n");
151                 exit $ERRORS{$state};
152         }
157 push(@snmpoids,$snmpIfOperStatus);
158 push(@snmpoids,$snmpIfAdminStatus);
159 push(@snmpoids,$snmpIfDescr);
160 push(@snmpoids,$snmpIfType);
161 push(@snmpoids,$snmpIfName) if ( defined $ifXTable);
165 foreach $snmpoid (@snmpoids) {
167    if (!defined($response = $session->get_table($snmpoid))) {
168       $answer=$session->error;
169       $session->close;
170       $state = 'CRITICAL';
171       print ("$state: $answer for $snmpoid  with snmp version $snmp_version\n");
172       exit $ERRORS{$state};
173    }
175    foreach $snmpkey (keys %{$response}) {
176       $snmpkey =~ /.*\.(\d+)$/;
177       $key = $1;
178       $ifStatus{$key}{$snmpoid} = $response->{$snmpkey};
179    }
183 $session->close;
185 foreach $key (keys %ifStatus) {
187         # check only if interface is administratively up
188     if ($ifStatus{$key}{$snmpIfAdminStatus} == 1 ) {
189         # check only if interface is not of type 23  aka PPP interface
190                 if ($ifStatus{$key}{$snmpIfType} != 23 ) {
191                         if ($ifStatus{$key}{$snmpIfOperStatus} == 1 ) { $ifup++ ;}
192                 if ($ifStatus{$key}{$snmpIfOperStatus} == 2 ) {
193                 $ifdown++ ;
194                                 $ifmessage .= sprintf("%s: down -> %s<BR>",
195                                  $ifStatus{$key}{$snmpIfDescr},
196                                                                  $ifStatus{$key}{$snmpIfName});
197                         }
198                 if ($ifStatus{$key}{$snmpIfOperStatus} == 5 ) { $ifdormant++ ;}
199                 }
200         }
201    
204    if ($ifdown > 0) {
205       $state = 'CRITICAL';
206       $answer = sprintf("host '%s', interfaces up: %d, down: %d, dormant: %d<BR>",
207                         $hostname,
208                         $ifup,
209                         $ifdown,
210                         $ifdormant);
211       $answer = $answer . $ifmessage . "\n";
212    }
213    else {
214       $state = 'OK';
215       $answer = sprintf("host '%s', interfaces up: %d, down: %d, dormant: %d\n",
216                         $hostname,
217                         $ifup,
218                         $ifdown,
219                         $ifdormant);
220    }
222 print ("$state: $answer");
223 exit $ERRORS{$state};
226 sub usage {
227         printf "\nMissing arguments!\n";
228         printf "\n";
229         printf "check_ifstatus -C <READCOMMUNITY> -p <PORT> -H <HOSTNAME>\n";
230         printf "Copyright (C) 2000 Christoph Kron\n";
231         printf "Updates 5/2002 Subhendu Ghosh\n";
232         printf "\n\n";
233         support();
234         exit $ERRORS{"UNKNOWN"};
237 sub print_help {
238         printf "check_ifstatus plugin for Nagios monitors operational \n";
239         printf "status of each network interface (except PPP interfaces) on the target host\n";
240         printf "\nUsage:\n";
241         printf "   -H (--hostname)   Hostname to query - (required)\n";
242         printf "   -C (--community)  SNMP read community (defaults to public,\n";
243         printf "                     used with SNMP v1 and v2c\n";
244         printf "   -v (--snmp_version)  1 for SNMP v1 (default)\n";
245         printf "                        2 for SNMP v2c\n";
246         printf "                        SNMP v2c will use get_bulk for less overhead\n";
247         printf "   -p (--port)       SNMP port (default 161)\n";
248         printf "   -I (--ifmib)      Agent supports IFMIB ifXTable.  Do not use if\n";
249         printf "                     you don't know what this is.\n";
250         printf "   -V (--version)    Plugin version\n";
251         printf "   -h (--help)       usage help \n\n";
252         print_revision($PROGNAME, '$Revision$');
253