簡體   English   中英

SNMP網絡腳本

[英]SNMP Network script

我正在研究構建一個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

更好的方法是使用數組,而不是單獨的變量。 使用端口號作為數組索引。

#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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM