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
37 use utils
qw($TIMEOUT %ERRORS &print_revision &support);
41 Getopt::Long::Configure('bundling');
43 my $PROGNAME = "check_ifstatus";
47 sub process_arguments ();
51 my %ifOperStatus = ('1','up',
57 '7','lowerLayerDown'); # down due to the state of lower layer interface(s));
60 my $state = "UNKNOWN";
65 my $community = "public";
66 my $maxmsgsize = 1472 ; # Net::SNMP default is 1472
67 my ($seclevel, $authproto, $secname, $authpass, $privpass, $auth, $priv, $context);
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';
101 # Just in case of problems, let's not hang Nagios
103 print ("ERROR: No snmp response from $hostname (alarm timeout)\n");
104 exit $ERRORS{"UNKNOWN"};
109 $status = process_arguments();
120 push(@snmpoids,$snmpIfOperStatus);
121 push(@snmpoids,$snmpIfAdminStatus);
122 push(@snmpoids,$snmpIfDescr);
123 push(@snmpoids,$snmpIfType);
124 push(@snmpoids,$snmpIfName) if ( defined $ifXTable);
125 push(@snmpoids,$snmpIfAlias) if ( defined $ifXTable);
130 foreach $snmpoid (@snmpoids) {
132 if (!defined($response = $session->get_table($snmpoid))) {
133 $answer=$session->error;
136 if ( ( $snmpoid =~ $snmpIfName ) && defined $ifXTable ) {
137 print ("$state: Device does not support ifTable - try without -I option\n");
139 print ("$state: $answer for $snmpoid with snmp version $snmp_version\n");
141 exit $ERRORS{$state};
144 foreach $snmpkey (keys %{$response}) {
145 $snmpkey =~ /.*\.(\d+)$/;
147 $ifStatus{$key}{$snmpoid} = $response->{$snmpkey};
156 foreach $key (keys %ifStatus) {
158 # skip unused interfaces
159 if (!defined($ifStatus{$key}{'notInUse'})) {
160 # check only if interface is administratively up
161 if ($ifStatus{$key}{$snmpIfAdminStatus} == 1 ) {
163 # check only if interface type is not listed in %excluded
164 if (!defined $excluded{$ifStatus{$key}{$snmpIfType}} ) {
165 if ($ifStatus{$key}{$snmpIfOperStatus} == 1 ) { $ifup++ ;}
166 if ($ifStatus{$key}{$snmpIfOperStatus} == 2 ) {
168 if (defined $ifXTable) {
169 $ifmessage .= sprintf("%s: down -> %s<BR>",
170 $ifStatus{$key}{$snmpIfName},
171 $ifStatus{$key}{$snmpIfAlias});
173 $ifmessage .= sprintf("%s: down <BR>",
174 $ifStatus{$key}{$snmpIfDescr});
177 if ($ifStatus{$key}{$snmpIfOperStatus} == 5 ) { $ifdormant++ ;}
190 $answer = sprintf("host '%s', interfaces up: %d, down: %d, dormant: %d, excluded: %d, unused: %d<BR>",
197 $answer = $answer . $ifmessage . "\n";
201 $answer = sprintf("host '%s', interfaces up: %d, down: %d, dormant: %d, excluded: %d, unused: %d",
209 my $perfdata = sprintf("up=%d,down=%d,dormant=%d,excluded=%d,unused=%d",$ifup,$ifdown,$ifdormant,$ifexclude,$ifunused);
210 print ("$state: $answer |$perfdata\n");
211 exit $ERRORS{$state};
215 printf "\nMissing arguments!\n";
217 printf "check_ifstatus -C <READCOMMUNITY> -p <PORT> -H <HOSTNAME>\n";
218 printf "Copyright (C) 2000 Christoph Kron\n";
219 printf "Updates 5/2002 Subhendu Ghosh\n";
222 exit $ERRORS{"UNKNOWN"};
226 printf "check_ifstatus plugin for Nagios monitors operational \n";
227 printf "status of each network interface on the target host\n";
229 printf " -H (--hostname) Hostname to query - (required)\n";
230 printf " -C (--community) SNMP read community (defaults to public,\n";
231 printf " used with SNMP v1 and v2c\n";
232 printf " -v (--snmp_version) 1 for SNMP v1 (default)\n";
233 printf " 2 for SNMP v2c\n";
234 printf " SNMP v2c will use get_bulk for less overhead\n";
235 printf " 3 for SNMPv3 (requires -U option)";
236 printf " -p (--port) SNMP port (default 161)\n";
237 printf " -I (--ifmib) Agent supports IFMIB ifXTable. For Cisco - this will provide\n";
238 printf " the descriptive name. Do not use if you don't know what this is. \n";
239 printf " -x (--exclude) A comma separated list of ifType values that should be excluded \n";
240 printf " from the report (default for an empty list is PPP(23).\n";
241 printf " -u (--unused_ports) A comma separated list of ifIndex values that should be excluded \n";
242 printf " from the report (default is an empty exclusion list).\n";
243 printf " See the IANAifType-MIB for a list of interface types.\n";
244 printf " -L (--seclevel) choice of \"noAuthNoPriv\", \"authNoPriv\", or \"authPriv\"\n";
245 printf " -U (--secname) username for SNMPv3 context\n";
246 printf " -c (--context) SNMPv3 context name (default is empty string)";
247 printf " -A (--authpass) authentication password (cleartext ascii or localized key\n";
248 printf " in hex with 0x prefix generated by using \"snmpkey\" utility\n";
249 printf " auth password and authEngineID\n";
250 printf " -a (--authproto) Authentication protocol ( MD5 or SHA1)\n";
251 printf " -X (--privpass) privacy password (cleartext ascii or localized key\n";
252 printf " in hex with 0x prefix generated by using \"snmpkey\" utility\n";
253 printf " privacy password and authEngineID\n";
254 printf " -M (--maxmsgsize) Max message size - usefull only for v1 or v2c\n";
255 printf " -t (--timeout) seconds before the plugin times out (default=$TIMEOUT)\n";
256 printf " -V (--version) Plugin version\n";
257 printf " -h (--help) usage help \n\n";
258 print_revision($PROGNAME, '$Revision$');
262 sub process_arguments() {
263 $status = GetOptions(
264 "V" => \$opt_V, "version" => \$opt_V,
265 "h" => \$opt_h, "help" => \$opt_h,
266 "v=i" => \$snmp_version, "snmp_version=i" => \$snmp_version,
267 "C=s" => \$community,"community=s" => \$community,
268 "L=s" => \$seclevel, "seclevel=s" => \$seclevel,
269 "a=s" => \$authproto, "authproto=s" => \$authproto,
270 "U=s" => \$secname, "secname=s" => \$secname,
271 "A=s" => \$authpass, "authpass=s" => \$authpass,
272 "X=s" => \$privpass, "privpass=s" => \$privpass,
273 "c=s" => \$context, "context=s" => \$context,
274 "p=i" =>\$port, "port=i" => \$port,
275 "H=s" => \$hostname, "hostname=s" => \$hostname,
276 "I" => \$ifXTable, "ifmib" => \$ifXTable,
277 "x:s" => \$opt_x, "exclude:s" => \$opt_x,
278 "u=s" => \$opt_u, "unused_ports=s" => \$opt_u,
279 "M=i" => \$maxmsgsize, "maxmsgsize=i" => \$maxmsgsize,
280 "t=i" => \$timeout, "timeout=i" => \$timeout,
288 print_revision($PROGNAME,'$Revision$ ');
297 unless (defined $timeout) {
301 if ($snmp_version =~ /3/ ) {
302 # Must define a security level even though default is noAuthNoPriv
303 # v3 requires a security username
304 if (defined $seclevel && defined $secname) {
306 # Must define a security level even though defualt is noAuthNoPriv
307 unless ($seclevel eq ('noAuthNoPriv' || 'authNoPriv' || 'authPriv' ) ) {
309 exit $ERRORS{"UNKNOWN"};
312 # Authentication wanted
313 if ($seclevel eq ('authNoPriv' || 'authPriv') ) {
315 unless ($authproto eq ('MD5' || 'SHA1') ) {
317 exit $ERRORS{"UNKNOWN"};
320 if ( !defined $authpass) {
322 exit $ERRORS{"UNKNOWN"};
324 if ($authpass =~ /^0x/ ) {
325 $auth = "-authkey => $authpass" ;
327 $auth = "-authpassword => $authpass";
333 # Privacy (DES encryption) wanted
334 if ($seclevel eq 'authPriv' ) {
335 if (! defined $privpass) {
337 exit $ERRORS{"UNKNOWN"};
339 if ($privpass =~ /^0x/){
340 $priv = "-privkey => $privpass";
342 $priv = "-privpassword => $privpass";
347 # Context name defined or default
349 unless ( defined $context) {
357 exit $ERRORS{'UNKNOWN'}; ;
361 # for snmp v1 & v2c we default to community = "public"
363 # Excluded interfaces types (ifType) (backup interfaces, dial-on demand interfaces, PPP interfaces
364 if (defined $opt_x) {
365 my @x = split(/,/, $opt_x);
371 $excluded{23} = 1; # default PPP(23) if empty list - note (AIX seems to think PPP is 22 according to a post)
375 # Excluded interface ports (ifIndex) - management reasons
377 @unused_ports = split(/,/,$opt_u);
378 foreach $key (@unused_ports) {
379 $ifStatus{$key}{'notInUse'}++ ;
383 if (! utils::is_hostname($hostname)){
385 exit $ERRORS{"UNKNOWN"};
388 # create SNMP session handle based on options passed.
390 if ( ! $snmp_version ) {
393 if ( $snmp_version =~ /[12]/ ) {
395 ($session, $error) = Net::SNMP->session(
396 -hostname => $hostname,
397 -community => $community,
399 -version => $snmp_version,
400 -maxmsgsize => $maxmsgsize
403 if (!defined($session)) {
406 print ("$state: $answer");
407 exit $ERRORS{$state};
411 }elsif ( $snmp_version =~ /3/ ) {
413 if ($seclevel eq 'noAuthNoPriv') {
414 ($session, $error) = Net::SNMP->session(
415 -hostname => $hostname,
417 -version => $snmp_version,
418 -username => $secname,
421 }elsif ( $seclevel eq 'authNoPriv' ) {
422 ($session, $error) = Net::SNMP->session(
423 -hostname => $hostname,
425 -version => $snmp_version,
426 -username => $secname,
427 -authprotocol => $authproto,
430 }elsif ($seclevel eq 'authPriv' ) {
431 ($session, $error) = Net::SNMP->session(
432 -hostname => $hostname,
434 -version => $snmp_version,
435 -username => $secname,
436 -authprotocol => $authproto,
443 if (!defined($session)) {
446 print ("$state: $answer");
447 exit $ERRORS{$state};
452 print ("$state: No support for SNMP v$snmp_version yet\n");
453 exit $ERRORS{$state};
457 return $ERRORS{"OK"};