nagios通过snmp监控网卡流量(通过IP识别)脚本

2631阅读 0评论2017-03-19 guofs123
分类:网络与安全

# /usr/local/nagios/libexec/check_snmp_iftraffic_by_ip  -H 10.1.8.251 -C public -l
.1.3.6.1.2.1.4.20.1.2.10.1.8.251 = INTEGER: 11

# /usr/local/nagios/libexec/check_snmp_iftraffic_by_ip  -H 10.1.8.251 -C public
Status is OK - Interface UP,TotalRate:1000Mbps,CurrentRate(In/Out):817.3bps/1154.2bps,Warning:90%,Critical:95% | In_traffic=817.3bps;943718400;996147200 Out_traffic=1154.2bps;943718400;996147200

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

Usage: check_snmp_iftraffic_by_ip -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
                 -r             Interface rate(unit:Mbps)
                 -h or -v       Help


define command{
        command_name    check_ifrate_by_ip
        command_line    $USER1$/check_snmp_iftraffic_by_ip -H $HOSTADDRESS$ -C $ARG1$ -r $ARG2$ -w $ARG3$ -c $ARG4$
        }


define service{
        use     local-service
        host_name       MT
        service_description  eth2_rate_ip
        check_command   check_ifrate_by_ip!public!1000!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 $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: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} ;
   #$string    = $opt{s} if defined $opt{s} ;
   $string    = $ipaddress ;
   $TotalSpeed = $opt{r} if defined $opt{r} ;
   my $cmd_str;
   my $return;

   if($opt{l})
   {
     $cmd_str = "/usr/bin/snmpwalk -v 2c $ipaddress -c $community 1.3.6.1.2.1.4.20.1.2.$string -On";
     $return = `$cmd_str`;
     chomp $return ;  
     print $return,"\n";
     $list_yes = 0;
   }
   else
   {
     $cmd_str = "/usr/bin/snmpwalk -v 2c $ipaddress -c $community 1.3.6.1.2.1.4.20.1.2.$string | cut -f2 -d'=' | cut -f3 -d' '";
     $return = `$cmd_str`;
     chomp $return ;
     #print "Index:",$return,"\n";
     $PortID = $return ;
     $list_yes = 1;
   }
 
   #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 ($list_yes == 1 )
   {

      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
   -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);
}


上一篇:nagios通过snmp监控进程脚本。
下一篇:nagios通过snmp监控网卡流量(通过接口名称识别)脚本