Update THANKS file
[monitoring-plugins.git] / plugins-scripts / check_ifstatus.pl
blob63c71ffae957f9c9f5411f4bb2a649788af83607
1 #!/usr/local/bin/perl -w
3 # check_ifstatus.pl - nagios plugin
4 #
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)
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.
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.
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.
28 # Report bugs to: ck@zet.net, nagiosplug-help@lists.sf.net
30 # 11.01.2000 Version 1.0
33 use POSIX;
34 use strict;
35 use lib utils.pm ;
36 use utils qw($TIMEOUT %ERRORS &print_revision &support);
38 use Net::SNMP;
39 use Getopt::Long;
40 Getopt::Long::Configure('bundling');
42 my $PROGNAME = "check_ifstatus";
44 sub print_help ();
45 sub usage ($);
46 sub print_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 $timeout ;
60 my $state = "UNKNOWN";
61 my $answer = "";
62 my $snmpkey=0;
63 my $snmpoid=0;
64 my $key=0;
65 my $community = "public";
66 my $maxmsgsize = 1472 ; # Net::SNMP default is 1472
67 my ($seclevel, $authproto, $secname, $authpass, $privpass, $privproto, $auth, $priv, $context);
68 my $port = 161;
69 my @snmpoids;
70 my $snmpIfAdminStatus = '1.3.6.1.2.1.2.2.1.7';
71 my $snmpIfDescr = '1.3.6.1.2.1.2.2.1.2';
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 $snmpIfAlias = '1.3.6.1.2.1.31.1.1.1.18';
75 my $snmpLocIfDescr = '1.3.6.1.4.1.9.2.2.1.1.28';
76 my $snmpIfType = '1.3.6.1.2.1.2.2.1.3';
77 my $hostname;
78 my $session;
79 my $error;
80 my $response;
81 my %ifStatus;
82 my $ifup =0 ;
83 my $ifdown =0;
84 my $ifdormant = 0;
85 my $ifexclude = 0 ;
86 my $ifunused = 0;
87 my $ifmessage = "";
88 my $snmp_version = 1;
89 my $ifXTable;
90 my $opt_h ;
91 my $opt_V ;
92 my $opt_u;
93 my $opt_x ;
94 my %excluded ;
95 my @unused_ports ;
96 my %session_opts;
102 # Just in case of problems, let's not hang Nagios
103 $SIG{'ALRM'} = sub {
104 print ("ERROR: No snmp response from $hostname (alarm timeout)\n");
105 exit $ERRORS{"UNKNOWN"};
109 #Option checking
110 $status = process_arguments();
112 if ($status != 0)
114 print_help() ;
115 exit $ERRORS{'OK'};
119 alarm($timeout);
120 ($session, $error) = Net::SNMP->session(%session_opts);
122 if (!defined($session)) {
123 $state='UNKNOWN';
124 $answer=$error;
125 print ("$state: $answer\n");
126 exit $ERRORS{$state};
130 push(@snmpoids,$snmpIfOperStatus);
131 push(@snmpoids,$snmpIfAdminStatus);
132 push(@snmpoids,$snmpIfDescr);
133 push(@snmpoids,$snmpIfType);
134 push(@snmpoids,$snmpIfName) if ( defined $ifXTable);
135 push(@snmpoids,$snmpIfAlias) if ( defined $ifXTable);
140 foreach $snmpoid (@snmpoids) {
142 if (!defined($response = $session->get_table($snmpoid))) {
143 $answer=$session->error;
144 $session->close;
145 $state = 'CRITICAL';
146 if ( ( $snmpoid =~ $snmpIfName ) && defined $ifXTable ) {
147 print ("$state: Device does not support ifTable - try without -I option\n");
148 }else{
149 print ("$state: $answer for $snmpoid with snmp version $snmp_version\n");
151 exit $ERRORS{$state};
154 foreach $snmpkey (keys %{$response}) {
155 $snmpkey =~ /.*\.(\d+)$/;
156 $key = $1;
157 $ifStatus{$key}{$snmpoid} = $response->{$snmpkey};
162 $session->close;
164 alarm(0);
166 foreach $key (keys %ifStatus) {
168 # skip unused interfaces
169 if (!defined($ifStatus{$key}{'notInUse'})) {
170 # check only if interface is administratively up
171 if ($ifStatus{$key}{$snmpIfAdminStatus} == 1 ) {
173 # check only if interface type is not listed in %excluded
174 if (!defined $excluded{$ifStatus{$key}{$snmpIfType}} ) {
175 if ($ifStatus{$key}{$snmpIfOperStatus} == 1 ) { $ifup++ ;}
176 if ($ifStatus{$key}{$snmpIfOperStatus} == 2 ) {
177 $ifdown++ ;
178 if (defined $ifXTable) {
179 $ifmessage .= sprintf("%s: down -> %s<BR>",
180 $ifStatus{$key}{$snmpIfName},
181 $ifStatus{$key}{$snmpIfAlias});
182 }else{
183 $ifmessage .= sprintf("%s: down <BR>",
184 $ifStatus{$key}{$snmpIfDescr});
187 if ($ifStatus{$key}{$snmpIfOperStatus} == 5 ) { $ifdormant++ ;}
188 }else{
189 $ifexclude++;
193 }else{
194 $ifunused++;
198 if ($ifdown > 0) {
199 $state = 'CRITICAL';
200 $answer = sprintf("host '%s', interfaces up: %d, down: %d, dormant: %d, excluded: %d, unused: %d<BR>",
201 $hostname,
202 $ifup,
203 $ifdown,
204 $ifdormant,
205 $ifexclude,
206 $ifunused);
207 $answer = $answer . $ifmessage . "\n";
209 else {
210 $state = 'OK';
211 $answer = sprintf("host '%s', interfaces up: %d, down: %d, dormant: %d, excluded: %d, unused: %d",
212 $hostname,
213 $ifup,
214 $ifdown,
215 $ifdormant,
216 $ifexclude,
217 $ifunused);
219 my $perfdata = sprintf("up=%d,down=%d,dormant=%d,excluded=%d,unused=%d",$ifup,$ifdown,$ifdormant,$ifexclude,$ifunused);
220 print ("$state: $answer |$perfdata\n");
221 exit $ERRORS{$state};
223 sub usage($) {
224 print "$_[0]\n";
225 print_usage();
226 exit $ERRORS{"UNKNOWN"};
229 sub print_usage() {
230 printf "\n";
231 printf "usage: \n";
232 printf "check_ifstatus -C <READCOMMUNITY> -p <PORT> -H <HOSTNAME>\n";
233 printf "Copyright (C) 2000 Christoph Kron\n";
234 printf "Updates 5/2002 Subhendu Ghosh\n";
235 support();
236 printf "\n\n";
239 sub print_help() {
240 print_revision($PROGNAME, '@NP_VERSION@');
241 print_usage();
242 printf "check_ifstatus plugin for Nagios monitors operational \n";
243 printf "status of each network interface on the target host\n";
244 printf "\nUsage:\n";
245 printf " -H (--hostname) Hostname to query - (required)\n";
246 printf " -C (--community) SNMP read community (defaults to public,\n";
247 printf " used with SNMP v1 and v2c\n";
248 printf " -v (--snmp_version) 1 for SNMP v1 (default)\n";
249 printf " 2 for SNMP v2c\n";
250 printf " SNMP v2c will use get_bulk for less overhead\n";
251 printf " 3 for SNMPv3 (requires -U option)";
252 printf " -p (--port) SNMP port (default 161)\n";
253 printf " -I (--ifmib) Agent supports IFMIB ifXTable. For Cisco - this will provide\n";
254 printf " the descriptive name. Do not use if you don't know what this is. \n";
255 printf " -x (--exclude) A comma separated list of ifType values that should be excluded \n";
256 printf " from the report (default for an empty list is PPP(23).\n";
257 printf " -u (--unused_ports) A comma separated list of ifIndex values that should be excluded \n";
258 printf " from the report (default is an empty exclusion list).\n";
259 printf " See the IANAifType-MIB for a list of interface types.\n";
260 printf " -L (--seclevel) choice of \"noAuthNoPriv\", \"authNoPriv\", or \"authPriv\"\n";
261 printf " -U (--secname) username for SNMPv3 context\n";
262 printf " -c (--context) SNMPv3 context name (default is empty string)\n";
263 printf " -A (--authpass) authentication password (cleartext ascii or localized key\n";
264 printf " in hex with 0x prefix generated by using \"snmpkey\" utility\n";
265 printf " auth password and authEngineID\n";
266 printf " -a (--authproto) Authentication protocol (MD5 or SHA1)\n";
267 printf " -X (--privpass) privacy password (cleartext ascii or localized key\n";
268 printf " in hex with 0x prefix generated by using \"snmpkey\" utility\n";
269 printf " privacy password and authEngineID\n";
270 printf " -P (--privproto) privacy protocol (DES or AES; default: DES)\n";
271 printf " -M (--maxmsgsize) Max message size - usefull only for v1 or v2c\n";
272 printf " -t (--timeout) seconds before the plugin times out (default=$TIMEOUT)\n";
273 printf " -V (--version) Plugin version\n";
274 printf " -h (--help) usage help \n\n";
275 print_revision($PROGNAME, '@NP_VERSION@');
279 sub process_arguments() {
280 $status = GetOptions(
281 "V" => \$opt_V, "version" => \$opt_V,
282 "h" => \$opt_h, "help" => \$opt_h,
283 "v=s" => \$snmp_version, "snmp_version=s" => \$snmp_version,
284 "C=s" => \$community,"community=s" => \$community,
285 "L=s" => \$seclevel, "seclevel=s" => \$seclevel,
286 "a=s" => \$authproto, "authproto=s" => \$authproto,
287 "U=s" => \$secname, "secname=s" => \$secname,
288 "A=s" => \$authpass, "authpass=s" => \$authpass,
289 "X=s" => \$privpass, "privpass=s" => \$privpass,
290 "P=s" => \$privproto, "privproto=s" => \$privproto,
291 "c=s" => \$context, "context=s" => \$context,
292 "p=i" =>\$port, "port=i" => \$port,
293 "H=s" => \$hostname, "hostname=s" => \$hostname,
294 "I" => \$ifXTable, "ifmib" => \$ifXTable,
295 "x:s" => \$opt_x, "exclude:s" => \$opt_x,
296 "u=s" => \$opt_u, "unused_ports=s" => \$opt_u,
297 "M=i" => \$maxmsgsize, "maxmsgsize=i" => \$maxmsgsize,
298 "t=i" => \$timeout, "timeout=i" => \$timeout,
301 if ($status == 0){
302 print_help();
303 exit $ERRORS{'OK'};
306 if ($opt_V) {
307 print_revision($PROGNAME,'@NP_VERSION@');
308 exit $ERRORS{'OK'};
311 if ($opt_h) {
312 print_help();
313 exit $ERRORS{'OK'};
316 unless (defined $timeout) {
317 $timeout = $TIMEOUT;
320 # Net::SNMP wants an integer
321 $snmp_version = 2 if $snmp_version eq "2c";
323 if ($snmp_version !~ /^[123]$/){
324 $state='UNKNOWN';
325 print ("$state: No support for SNMP v$snmp_version yet\n");
326 exit $ERRORS{$state};
329 %session_opts = (
330 -hostname => $hostname,
331 -port => $port,
332 -version => $snmp_version,
333 -maxmsgsize => $maxmsgsize
336 $session_opts{'-community'} = $community if (defined $community && $snmp_version =~ /[12]/);
338 if ($snmp_version =~ /3/ ) {
339 # Must define a security level even though default is noAuthNoPriv
340 # v3 requires a security username
341 if (defined $seclevel && defined $secname) {
342 $session_opts{'-username'} = $secname;
344 # Must define a security level even though defualt is noAuthNoPriv
345 unless ( grep /^$seclevel$/, qw(noAuthNoPriv authNoPriv authPriv) ) {
346 usage("Must define a valid security level even though default is noAuthNoPriv");
349 # Authentication wanted
350 if ( $seclevel eq 'authNoPriv' || $seclevel eq 'authPriv' ) {
351 if (defined $authproto && $authproto ne 'MD5' && $authproto ne 'SHA1') {
352 usage("Auth protocol can be either MD5 or SHA1");
354 $session_opts{'-authprotocol'} = $authproto if(defined $authproto);
356 if ( !defined $authpass) {
357 usage("Auth password/key is not defined");
358 }else{
359 if ($authpass =~ /^0x/ ) {
360 $session_opts{'-authkey'} = $authpass ;
361 }else{
362 $session_opts{'-authpassword'} = $authpass ;
367 # Privacy (DES encryption) wanted
368 if ($seclevel eq 'authPriv' ) {
369 if (! defined $privpass) {
370 usage("Privacy passphrase/key is not defined");
371 }else{
372 if ($privpass =~ /^0x/){
373 $session_opts{'-privkey'} = $privpass;
374 }else{
375 $session_opts{'-privpassword'} = $privpass;
379 $session_opts{'-privprotocol'} = $privproto if(defined $privproto);
382 # Context name defined or default
383 unless ( defined $context) {
384 $context = "";
387 }else {
388 usage("Security level or name is not defined");
390 } # end snmpv3
392 # Excluded interfaces types (ifType) (backup interfaces, dial-on demand interfaces, PPP interfaces
393 if (defined $opt_x) {
394 my @x = split(/,/, $opt_x);
395 if ( @x) {
396 foreach $key (@x){
397 $excluded{$key} = 1;
399 }else{
400 $excluded{23} = 1; # default PPP(23) if empty list - note (AIX seems to think PPP is 22 according to a post)
404 # Excluded interface ports (ifIndex) - management reasons
405 if ($opt_u) {
406 @unused_ports = split(/,/,$opt_u);
407 foreach $key (@unused_ports) {
408 $ifStatus{$key}{'notInUse'}++ ;
412 if (! utils::is_hostname($hostname)){
413 usage("Hostname invalid or not given");
414 exit $ERRORS{"UNKNOWN"};
418 if ($snmp_version !~ /[123]/) {
419 $state='UNKNOWN';
420 print ("$state: No support for SNMP v$snmp_version yet\n");
421 exit $ERRORS{$state};
424 return $ERRORS{"OK"};