Chinaunix首页 | 论坛 | 博客
  • 博客访问: 184701
  • 博文数量: 16
  • 博客积分: 170
  • 博客等级: 入伍新兵
  • 技术积分: 753
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-11 10:57
文章分类

全部博文(16)

文章存档

2017年(3)

2016年(2)

2015年(3)

2014年(1)

2013年(2)

2012年(5)

我的朋友

分类: 网络与安全

2017-03-19 11:35:29

# /usr/local/nagios/libexec/check_snmp_iftraffic_by_ifname -h
--------------------------------------------------------------------
check_snmp_iftraffic_by_ifname v0.1

Usage: check_snmp_iftraffic_by_ifname -H -C [...]
Options:         -H             Hostname or IP address
                 -p             snmp port(default is tcp161)
                 -v             snmp Version(default is v2c)
                 -C             Community (default is public)
                 -w             Warning threshold
                 -c             Critical threshold
                 -l             List snmp value
                 -s             Match interface name string
                 -r             Interface rate(unit:Mbps)
                 -h or -v       Help

--------------------------------------------------------------------
Copyright 2017   Limited

This program is free software; you can redistribute it or modify
it under the terms of the GNU General Public License
--------------------------------------------------------------------


# /usr/local/nagios/libexec/check_snmp_iftraffic_by_ifname -H 10.1.1.254 -C public -l
1.3.6.1.2.1.2.2.1.2.10001 = OCTET STRING: FastEthernet1/0/1
1.3.6.1.2.1.2.2.1.2.10002 = OCTET STRING: FastEthernet1/0/2
1.3.6.1.2.1.2.2.1.2.10003 = OCTET STRING: FastEthernet1/0/3
1.3.6.1.2.1.2.2.1.2.10004 = OCTET STRING: FastEthernet1/0/4
1.3.6.1.2.1.2.2.1.2.10005 = OCTET STRING: FastEthernet1/0/5
1.3.6.1.2.1.2.2.1.2.10006 = OCTET STRING: FastEthernet1/0/6
1.3.6.1.2.1.2.2.1.2.10007 = OCTET STRING: FastEthernet1/0/7


# /usr/local/nagios/libexec/check_snmp_iftraffic_by_ifname -H 10.1.1.254 -C public -s 'FastEthernet1/0/1'
Status is OK - Interface UP,TotalRate:1000Mbps,CurrentRate(In/Out):46623.0bps/82348.0bps,Warning:90%,Critical:95% | In_traffic=46623.0bps;943718400;996147200 Out_traffic=82348.0bps;943718400;996147200


define command{
        command_name    check_ifrate_by_ifname
        command_line    $USER1$/check_snmp_iftraffic_by_ifname -H $HOSTADDRESS$ -C $ARG1$ -r $ARG2$ -s $ARG3$ -w $ARG4$ -c $ARG5$
        }

define service{
        use                     core-Service
        host_name               sz-cisco2960-64.130
        icon_image              globe.png
        service_description     FastEthernet1/0/1
        check_command           check_ifrate_by_ifname!public!50!'FastEthernet1/0/1'!80!90
}


详细代码如下:
--------------------------------------------------------------------------------------------------
#!/usr/bin/perl
# Version : 0.1
# Date :  2017-02-28
# Author  : Darry Guo
# Help :
# Licence : GPL -

use strict;
use Net::SNMP qw{ :asn1 :snmp :translate };
use Getopt::Std;


my $script = GetScriptName($0);
my $script_version = "0.1";


my $ipaddress = "127.0.0.1";
my $port = 161;
my $version = "snmpv2c";
my $community = "public";
my $warning = 90;
my $critical = 95;
my $vMAX = 100;
my $timeout = 2;
my $status = 0;
my $returnstring = "";
my $temp = 5;
my $OidBase = '1.3.6.1.2.1.2.2.1.2';
my $vOID_in = '1.3.6.1.2.1.2.2.1.10';
my $vOID_out ='1.3.6.1.2.1.2.2.1.16';
my $string = "test";
my $list_yes = 0;
my @arry_value = ();
my $PortID = -1;
my $TotalSpeed = 1000;
if (@ARGV < 1) {
     print "Too few arguments\n";
     usage();
}

