[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/se3master/usr/share/se3/scripts-alertes/ -> check_ifstatus (source)

   1  #! /usr/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  #
  32  # $Id: check_ifstatus.pl,v 1.9 2004/08/18 19:51:35 tonvoon Exp $
  33  
  34  use POSIX;
  35  use strict;
  36  use lib "/usr/lib/nagios/plugins" ;
  37  use utils qw($TIMEOUT %ERRORS &print_revision &support);
  38  
  39  use Net::SNMP;
  40  use Getopt::Long;
  41  Getopt::Long::Configure('bundling');
  42  
  43  my $PROGNAME = "check_ifstatus";
  44  
  45  sub print_help ();
  46  sub usage ();
  47  sub process_arguments ();
  48  
  49  
  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));
  58  
  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, $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  
  97  
  98  
  99  
 100  
 101  # Just in case of problems, let's not hang Nagios
 102  $SIG{'ALRM'} = sub {
 103       print ("ERROR: No snmp response from $hostname (alarm timeout)\n");
 104       exit $ERRORS{"UNKNOWN"};
 105  };
 106  
 107  
 108  #Option checking
 109  $status = process_arguments();
 110  
 111  if ($status != 0)
 112  {
 113      print_help() ;
 114      exit $ERRORS{'OK'};
 115  }
 116  
 117  
 118  alarm($timeout);
 119  
 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);
 126  
 127  
 128  
 129  
 130  foreach $snmpoid (@snmpoids) {
 131  
 132     if (!defined($response = $session->get_table($snmpoid))) {
 133        $answer=$session->error;
 134        $session->close;
 135        $state = 'CRITICAL';
 136              if ( ( $snmpoid =~ $snmpIfName ) && defined $ifXTable ) {
 137                  print ("$state: Device does not support ifTable - try without -I option\n");
 138              }else{
 139                  print ("$state: $answer for $snmpoid  with snmp version $snmp_version\n");
 140              }
 141        exit $ERRORS{$state};
 142     }
 143  
 144     foreach $snmpkey (keys %{$response}) {
 145        $snmpkey =~ /.*\.(\d+)$/;
 146        $key = $1;
 147        $ifStatus{$key}{$snmpoid} = $response->{$snmpkey};
 148     }
 149  }
 150  
 151  
 152  $session->close;
 153  
 154  alarm(0);
 155  
 156  foreach $key (keys %ifStatus) {
 157  
 158      # skip unused interfaces
 159      if (!defined($ifStatus{$key}{'notInUse'})) {
 160          # check only if interface is administratively up
 161      if ($ifStatus{$key}{$snmpIfAdminStatus} == 1 ) {
 162      
 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 ) {
 167                                  $ifdown++ ;
 168                                  if (defined $ifXTable) {
 169                                      $ifmessage .= sprintf("%s: down -> %s<BR>",
 170                                  $ifStatus{$key}{$snmpIfName},
 171                                                                  $ifStatus{$key}{$snmpIfAlias});
 172                                  }else{
 173                                      $ifmessage .= sprintf("%s: down <BR>",
 174                                                                  $ifStatus{$key}{$snmpIfDescr});
 175                                  }
 176                  }
 177                  if ($ifStatus{$key}{$snmpIfOperStatus} == 5 ) { $ifdormant++ ;}
 178              }else{
 179                  $ifexclude++;
 180              }
 181          
 182          }
 183      }else{
 184          $ifunused++;
 185      }
 186  }
 187  
 188     if ($ifdown > 0) {
 189        $state = 'CRITICAL';
 190        $answer = sprintf("host '%s', interfaces up: %d, down: %d, dormant: %d, excluded: %d, unused: %d<BR>",
 191                          $hostname,
 192              $ifup,
 193              $ifdown,
 194              $ifdormant,
 195              $ifexclude,
 196              $ifunused);
 197        $answer = $answer . $ifmessage . "\n";
 198     }
 199     else {
 200        $state = 'OK';
 201        $answer = sprintf("host '%s', interfaces up: %d, down: %d, dormant: %d, excluded: %d, unused: %d",
 202                          $hostname,
 203              $ifup,
 204              $ifdown,
 205              $ifdormant,
 206              $ifexclude,
 207              $ifunused);
 208     }
 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};
 212  
 213  
 214  sub usage (){
 215      printf "\nMissing arguments!\n";
 216      printf "\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";
 220      printf "\n\n";
 221      support();
 222      exit $ERRORS{"UNKNOWN"};
 223  }
 224  
 225  sub print_help (){
 226      printf "check_ifstatus plugin for Nagios monitors operational \n";
 227      printf "status of each network interface on the target host\n";
 228      printf "\nUsage:\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: 1.9 $');
 259      
 260  }
 261  
 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,
 281          );
 282          
 283      if ($status == 0){
 284          print_help() ;
 285          exit $ERRORS{'OK'};
 286      }
 287      if ($opt_V) {
 288          print_revision($PROGNAME,'$Revision: 1.9 $ ');
 289          exit $ERRORS{'OK'};
 290      }
 291  
 292      if ($opt_h) {
 293          print_help();
 294          exit $ERRORS{'OK'};
 295      }
 296  
 297      unless (defined $timeout) {
 298          $timeout = $TIMEOUT;
 299      }
 300  
 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) {
 305          
 306              # Must define a security level even though defualt is noAuthNoPriv
 307              unless ($seclevel eq ('noAuthNoPriv' || 'authNoPriv' || 'authPriv' ) ) {
 308                  usage();
 309                  exit $ERRORS{"UNKNOWN"};
 310              }
 311              
 312              # Authentication wanted
 313              if ($seclevel eq ('authNoPriv' || 'authPriv') ) {
 314          
 315                  unless ($authproto eq ('MD5' || 'SHA1') ) {
 316                      usage();
 317                      exit $ERRORS{"UNKNOWN"};
 318                  }
 319  
 320                  if ( !defined $authpass) {
 321                      usage();
 322                      exit $ERRORS{"UNKNOWN"};
 323                  }else{
 324                      if ($authpass =~ /^0x/ ) {
 325                          $auth = "-authkey => $authpass" ;
 326                      }else{
 327                          $auth = "-authpassword => $authpass";
 328                      }
 329                  }
 330                      
 331              }
 332              
 333              # Privacy (DES encryption) wanted
 334              if ($seclevel eq  'authPriv' ) {
 335                  if (! defined $privpass) {
 336                      usage();
 337                      exit $ERRORS{"UNKNOWN"};
 338                  }else{
 339                      if ($privpass =~ /^0x/){
 340                          $priv = "-privkey => $privpass";
 341                      }else{
 342                          $priv = "-privpassword => $privpass";
 343                      }
 344                  }
 345              }
 346  
 347              # Context name defined or default
 348  
 349              unless ( defined $context) {
 350                  $context = "";
 351              }
 352          
 353          
 354          
 355          }else {
 356                      usage();
 357                      exit $ERRORS{'UNKNOWN'}; ;
 358          }
 359      } # end snmpv3
 360  
 361      # for snmp v1 & v2c we default to community = "public"
 362      
 363      # Excluded interfaces types (ifType) (backup interfaces, dial-on demand interfaces, PPP interfaces
 364      if (defined $opt_x) {
 365          my @x = split(/,/, $opt_x);
 366          if ( @x) {
 367              foreach $key (@x){
 368                  $excluded{$key} = 1;
 369              }
 370          }else{
 371              $excluded{23} = 1; # default PPP(23) if empty list - note (AIX seems to think PPP is 22 according to a post)
 372          }
 373      }
 374      
 375      # Excluded interface ports (ifIndex) - management reasons
 376      if ($opt_u) {
 377          @unused_ports = split(/,/,$opt_u);
 378          foreach $key (@unused_ports) { 
 379              $ifStatus{$key}{'notInUse'}++ ;
 380          }
 381      }
 382  
 383      if (! utils::is_hostname($hostname)){
 384          usage();
 385          exit $ERRORS{"UNKNOWN"};
 386      }
 387  
 388      # create SNMP session handle based on options passed.
 389  
 390      if ( ! $snmp_version ) {
 391          $snmp_version =1 ;
 392      }else{
 393          if ( $snmp_version =~ /[12]/ ) {
 394      
 395              ($session, $error) = Net::SNMP->session(
 396                          -hostname  => $hostname,
 397                          -community => $community,
 398                          -port      => $port,
 399                          -version    => $snmp_version,
 400                          -maxmsgsize => $maxmsgsize
 401               );
 402  
 403          if (!defined($session)) {
 404                      $state='UNKNOWN';
 405                      $answer=$error;
 406                      print ("$state: $answer");
 407                      exit $ERRORS{$state};
 408          }
 409  
 410          
 411      }elsif ( $snmp_version =~ /3/ ) {
 412  
 413          if ($seclevel eq 'noAuthNoPriv') {
 414              ($session, $error) = Net::SNMP->session(
 415                  -hostname  => $hostname,
 416                  -port      => $port,
 417                  -version  => $snmp_version,
 418                  -username => $secname,
 419              );
 420  
 421          }elsif ( $seclevel eq 'authNoPriv' ) {
 422              ($session, $error) = Net::SNMP->session(
 423                  -hostname  => $hostname,
 424                  -port      => $port,
 425                  -version  => $snmp_version,
 426                  -username => $secname,
 427                  -authprotocol => $authproto,
 428                  $auth
 429              );    
 430          }elsif ($seclevel eq 'authPriv' ) {
 431              ($session, $error) = Net::SNMP->session(
 432                  -hostname  => $hostname,
 433                  -port      => $port,
 434                  -version  => $snmp_version,
 435                  -username => $secname,
 436                  -authprotocol => $authproto,
 437                  $auth,
 438                  $priv
 439              );
 440          }
 441                      
 442                      
 443          if (!defined($session)) {
 444                      $state='UNKNOWN';
 445                      $answer=$error;
 446                      print ("$state: $answer");
 447                      exit $ERRORS{$state};
 448          }
 449              
 450      }else{
 451          $state='UNKNOWN';
 452          print ("$state: No support for SNMP v$snmp_version yet\n");
 453          exit $ERRORS{$state};
 454      }
 455  }
 456  
 457  return $ERRORS{"OK"};
 458                  
 459  }


Generated: Tue Mar 17 22:47:18 2015 Cross-referenced by PHPXref 0.7.1