Code

check_mysql: Fix segfault with old slaves (#2696823 - Oskar Ahner)
[nagiosplug.git] / plugins-scripts / check_ifstatus.pl
1 #!/usr/local/bin/perl -w
2 #
3 # check_ifstatus.pl - nagios plugin 
4
5 #
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)
12 #
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.
17 #
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.
22 #
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.
26 #
27 #
28 # Report bugs to: ck@zet.net, nagiosplug-help@lists.sf.net
29
30 # 11.01.2000 Version 1.0
31 #
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 process_arguments ();
49 my $status;
50 my %ifOperStatus =      ('1','up',
51                          '2','down',
52                          '3','testing',
53                          '4','unknown',
54                          '5','dormant',
55                          '6','notPresent',
56                          '7','lowerLayerDown');  # down due to the state of lower layer interface(s));
58 my $timeout ;
59 my $state = "UNKNOWN";
60 my $answer = "";
61 my $snmpkey=0;
62 my $snmpoid=0;
63 my $key=0;
64 my $community = "public";
65 my $maxmsgsize = 1472 ; # Net::SNMP default is 1472
66 my ($seclevel, $authproto, $secname, $authpass, $privpass, $auth, $priv, $context);
67 my $port = 161;
68 my @snmpoids;
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';
76 my $hostname;
77 my $session;
78 my $error;
79 my $response;
80 my %ifStatus;
81 my $ifup =0 ;
82 my $ifdown =0;
83 my $ifdormant = 0;
84 my $ifexclude = 0 ;
85 my $ifunused = 0;
86 my $ifmessage = "";
87 my $snmp_version = 1;
88 my $ifXTable;
89 my $opt_h ;
90 my $opt_V ;
91 my $opt_u;
92 my $opt_x ;
93 my %excluded ;
94 my @unused_ports ;
100 # Just in case of problems, let's not hang Nagios
101 $SIG{'ALRM'} = sub {
102      print ("ERROR: No snmp response from $hostname (alarm timeout)\n");
103      exit $ERRORS{"UNKNOWN"};
104 };
107 #Option checking
108 $status = process_arguments();
110 if ($status != 0)
112         print_help() ;
113         exit $ERRORS{'OK'};
117 alarm($timeout);
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;
133       $session->close;
134       $state = 'CRITICAL';
135                         if ( ( $snmpoid =~ $snmpIfName ) && defined $ifXTable ) {
136                                 print ("$state: Device does not support ifTable - try without -I option\n");
137                         }else{
138                                 print ("$state: $answer for $snmpoid  with snmp version $snmp_version\n");
139                         }
140       exit $ERRORS{$state};
141    }
143    foreach $snmpkey (keys %{$response}) {
144       $snmpkey =~ /.*\.(\d+)$/;
145       $key = $1;
146       $ifStatus{$key}{$snmpoid} = $response->{$snmpkey};
147    }
151 $session->close;
153 alarm(0);
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 ) {
161     
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 ) {
166                                                                 $ifdown++ ;
167                                                                 if (defined $ifXTable) {
168                                                                         $ifmessage .= sprintf("%s: down -> %s<BR>",
169                                 $ifStatus{$key}{$snmpIfName},
170                                                                                                                                 $ifStatus{$key}{$snmpIfAlias});
171                                                                 }else{
172                                                                         $ifmessage .= sprintf("%s: down <BR>",
173                                                                                                                                 $ifStatus{$key}{$snmpIfDescr});
174                                                                 }
175                                 }
176                                 if ($ifStatus{$key}{$snmpIfOperStatus} == 5 ) { $ifdormant++ ;}
177                         }else{
178                                 $ifexclude++;
179                         }
180                 
181                 }
182         }else{
183                 $ifunused++;
184         }
187    if ($ifdown > 0) {
188       $state = 'CRITICAL';
189       $answer = sprintf("host '%s', interfaces up: %d, down: %d, dormant: %d, excluded: %d, unused: %d<BR>",
190                         $hostname,
191                         $ifup,
192                         $ifdown,
193                         $ifdormant,
194                         $ifexclude,
195                         $ifunused);
196       $answer = $answer . $ifmessage . "\n";
197    }
198    else {
199       $state = 'OK';
200       $answer = sprintf("host '%s', interfaces up: %d, down: %d, dormant: %d, excluded: %d, unused: %d",
201                         $hostname,
202                         $ifup,
203                         $ifdown,
204                         $ifdormant,
205                         $ifexclude,
206                         $ifunused);
207    }
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};
213 sub usage (){
214         printf "\nMissing arguments!\n";
215         printf "\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";
219         printf "\n\n";
220         support();
221         exit $ERRORS{"UNKNOWN"};
224 sub print_help (){
225         printf "check_ifstatus plugin for Nagios monitors operational \n";
226         printf "status of each network interface on the target host\n";
227         printf "\nUsage:\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@');
258         
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,
280                 );
281                 
282         if ($status == 0){
283                 print_help() ;
284                 exit $ERRORS{'OK'};
285         }
286         if ($opt_V) {
287                 print_revision($PROGNAME,'@NP_VERSION@');
288                 exit $ERRORS{'OK'};
289         }
291         if ($opt_h) {
292                 print_help();
293                 exit $ERRORS{'OK'};
294         }
296         unless (defined $timeout) {
297                 $timeout = $TIMEOUT;
298         }
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) {
304                 
305                         # Must define a security level even though defualt is noAuthNoPriv
306                         unless ($seclevel eq ('noAuthNoPriv' || 'authNoPriv' || 'authPriv' ) ) {
307                                 usage();
308                                 exit $ERRORS{"UNKNOWN"};
309                         }
310                         
311                         # Authentication wanted
312                         if ($seclevel eq ('authNoPriv' || 'authPriv') ) {
313                 
314                                 unless ($authproto eq ('MD5' || 'SHA1') ) {
315                                         usage();
316                                         exit $ERRORS{"UNKNOWN"};
317                                 }
319                                 if ( !defined $authpass) {
320                                         usage();
321                                         exit $ERRORS{"UNKNOWN"};
322                                 }else{
323                                         if ($authpass =~ /^0x/ ) {
324                                                 $auth = "-authkey => $authpass" ;
325                                         }else{
326                                                 $auth = "-authpassword => $authpass";
327                                         }
328                                 }
329                                         
330                         }
331                         
332                         # Privacy (DES encryption) wanted
333                         if ($seclevel eq  'authPriv' ) {
334                                 if (! defined $privpass) {
335                                         usage();
336                                         exit $ERRORS{"UNKNOWN"};
337                                 }else{
338                                         if ($privpass =~ /^0x/){
339                                                 $priv = "-privkey => $privpass";
340                                         }else{
341                                                 $priv = "-privpassword => $privpass";
342                                         }
343                                 }
344                         }
346                         # Context name defined or default
348                         unless ( defined $context) {
349                                 $context = "";
350                         }
351                 
352                 
353                 
354                 }else {
355                                         usage();
356                                         exit $ERRORS{'UNKNOWN'}; ;
357                 }
358         } # end snmpv3
360         # for snmp v1 & v2c we default to community = "public"
361         
362         # Excluded interfaces types (ifType) (backup interfaces, dial-on demand interfaces, PPP interfaces
363         if (defined $opt_x) {
364                 my @x = split(/,/, $opt_x);
365                 if ( @x) {
366                         foreach $key (@x){
367                                 $excluded{$key} = 1;
368                         }
369                 }else{
370                         $excluded{23} = 1; # default PPP(23) if empty list - note (AIX seems to think PPP is 22 according to a post)
371                 }
372         }
373         
374         # Excluded interface ports (ifIndex) - management reasons
375         if ($opt_u) {
376                 @unused_ports = split(/,/,$opt_u);
377                 foreach $key (@unused_ports) { 
378                         $ifStatus{$key}{'notInUse'}++ ;
379                 }
380         }
382         if (! utils::is_hostname($hostname)){
383                 usage();
384                 exit $ERRORS{"UNKNOWN"};
385         }
387         # create SNMP session handle based on options passed.
389         if ( ! $snmp_version ) {
390                 $snmp_version =1 ;
391         }else{
392                 if ( $snmp_version =~ /[12]/ ) {
393         
394                         ($session, $error) = Net::SNMP->session(
395                                                 -hostname  => $hostname,
396                                                 -community => $community,
397                                                 -port      => $port,
398                                                 -version        => $snmp_version,
399                                                 -maxmsgsize => $maxmsgsize
400                          );
402                 if (!defined($session)) {
403                                         $state='UNKNOWN';
404                                         $answer=$error;
405                                         print ("$state: $answer");
406                                         exit $ERRORS{$state};
407                 }
409                 
410         }elsif ( $snmp_version =~ /3/ ) {
412                 if ($seclevel eq 'noAuthNoPriv') {
413                         ($session, $error) = Net::SNMP->session(
414                                 -hostname  => $hostname,
415                                 -port      => $port,
416                                 -version  => $snmp_version,
417                                 -username => $secname,
418                         );
420                 }elsif ( $seclevel eq 'authNoPriv' ) {
421                         ($session, $error) = Net::SNMP->session(
422                                 -hostname  => $hostname,
423                                 -port      => $port,
424                                 -version  => $snmp_version,
425                                 -username => $secname,
426                                 -authprotocol => $authproto,
427                                 $auth
428                         );      
429                 }elsif ($seclevel eq 'authPriv' ) {
430                         ($session, $error) = Net::SNMP->session(
431                                 -hostname  => $hostname,
432                                 -port      => $port,
433                                 -version  => $snmp_version,
434                                 -username => $secname,
435                                 -authprotocol => $authproto,
436                                 $auth,
437                                 $priv
438                         );
439                 }
440                                         
441                                         
442                 if (!defined($session)) {
443                                         $state='UNKNOWN';
444                                         $answer=$error;
445                                         print ("$state: $answer");
446                                         exit $ERRORS{$state};
447                 }
448                         
449         }else{
450                 $state='UNKNOWN';
451                 print ("$state: No support for SNMP v$snmp_version yet\n");
452                 exit $ERRORS{$state};
453         }
456 return $ERRORS{"OK"};
457