my %opt=();
getopts("hvlH:C:c:w:s:r:",\%opt);

if($opt{h} || $opt{v})
{
    usage();
    exit(0);
}

if($opt{l})
{
 $list_yes = 1;
}

main();


####################################################################
# sub program                                                      #
####################################################################
sub main()
{
    #Create the SNMP session
    $ipaddress = $opt{H} if defined $opt{H} ;
    $community = $opt{C} if defined $opt{C} ;
    $warning   = $opt{w} if defined $opt{w} ;
    $critical  = $opt{c} if defined $opt{c} ;
    #$OidBase   = $opt{o} if defined $opt{o} ;
    $string    = $opt{s} if defined $opt{s} ;
    $OidBase = '1.3.6.1.2.1.2.2.1.2';
    $TotalSpeed = $opt{r} if defined $opt{r} ;

    @arry_value = GetOIDbyString($ipaddress, $community, $port, $version, $timeout, $OidBase, $string, $list_yes);   
    #if not list mode,then it's plug output
    if ($arry_value[0] ne 'LIST_OID' )
    {
      my $len = @arry_value ;

      if ($len == 0 )
      {
        #print "Not find : $string . \n";
        $status = 3;
        $returnstring = "Not find : $string .";
      }
      else
      {
        if ($len == 1 )
 {
   #printf("OID : %s, value : %s, index: %i \n",$arry_value[0]->[0],$arry_value[0]->[1],$arry_value[0]->[2]);
   $PortID = $arry_value[0]->[2];
 }
 else
 {
          #foreach (@arry_value)
          #{
          #  printf("OID : %s, value : %s, index: %i \n",$_->[0],$_->[1],$_->[2]);
          #}
   #print "Soryy,string($string} is not the only one, total $len records.\n";
          $status = 3;
          $returnstring = "Soryy,string($string} is not the only one, total $len records.";
 }
      }
    }

   #if get $Port_ID true
   if ($PortID != -1)
   {
     my $TotalRateByte = $TotalSpeed * 1024 * 1024 / 8;
     my $Byte_w_value = $TotalRateByte * $warning / 100 ;
     my $Byte_c_value = $TotalRateByte * $critical / 100 ;

     #check interface status:up or down
     my $cmd_str="/usr/bin/snmpwalk -v 2c $ipaddress -c $community 1.3.6.1.2.1.2.2.1.8.$PortID | cut -f2 -d'(' | cut -f1 -d')'";
     my $return=`$cmd_str`;
     chomp $return;
     if ($return == 1)
     {
       #interface up

       #get rate
       my $cmd_str="/usr/local/nagios/libexec/check_snmp -H $ipaddress -P 2c -C $community -o $vOID_in.$PortID -w 0:$Byte_w_value -c 0:$Byte_c_value -l 'In' -o $vOID_out.$PortID -w 0:$Byte_w_value -c 0:$Byte_c_value -l 'Out' -D ',' --rate --rate-multiplier 1";
       my $return=`$cmd_str`;
       chomp $return;

       if ($return eq 'No previous data to calculate rate - assume okay')
       {
         #first get value
         $status = 1;
         $returnstring = $return;
       }
       else
       {
         #get value
         #print "\nok!\n";
         #print "$cmd_str\n";
         #print $return,"\n";
         #print "----------------------\n";
         my $positionIn = index($return,"In=");
         #print "In:$positionIn\n";
         my $positionOut = index($return,"Out=");
         #print "Out:$positionOut\n";

         #print "\n\n";
         my $in_length = $positionOut - $positionIn - 3 - 1;
         my $in_number = substr($return,$positionIn+3,$in_length);
         my $in_rate = $in_number * 8;
         my $in_rate_value=sprintf("%.1f",$in_number * 8);
         #print "In_rate:$in_number,$in_rate,$in_rate_value\n";
         my $str_length = length($return);
         my $out_length = $str_length - $positionOut - 3 - 2;
         my $out_number = substr($return,$positionOut+4,$out_length);
         my $out_rate = $out_number * 8;
         my $out_rate_value=sprintf("%.1f",$out_number * 8);
         #print "Out_rate:$out_number,$out_rate,$out_rate_value\n";

         my $bit_w = $TotalSpeed * 1024 * 1024 * $warning / 100 ;
         my $bit_c = $TotalSpeed * 1024 * 1024 * $critical / 100 ;

         my $in_str1 = "$in_rate_value";
         my $out_str1 = "$out_rate_value";

         if (($in_rate_value < $bit_w) && ($out_rate_value < $bit_w))
         {
            $status = 0;
         }

         if ( (($in_rate_value >= $bit_w) && ($in_rate_value < $bit_c))  ||  (($out_rate_value >= $bit_w) && ($out_rate_value < $bit_c))  )
         {
           $status = 1;
           if (($in_rate_value >= $bit_w) && ($in_rate_value < $bit_c))
           {
               $in_str1 = "*$in_rate_value*";
           }
           if (($out_rate_value >= $bit_w) && ($out_rate_value < $bit_c))
           {
               $out_str1 = "*$out_rate_value*";
           }
         }

         if ( ($in_rate_value >= $bit_c)  ||  ($out_rate_value >= $bit_c) )
         {
           $status = 2;
           if ($in_rate_value >= $bit_c)
           {
               $in_str1 = "*$in_rate_value*";
           }
           if ($out_rate_value >= $bit_c)
           {
               $out_str1 = "*$out_rate_value*";
           }
         }

         #set return value
         $returnstring = "Interface UP,TotalRate:" . $TotalSpeed . "Mbps,CurrentRate(In/Out):" . $in_str1 . "bps/" . $out_str1 . "bps,Warning:$warning%,Critical:$critical% | In_traffic=$in_rate_value"."bps".";$bit_w;$bit_c Out_traffic=$out_rate_value"."bps".";$bit_w;$bit_c";

       }


     }
     else
     {
       #interface down
       $status = 2;
       $returnstring = "This interface is DOWN";
    }


   }

   #if not list mode,then it's plug output
   if ($arry_value[0] ne 'LIST_OID' )
   {

      if ($returnstring eq ""){
        $status = 3;
      }

      if ($status == 0){
        print "Status is OK - $returnstring\n";
      }
      elsif ($status == 1){
        print "Status is a WARNING level - $returnstring\n";
      }
      elsif ($status == 2){
         print "Status is CRITICAL - $returnstring\n";
      }
      else{
        print "Status is UNKNOWN - $returnstring\n";
      }

      exit $status;
   }

}


