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 #
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 # $Id$
29 use POSIX;
30 use strict;
31 use lib utils.pm ;
32 use utils qw($TIMEOUT %ERRORS &print_revision &support);
34 use Net::SNMP;
35 use Getopt::Long;
36 &Getopt::Long::config('bundling');
38 my $PROGNAME = "check_ifoperstatus";
39 my $status;
40 my %ifOperStatus = ('1','up',
41 '2','down',
42 '3','testing',
43 '4','unknown',
44 '5','dormant',
45 '6','notPresent');
47 my $state = "UNKNOWN";
48 my $answer = "";
49 my $snmpkey = 0;
50 my $community = "public";
51 my $port = 161;
52 my @snmpoids;
53 my $snmpIfDescr = '1.3.6.1.2.1.2.2.1.2';
54 my $snmpIfOperStatus = '1.3.6.1.2.1.2.2.1.8';
55 my $snmpIfName = '1.3.6.1.2.1.31.1.1.1.1';
56 my $snmpIfAlias = '1.3.6.1.2.1.31.1.1.1.18';
57 my $snmpLocIfDescr = '1.3.6.1.4.1.9.2.2.1.1.28';
58 my $hostname;
59 my $session;
60 my $error;
61 my $response;
62 my $snmp_version = 1 ;
63 my $ifXTable;
64 my $opt_h ;
65 my $opt_V ;
66 my $ifdescr;
67 my $key;
71 # Just in case of problems, let's not hang Nagios
72 $SIG{'ALRM'} = sub {
73 print ("ERROR: No snmp response from $hostname (alarm)\n");
74 exit $ERRORS{"UNKNOWN"};
75 };
76 #alarm($TIMEOUT);
79 $status = GetOptions(
80 "V" => \$opt_V, "version" => \$opt_V,
81 "h" => \$opt_h, "help" => \$opt_h,
82 "v=i" => \$snmp_version, "snmp_version=i" => \$snmp_version,
83 "C=s" =>\$community, "community=s" => \$community,
84 "k=i" =>\$snmpkey, "key=i",\$snmpkey,
85 "d=s" =>\$ifdescr, "descr=s" => \$ifdescr,
86 "p=i" =>\$port, "port=i",\$port,
87 "H=s" => \$hostname, "hostname=s" => \$hostname,
88 "I" => \$ifXTable, "ifmib" => \$ifXTable);
92 if ($status == 0)
93 {
94 print_help();
95 exit $ERRORS{'OK'};
96 }
98 if ($opt_V) {
99 print_revision($PROGNAME,'$Revision$ ');
100 exit $ERRORS{'OK'};
101 }
103 if ($opt_h) {
104 print_help();
105 exit $ERRORS{'OK'};
106 }
108 if (! utils::is_hostname($hostname)){
109 usage();
110 exit $ERRORS{"UNKNOWN"};
111 }
114 if ( $snmp_version =~ /[12]/ ) {
115 ($session, $error) = Net::SNMP->session(
116 -hostname => $hostname,
117 -community => $community,
118 -port => $port,
119 -version => $snmp_version
120 );
122 if (!defined($session)) {
123 $state='UNKNOWN';
124 $answer=$error;
125 print ("$state: $answer");
126 exit $ERRORS{$state};
127 }
128 }elsif ( $snmp_version =~ /3/ ) {
129 $state='UNKNOWN';
130 print ("$state: No support for SNMP v3 yet\n");
131 exit $ERRORS{$state};
132 }else{
133 $state='UNKNOWN';
134 print ("$state: No support for SNMP v$snmp_version yet\n");
135 exit $ERRORS{$state};
136 }
138 if (defined $ifdescr) {
139 # escape "/" in ifdescr - very common in the Cisco world
140 $ifdescr =~ s/\//\\\//g;
142 $status=fetch_ifdescr(); # if using on device with large number of interfaces
143 # recommend use of SNMP v2 (get-bulk)
144 if ($status==0) {
145 $state = "UNKNOWN";
146 printf "$state: could not retrive ifIndex - $status-$snmpkey\n";
147 $session->close;
148 exit $ERRORS{$state};
149 }
150 }
151 if ( $snmpkey == 0 ) {
152 printf "ifIndex key cannot be 0\n";
153 usage();
154 exit $ERRORS{'UNKNOWN'};
155 }
157 $snmpIfOperStatus = '1.3.6.1.2.1.2.2.1.8' . "." . $snmpkey;
158 $snmpIfDescr = '1.3.6.1.2.1.2.2.1.2' . "." . $snmpkey;
159 $snmpIfAlias = '1.3.6.1.2.1.31.1.1.1.18' . "." . $snmpkey ;
162 push(@snmpoids,$snmpIfOperStatus);
163 push(@snmpoids,$snmpIfDescr);
164 push(@snmpoids,$snmpIfAlias) if (defined $ifXTable) ;
166 if (!defined($response = $session->get_request(@snmpoids))) {
167 $answer=$session->error;
168 $session->close;
169 $state = 'CRITICAL';
170 print ("$state: $answer for ifIndex $snmpkey\n");
171 exit $ERRORS{$state};
172 }
174 $answer = sprintf("host '%s', %s(%s) is %s\n",
175 $hostname,
176 $response->{$snmpIfDescr},
177 $snmpkey,
178 $ifOperStatus{$response->{$snmpIfOperStatus}}
179 );
181 $session->close;
183 if ( $response->{$snmpIfOperStatus} == 1 ) {
184 $state = 'OK';
185 }
186 else {
187 $state = 'CRITICAL';
188 }
190 print ("$state: $answer");
191 exit $ERRORS{$state};
194 sub fetch_ifdescr {
195 if (!defined ($response = $session->get_table($snmpIfDescr))) {
196 $answer=$session->error;
197 $session->close;
198 $state = 'CRITICAL';
199 printf ("$state: $answer for $snmpIfDescr with snmp version $snmp_version\n");
200 $session->close;
201 exit $ERRORS{$state};
202 }
204 foreach $key ( keys %{$response}) {
205 if ($response->{$key} =~ /$ifdescr/) {
206 $key =~ /.*\.(\d+)$/;
207 $snmpkey = $1;
208 #print "$ifdescr = $key / $snmpkey \n"; #debug
209 }
210 }
211 unless (defined $snmpkey) {
212 $session->close;
213 $state = 'CRITICAL';
214 printf "$state: Could not match $ifdescr on $hostname\n";
215 exit $ERRORS{$state};
216 }
218 return $snmpkey;
219 }
221 sub usage {
222 printf "\nMissing arguments!\n";
223 printf "\n";
224 printf "usage: \n";
225 printf "check_ifoperstatus -k <IF_KEY> -H <HOSTNAME> [-C <community>]\n";
226 printf "Copyright (C) 2000 Christoph Kron\n";
227 printf "check_ifoperstatus.pl comes with ABSOLUTELY NO WARRANTY\n";
228 printf "This programm is licensed under the terms of the ";
229 printf "GNU General Public License\n(check source code for details)\n";
230 printf "\n\n";
231 exit $ERRORS{"UNKNOWN"};
232 }
234 sub print_help {
235 printf "check_ifoperstatus plugin for Nagios monitors operational \n";
236 printf "status of a particular network interface on the target host\n";
237 printf "\nUsage:\n";
238 printf " -H (--hostname) Hostname to query - (required)\n";
239 printf " -C (--community) SNMP read community (defaults to public,\n";
240 printf " used with SNMP v1 and v2c\n";
241 printf " -v (--snmp_version) 1 for SNMP v1 (default)\n";
242 printf " 2 for SNMP v2c\n";
243 printf " SNMP v2c will use get_bulk for less overhead\n";
244 printf " if monitoring with -d\n";
245 printf " -k (--key) SNMP ifIndex value\n";
246 printf " -d (--descr) SNMP ifDescr value\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 printf " -k or -d must be specified\n\n";
253 print_revision($PROGNAME, '$Revision$');
255 }