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";
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, $privproto, $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';
102 # Just in case of problems, let's not hang Nagios
104 print ("ERROR: No snmp response from $hostname (alarm timeout)\n");
105 exit $ERRORS{"UNKNOWN"};
110 $status = process_arguments();
120 ($session, $error) = Net::SNMP->session(%session_opts);
122 if (!defined($session)) {
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;
146 if ( ( $snmpoid =~ $snmpIfName ) && defined $ifXTable ) {
147 print ("$state: Device does not support ifTable - try without -I option\n");
149 print ("$state: $answer for $snmpoid with snmp version $snmp_version\n");
151 exit $ERRORS{$state};
154 foreach $snmpkey (keys %{$response}) {
155 $snmpkey =~ /.*\.(\d+)$/;
157 $ifStatus{$key}{$snmpoid} = $response->{$snmpkey};
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 ) {
178 if (defined $ifXTable) {
179 $ifmessage .= sprintf("%s: down -> %s<BR>",
180 $ifStatus{$key}{$snmpIfName},
181 $ifStatus{$key}{$snmpIfAlias});
183 $ifmessage .= sprintf("%s: down <BR>",
184 $ifStatus{$key}{$snmpIfDescr});
187 if ($ifStatus{$key}{$snmpIfOperStatus} == 5 ) { $ifdormant++ ;}
200 $answer = sprintf("host '%s', interfaces up: %d, down: %d, dormant: %d, excluded: %d, unused: %d<BR>",
207 $answer = $answer . $ifmessage . "\n";
211 $answer = sprintf("host '%s', interfaces up: %d, down: %d, dormant: %d, excluded: %d, unused: %d",
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};
226 exit $ERRORS{"UNKNOWN"};
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";
240 print_revision($PROGNAME, '@NP_VERSION@');
242 printf "check_ifstatus plugin for Nagios monitors operational \n";
243 printf "status of each network interface on the target host\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,
307 print_revision($PROGNAME,'@NP_VERSION@');
316 unless (defined $timeout) {
320 # Net::SNMP wants an integer
321 $snmp_version = 2 if $snmp_version eq "2c";
323 if ($snmp_version !~ /^[123]$/){
325 print ("$state: No support for SNMP v$snmp_version yet\n");
326 exit $ERRORS{$state};
330 -hostname => $hostname,
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");
359 if ($authpass =~ /^0x/ ) {
360 $session_opts{'-authkey'} = $authpass ;
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");
372 if ($privpass =~ /^0x/){
373 $session_opts{'-privkey'} = $privpass;
375 $session_opts{'-privpassword'} = $privpass;
379 $session_opts{'-privprotocol'} = $privproto if(defined $privproto);
382 # Context name defined or default
383 unless ( defined $context) {
388 usage
("Security level or name is not defined");
392 # Excluded interfaces types (ifType) (backup interfaces, dial-on demand interfaces, PPP interfaces
393 if (defined $opt_x) {
394 my @x = split(/,/, $opt_x);
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
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]/) {
420 print ("$state: No support for SNMP v$snmp_version yet\n");
421 exit $ERRORS{$state};
424 return $ERRORS{"OK"};