【问题标题】:SNMP Network scriptSNMP 网络脚本
【发布时间】:2017-09-04 02:47:46
【问题描述】:

我正在考虑构建一个 SNMP 监控脚本,该脚本通过 SNMP 从交换机上的每个端口获取每个数据包值。

不幸的是,我的脚本似乎给了我变量所在的错误。

oldin-1=1:找不到命令

有谁知道将变量增加到每个端口号的更好方法是什么?

#Defining Variables
switch=192.168.0.4
firstport=1
lastport=26
sleeptime=5


function switchinfo {
for ((counter=$firstport; counter<=$lastport; counter++))
do
  sleep $sleeptime
#  echo "Working..."

  #Connect to Host
  in-$counter=`snmpget -v 2c -c public $switch IF-MIB::ifInOctets.$counter -Ov`
  out-$counter=`snmpget -v 2c -c public $switch IF-MIB::ifOutOctets.$counter -Ov`

  #Strip out the value from the string
  in-$counter=$(echo $in-$counter | cut -c 12-)
  out-$counter=$(echo $out-$counter | cut -c 12-)

  #Get the difference between the old and current
  diffin-$counter=$((in-$counter - oldin-$counter))
  diffout-$counter=$((out-$counter - oldout-$counter))

  inbps-$counter=$((diffin-$counter / sleeptime))
  outbps-$counter=$((diffout-$counter / sleeptime))

  #Basic Data Validation - Can't have values less than 0!
  if [[ $inbps-$counter -lt 0 || $outbps-$counter -lt 0 ]];
  then
      #There is an issue with one or more readings, get fresh ones
      #then wait for the next loop to calculate again.
      echo "We have a problem...moving to plan B"

      in=`snmpget -v 2c -c public $switch IF-MIB::ifInOctets.$counter -Ov`
      out=`snmpget -v 2c -c public $switch IF-MIB::ifOutOctets.$counter -Ov`

      #Strip out the value from the string
      in-$counter=$(echo $in-$counter | cut -c 12-)
      out-$counter=$(echo $out-$counter | cut -c 12-)

  else
      #Output the current traffic
      echo "Main current inbound traffic for Port $counter: $inbps-$counter bps"
      echo "Main current outbound traffic for Port $counter: $outbps-$counter bps"

      #Move the current variables to the old ones
      oldin-$counter=$in-$counter
      oldout-$counter=$out-$counter

  fi

done
}

echo "Press [CTRL+C] to stop..."
while :
do
    switchinfo
done

【问题讨论】:

    标签: bash networking monitoring bash4


    【解决方案1】:

    更好的方法是使用数组,而不是单独的变量。使用端口号作为数组索引。

    #Defining Variables
    switch=192.168.0.4
    firstport=1
    lastport=26
    sleeptime=5
    in=()
    out=()
    diffin=()
    diffout=()
    inbps=()
    outbps=()
    
    function switchinfo {
    for ((counter=$firstport; counter<=$lastport; counter++))
    do
      sleep $sleeptime
    #  echo "Working..."
    
    #Connect to Host
      in[$counter]=`snmpget -v 2c -c public $switch IF-MIB::ifInOctets.$counter -Ov`
      out[$counter]=`snmpget -v 2c -c public $switch IF-MIB::ifOutOctets.$counter -Ov`
    
    #Strip out the value from the string
    in[$counter]=$(echo ${in[$counter]} | cut -c 12-)
    out[$counter]=$(echo ${out[$counter]} | cut -c 12-)
    
    #Get the difference between the old and current
    diffin[$counter]=$((${in[$counter]} - ${oldin[$counter]}))
    diffout[$counter]=$((${out[$counter]} - ${oldout[$counter]}))
    
    inbps[$counter]=$((${diffin[$counter]} / sleeptime))
    outbps[$counter]=$((${diffout[$counter]} / sleeptime))
    
    #Basic Data Validation - Can't have values less than 0!
    if [[ ${inbps[$counter]} -lt 0 || ${outbps[$counter]} -lt 0 ]];
    then
      #There is an issue with one or more readings, get fresh ones
      #then wait for the next loop to calculate again.
      echo "We have a problem...moving to plan B"
    
      in[$counter]=`snmpget -v 2c -c public $switch IF-MIB::ifInOctets.$counter -Ov`
      out[$counter]=`snmpget -v 2c -c public $switch IF-MIB::ifOutOctets.$counter -Ov`
    
      #Strip out the value from the string
      in[$counter]=$(echo ${in[$counter]} | cut -c 12-)
      out[$counter]=$(echo ${out[$counter]} | cut -c 12-)
    
    else
      #Output the current traffic
      echo "Main current inbound traffic for Port $counter: $inbps-$counter bps"
      echo "Main current outbound traffic for Port $counter: $outbps-$counter bps"
    
      #Move the current variables to the old ones
      oldin[$counter]=${in[$counter]}
      oldout[$counter]=${out[$counter]}
    
    fi
    
    done
    }
    
    echo "Press [CTRL+C] to stop..."
    while :
    do
        switchinfo
    done
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多