sub usage {
    print << "USAGE";
-------------------------------------------------------------------- 
$script v$script_version

Usage: $script -H -C [...]
Options:         -H   Hostname or IP address
                 -p             snmp port(default is tcp161)
                 -v             snmp Version(default is v2c)
                 -C   Community (default is public)
                 -w             Warning threshold
                 -c             Critical threshold
                 -l             List snmp value
                 -s  Match interface name string
   -r  Interface rate(unit:Mbps)
                 -h or -v       Help                

-------------------------------------------------------------------- 
Copyright 2017   Limited 
 
This program is free software; you can redistribute it or modify
it under the terms of the GNU General Public License
--------------------------------------------------------------------

USAGE
     exit 1;
}


sub GetScriptName
{
   my @str = @_;
   my $len = rindex($str[0],"/");
   return  substr($str[0],$len + 1);
}

sub GetOIDbyString
{
 #USAGE
 #   GetOIDbyString($ipaddress, $community, $port, $version, $timeout, $OidBase, $string, $list_yes)
 #
 #Return value
 #Format 1:
 #   @arry =(
 #          [oid_1,value1,index1],
 #          [oid_2,value2,index2],
 #          ...,
 #          [oid_N,valueN,indexN]
 #          )
 #   return @arry
 #example
 #  #./get_oid_by_string_new_v2 -H 10.1.8.251 -C aspireeip -o 1.3.6.1.2.1.25.4.2.1.2 -s 'svchost.exe'
 #  OID : 1.3.6.1.2.1.25.4.2.1.2.248, value : svchost.exe, index: 248
 #  OID : 1.3.6.1.2.1.25.4.2.1.2.608, value : svchost.exe, index: 608
 #  OID : 1.3.6.1.2.1.25.4.2.1.2.688, value : svchost.exe, index: 688
 #  #./get_oid_by_string_new_v2 -H 10.1.1.254 -C aspiretest -o  1.3.6.1.2.1.2.2.1.2 -s 'FastEthernet1/0/48'
 #  OID : 1.3.6.1.2.1.2.2.1.2.10048, value : FastEthernet1/0/48, index: 10048
 #  #./get_oid_by_string_new_v2 -H 10.1.8.251 -C aspireeip -o 1.3.6.1.2.1.25.4.2.1.2 -l
 #  1.3.6.1.2.1.25.4.2.1.2.1 = OCTET STRING: System Idle Process
 #  1.3.6.1.2.1.25.4.2.1.2.4 = OCTET STRING: System
 #  1.3.6.1.2.1.25.4.2.1.2.216 = OCTET STRING: smss.exe
 #  1.3.6.1.2.1.25.4.2.1.2.248 = OCTET STRING: svchost.exe
 #  1.3.6.1.2.1.25.4.2.1.2.300 = OCTET STRING: csrss.exe
 #  1.3.6.1.2.1.25.4.2.1.2.352 = OCTET STRING: csrss.exe
 #
 #Format 2:
 #   return 'LIST_OID'
 #
 my $ipaddress = $_[0];
 my $community = $_[1];
 my $port = $_[2];
 my $version = $_[3];
 my $timeout = $_[4];
 my $OidBase = $_[5];
 my $string     = $_[6];
 my $list_yes = $_[7];
 my @return_arry  = ();
 my @arry_id  = ();

 my ($session, $error) = Net::SNMP->session(
         -community  =>  $community,
         -hostname   =>  $ipaddress,
         -port       =>  $port,
         -version    =>  $version,
         -timeout    =>  $timeout
 );

 my $result = $session->get_table(-baseoid =>$OidBase);
 $session->snmp_dispatcher();

 for ($session->var_bind_names())
 {
   #my $type = snmp_type_ntop($session->var_bind_types()->{$_});
   #my $value = $session->var_bind_list()->{$_};
   #my $return = "$_,$type,$value \n";
   #print $return;
   #printf("%s = %s: %s\n",$_,snmp_type_ntop($session->var_bind_types()->{$_}),$session->var_bind_list()->{$_}) if ($opt{l});
  
   if ($list_yes)
   {   #list all OID value
       printf("%s = %s: %s\n",$_,snmp_type_ntop($session->var_bind_types()->{$_}),$session->var_bind_list()->{$_});
   }
   else
   {
      if ( $string eq $session->var_bind_list()->{$_} )
      {
         #printf("%s = %s: %s  ok \n",$_,snmp_type_ntop($session->var_bind_types()->{$_}),$session->var_bind_list()->{$_});
         = ($_,$session->var_bind_list()->{$_});
         #$length = length($_);
         #$ID_in_oid = substr($_,$length-1,1);
         @arry_id = split(/\./,$_);
         push (@return_arry,[$_,$session->var_bind_list()->{$_},$arry_id[-1]]);
      }
   }
 }
 #Close the session
 $session->close();

 #return
 if ($list_yes == 0)
 {
   return @return_arry;
 }
 else
 {
   return "LIST_OID";
 }

}


阅读(2699) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~