Definition read the file:
[root@RHEL5 ~]# vi asptools/Report_Host
Eth__0/0:30.30.30.1,Firewall,ifInOctets.1 Eth__0/1:30.30.30.1,Firewall,ifInOctets.2 Fast_0/0:172.17.25.250,CTP2,ifInOctets.1 Ser0/0/0:172.17.24.250,HYQH.BJ,ifInOctets.7 Ser0/0/1:172.17.24.250,HYQH.BJ,ifInOctets.8 Fa___0/1:172.17.21.250,SH,ifInOctets.2 Ser0/0/0:172.17.21.250,SH,ifInOctets.11 Ser0/0/1:172.17.21.250,SH,ifInOctets.12 Ser0/1/1:172.17.21.250,SH,ifInOctets.14 Ser0/2/0:172.17.21.250,SH,ifInOctets.15 Ser0/2/1:172.17.21.250,SH,ifInOctets.16 Ser0/0/0:172.17.20.250,ZQ,ifInOctets.7 Ser0/0/1:172.17.20.250,ZQ,ifInOctets.8 Gi___0/1:172.17.22.250,ShuangLong,ifInOctets.3
Defined monitoring script:
[root@RHEL5 ~]# vi asptools/Report_traffic.sh
#!/bin/sh # Author: Kingcraft,20131104 Flow_Monitor() { for line in $(<$Report_File) do Ip=`echo $line | awk -F: '{print $2}' | awk -F, '{print $1}'` Comm=`echo $line | awk -F: '{print $2}' | awk -F, '{print $2}'` Int=`echo $line | awk -F: '{print $2}' | awk -F, '{print $3}'` Int_name=`echo $line | awk -F: '{print $1}'` COMMAND=`echo "snmpwalk -v 2c $Ip -c $Comm $Int"` Counter1=`$COMMAND` 2>/dev/null Traffic_a=`echo $Counter1 |awk -F: '{print $4}'` 2>/dev/null sleep 1 Counter2=`$COMMAND` 2>/dev/null Traffic_b=`echo $Counter2 |awk -F: '{print $4}'` 2>/dev/null if [[ $Traffic_a -gt $Traffic_b ]] then Traffic1=$(($Traffic_a - $Traffic_b)) else Traffic1=$(($Traffic_b - $Traffic_a)) fi KB=1024 if [[ $Traffic1 -eq 0 ]] then Traffic=0 elif [[ $Traffic1 -ge 1024 ]] then Traffic="`echo "scale=3;$Traffic1/$KB" | bc`" 2>/dev/null else Traffic="0`echo "scale=3;$Traffic1/$KB" | bc`" 2>/dev/null fi echo -e "$(date +%H:%M:%S)\t\t$Ip\t\t$Int_name\t\t$Traffic KB/s" >> $Dest_Path/Traffic.log done } ##########Main############ Dest_Path=/root/asptools/log Report_File=/root/asptools/Report_Host First_time=$1 Last_time=$2 #Check the log directory exists and create if [[ ! -d $Dest_Path ]] then mkdir -p $Dest_Path fi if [[ $(date +%H%M) == "$First_time" ]] then echo -e "\n" > $Dest_Path/Traffic.log echo -e "\n" >> $Dest_Path/Traffic.log echo -e "\t\t\t\txxxxxxxxx有限公司" >> $Dest_Path/Traffic.log echo -e "\t\t\t\t日期:$(date +%Y-%m-%d)" >> $Dest_Path/Traffic.log echo -e "\n" >> $Dest_Path/Traffic.log echo -e "\n" >> $Dest_Path/Traffic.log echo -e "Hour:Minute\t\tHOSTNAME\t\tInterface\t\tTraffic" >> $Dest_Path/Traffic.log Flow_Monitor sleep 60 elif [[ $(date +%H%M) == "$Last_time" ]] then Flow_Monitor sleep 60 fi exit 0
Defined monitoring script:
[root@RHEL5 ~]# crontab -e
* 9,13 * * * /root/asptools/Report_traffic.sh 0915 1345 >> /tmp/log
[root@RHEL5 ~]# service crond restart