1 #!/usr/local/bin/perl -w
3 # check_ifstatus.pl - nagios plugin
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
36 use utils
qw($TIMEOUT %ERRORS &print_revision &support);
40 Getopt::Long::Configure('bundling');
42 my $PROGNAME = "check_ifstatus";
46 sub process_arguments ();
50 my %ifOperStatus = ('1','up',
56 '7','lowerLayerDown'); # down due to the state of lower layer interface(s));
59 my $state = "UNKNOWN";
64 my $community = "public";
65 my $maxmsgsize = 1472 ; # Net::SNMP default is 1472
66 my ($seclevel, $authproto, $secname, $authpass, $privpass, $auth, $priv, $context);
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';
100 # Just in case of problems, let's not hang Nagios
102 print ("ERROR: No snmp response from $hostname (alarm timeout)\n");
103 exit $ERRORS{"UNKNOWN"};
108 $status = process_arguments();
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;
135 if ( ( $snmpoid =~ $snmpIfName ) && defined $ifXTable ) {
136 print ("$state: Device does not support ifTable - try without -I option\n");
138 print ("$state: $answer for $snmpoid with snmp version $snmp_version\n");
140 exit $ERRORS{$state};
143 foreach $snmpkey (keys %{$response}) {
144 $snmpkey =~ /.*\.(\d+)$/;
146 $ifStatus{$key}{$snmpoid} = $response->{$snmpkey};
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 ) {
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 ) {
167 if (defined $ifXTable) {
168 $ifmessage .= sprintf("%s: down -> %s<BR>",
169 $ifStatus{$key}{$snmpIfName},
170 $ifStatus{$key}{$snmpIfAlias});
172 $ifmessage .= sprintf("%s: down <BR>",
173 $ifStatus{$key}{$snmpIfDescr});
176 if ($ifStatus{$key}{$snmpIfOperStatus} == 5 ) { $ifdormant++ ;}
189 $answer = sprintf("host '%s', interfaces up: %d, down: %d, dormant: %d, excluded: %d, unused: %d<BR>",
196 $answer = $answer . $ifmessage . "\n";
200 $answer = sprintf("host '%s', interfaces up: %d, down: %d, dormant: %d, excluded: %d, unused: %d",
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};
214 printf "\nMissing arguments!\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";
221 exit $ERRORS{"UNKNOWN"};
225 printf "check_ifstatus plugin for Nagios monitors operational \n";
226 printf "status of each network interface on the target host\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 " -t (--timeout) seconds before the plugin times out (default=$TIMEOUT)\n";
255 printf " -V (--version) Plugin version\n";
256 printf " -h (--help) usage help \n\n";
257 print_revision($PROGNAME, '@NP_VERSION@');
261 sub process_arguments() {
262 $status = GetOptions(
263 "V" => \$opt_V, "version" => \$opt_V,
264 "h" => \$opt_h, "help" => \$opt_h,
265 "v=i" => \$snmp_version, "snmp_version=i" => \$snmp_version,
266 "C=s" => \$community,"community=s" => \$community,
267 "L=s" => \$seclevel, "seclevel=s" => \$seclevel,
268 "a=s" => \$authproto, "authproto=s" => \$authproto,
269 "U=s" => \$secname, "secname=s" => \$secname,
270 "A=s" => \$authpass, "authpass=s" => \$authpass,
271 "X=s" => \$privpass, "privpass=s" => \$privpass,
272 "c=s" => \$context, "context=s" => \$context,
273 "p=i" =>\$port, "port=i" => \$port,
274 "H=s" => \$hostname, "hostname=s" => \$hostname,
275 "I" => \$ifXTable, "ifmib" => \$ifXTable,
276 "x:s" => \$opt_x, "exclude:s" => \$opt_x,
277 "u=s" => \$opt_u, "unused_ports=s" => \$opt_u,
278 "M=i" => \$maxmsgsize, "maxmsgsize=i" => \$maxmsgsize,
279 "t=i" => \$timeout, "timeout=i" => \$timeout,
287 print_revision($PROGNAME,'@NP_VERSION@');
296 unless (defined $timeout) {
300 if ($snmp_version =~ /3/ ) {
301 # Must define a security level even though default is noAuthNoPriv
302 # v3 requires a security username
303 if (defined $seclevel && defined $secname) {
305 # Must define a security level even though defualt is noAuthNoPriv
306 unless ($seclevel eq ('noAuthNoPriv' || 'authNoPriv' || 'authPriv' ) ) {
308 exit $ERRORS{"UNKNOWN"};
311 # Authentication wanted
312 if ($seclevel eq ('authNoPriv' || 'authPriv') ) {
314 unless ($authproto eq ('MD5' || 'SHA1') ) {
316 exit $ERRORS{"UNKNOWN"};
319 if ( !defined $authpass) {
321 exit $ERRORS{"UNKNOWN"};
323 if ($authpass =~ /^0x/ ) {
324 $auth = "-authkey => $authpass" ;
326 $auth = "-authpassword => $authpass";
332 # Privacy (DES encryption) wanted
333 if ($seclevel eq 'authPriv' ) {
334 if (! defined $privpass) {
336 exit $ERRORS{"UNKNOWN"};
338 if ($privpass =~ /^0x/){
339 $priv = "-privkey => $privpass";
341 $priv = "-privpassword => $privpass";
346 # Context name defined or default
348 unless ( defined $context) {
356 exit $ERRORS{'UNKNOWN'}; ;
360 # for snmp v1 & v2c we default to community = "public"
362 # Excluded interfaces types (ifType) (backup interfaces, dial-on demand interfaces, PPP interfaces
363 if (defined $opt_x) {
364 my @x = split(/,/, $opt_x);
370 $excluded{23} = 1; # default PPP(23) if empty list - note (AIX seems to think PPP is 22 according to a post)
374 # Excluded interface ports (ifIndex) - management reasons
376 @unused_ports = split(/,/,$opt_u);
377 foreach $key (@unused_ports) {
378 $ifStatus{$key}{'notInUse'}++ ;
382 if (! utils::is_hostname($hostname)){
384 exit $ERRORS{"UNKNOWN"};
387 # create SNMP session handle based on options passed.
389 if ( ! $snmp_version ) {
392 if ( $snmp_version =~ /[12]/ ) {
394 ($session, $error) = Net::SNMP->session(
395 -hostname => $hostname,
396 -community => $community,
398 -version => $snmp_version,
399 -maxmsgsize => $maxmsgsize
402 if (!defined($session)) {
405 print ("$state: $answer");
406 exit $ERRORS{$state};
410 }elsif ( $snmp_version =~ /3/ ) {
412 if ($seclevel eq 'noAuthNoPriv') {
413 ($session, $error) = Net::SNMP->session(
414 -hostname => $hostname,
416 -version => $snmp_version,
417 -username => $secname,
420 }elsif ( $seclevel eq 'authNoPriv' ) {
421 ($session, $error) = Net::SNMP->session(
422 -hostname => $hostname,
424 -version => $snmp_version,
425 -username => $secname,
426 -authprotocol => $authproto,
429 }elsif ($seclevel eq 'authPriv' ) {
430 ($session, $error) = Net::SNMP->session(
431 -hostname => $hostname,
433 -version => $snmp_version,
434 -username => $secname,
435 -authprotocol => $authproto,
442 if (!defined($session)) {
445 print ("$state: $answer");
446 exit $ERRORS{$state};
451 print ("$state: No support for SNMP v$snmp_version yet\n");
452 exit $ERRORS{$state};
456 return $ERRORS{"OK"};