Hunter的大杂烩

May 29, 2012

linux下用snmp协议统计交换机端口流量脚本

Filed under: 技术话题 — hunter @ 11:44 am

#!/bin/sh
groupname=”yourgroup”
ipaddress=”192.168.0.1″
snmpwalk=”/usr/bin/snmpwalk”

export PATH=$PATH:/usr/bin:/bin
datestr=`date +”%Y/%m/%d %R”`
curtime=`date +%H%M`

#up link port
inf=your port no

data=(`/usr/bin/snmpget -v 2c -c ${groupname} ${ipaddress} ifOperStatus.${inf}  ifInOctets.${inf} ifInDiscards.${inf}  ifInErrors.${inf} ifInUcastPkts.${inf} ifInNUcastPkts.${inf}  ifOutOctets.${inf} ifOutDiscards.${inf} ifOutErrors.${inf} ifOutUcastPkts.${inf} ifOutNUcastPkts.${inf}| awk ‘{print $4}’`)
size=${#data[@]}

if [ $size -ne 11 ];then
echo “[” $datestr “}get error!”
exit;
fi

open=${data[0]}
inoctets=${data[1]}
outoctets=${data[6]}
inpkg=`expr ${data[4]} + ${data[5]}`
outpkg=`expr ${data[9]} + ${data[10]}`
inerr=`expr ${data[2]} + ${data[3]}`
outerr=`expr ${data[7]} + ${data[8]}`

old_inb=0
old_outb=0
old_inpkg=0
old_outpkg=0
old_inerr=0
old_outerr=0

if [ -f tmplog ];then
oldata=(`cat tmplog`)
old_inb=${oldata[0]};
old_outb=${oldata[1]};
old_inpkg=${oldata[2]};
old_outpkg=${oldata[3]};
old_inerr=${oldata[4]};
old_outerr=${oldata[5]};

#echo [$old_outb]
#echo [$old_inb]
fi

echo $inoctets ” “$outoctets ” “$inpkg ” “$outpkg ” “$inerr ” “$outerr > tmplog

diffinb=`expr $inoctets – $old_inb`
diffoutb=`expr $outoctets – $old_outb`
diffinpkg=`expr $inpkg – $old_inpkg`
diffoutpkg=`expr $outpkg – $old_outpkg`
diffinerr=`expr $inerr – $old_inerr`
diffouterr=`expr $outerr – $old_outerr`

if [ -f uplink.log ];then
sizeoffile=`filesize uplink.log`
if  [ $sizeoffile -gt 10000000 ];then
mv uplink.log uplink.log.old
echo “datetime, open, inbyte, outbyte, inpkg, outpkg, inerr, outerr” >> uplink.log
fi
fi
if [ $curtime  = “1111” ];then
echo “datetime, open, inbyte, outbyte, inpkg, outpkg, inerr, outerr” >> uplink.log
fi
echo  $datestr “,” $open  “,” $diffinb “, ” $diffoutb “, ” $diffinpkg “, ” $diffoutpkg”,”$diffinerr”,”$diffouterr >> uplink.log

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress