前言
使用shell命令从/proc/net/dev获取流量信息进行监控。
脚本
#!/bin/bash
interface=$1
warn_rx_limit=$2
warn_tx_limit=$3
critical_rx_limit=$4
critical_tx_limit=$5
#使用方法
USAGE="USAGE : $0 interface warn_rx_limit warn_tx_limit critical_rx_limit critical_tx_limit, such as $0 eth0 10 10 50 50"
if [ $# -lt 5 ]; then
echo "Hava no enough parameters - "$USAGE
exit 3
fi
record_time=$(date +%s)
rx=0
tx=0
content=$(cat /proc/net/dev | grep -v "|" | awk '{split($1,b,":");print b[1]" "$2" "$10 }')
a=0
for i in $content; do
if [ "$i" == "$interface" ]; then
# echo $i
let a=a+1
continue
fi
if [ "$a" == "1" ]; then
rx=$i
# echo "a=$a"
let a=a+1
continue
fi
if [ "$a" == "2" ]; then
tx=$i
# echo "a=$a"
break
fi
# echo "a=$a"
# echo $i
done
if [ "$a" == "0" ]; then
echo "$interface is not exists"
exit 3
fi
# echo "rx="$rx
# echo "tx="$tx
record_file="/tmp/net_traffic_$interface"
if [ ! -f $record_file ]; then
echo "$record_time $rx $tx" >$record_file
echo "first check..."
exit 0
fi
last_record_time=$(cat $record_file | awk '{print $1}')
last_rx=$(cat $record_file | awk '{print $2}')
last_tx=$(cat $record_file | awk '{print $3}')
# echo $last_record_time
# echo $last_rx
# echo $last_tx
interval=$(awk -v last_record_time=$last_record_time -v record_time=$record_time 'BEGIN{print record_time-last_record_time}')
if [ "$interval" == "0" ]
then
echo "check interval must greater than 1s"
exit 4
fi
avg_rx=$(awk -v rx=$rx -v last_rx=$last_rx -v last_record_time=$last_record_time -v record_time=$record_time 'BEGIN{a=(rx-last_rx)*8.0/1024.0/1024.0/(record_time-last_record_time);printf("%.4f",a)}')
# echo "avg_rx="$avg_rx"Mbps"
avg_tx=$(awk -v tx=$tx -v last_tx=$last_tx -v last_record_time=$last_record_time -v record_time=$record_time 'BEGIN{a=(tx-last_tx)*8.0/1024.0/1024.0/(record_time-last_record_time);printf("%.4f",a)}')
total=$(awk -v avg_rx=$avg_rx -v avg_tx=$avg_tx 'BEGIN{printf("%.4f",avg_rx+avg_tx)}' )
rx_result=$(awk -v avg_rx=$avg_rx -v warn_rx_limit=$warn_rx_limit -v critical_rx_limit=$critical_rx_limit 'BEGIN{if(avg_rx>critical_rx_limit)print 100 ; else if(avg_rx>warn_rx_limit) print 50 ; else print 0}')
tx_result=$(awk -v avg_tx=$avg_tx -v warn_tx_limit=$warn_tx_limit -v critical_tx_limit=$critical_tx_limit 'BEGIN{if(avg_tx>critical_tx_limit)print 100 ; else if(avg_tx>warn_tx_limit) print 50 ; else print 0}')
if [[ "$rx_result" == "100" || "$tx_result" == "100" ]]; then
echo "Critical - Network traffice in is $avg_rx Mbps , out is $avg_tx Mbps ,check interval is $interval s | In=${avg_rx}Mbps;$warn_rx_limit;$critical_rx_limit;0;0 Out=${avg_tx}Mbps;$warn_tx_limit;$critical_tx_limit;0;0 Total=${total}Mbps Time_interval=${interval}s"
echo "$record_time $rx $tx" >$record_file
exit 1
elif [[ "$rx_result" == "50" || "$tx_result" == "50" ]]; then
echo "Warning - Network traffice in is $avg_rx Mbps , out is $avg_tx Mbps ,check interval is $interval s | In=${avg_rx}Mbps;$warn_rx_limit;$critical_rx_limit;0;0 Out=${avg_tx}Mbps;$warn_tx_limit;$critical_tx_limit;0;0 Total=${total}Mbps Time_interval=${interval}s"
echo "$record_time $rx $tx" >$record_file
exit 2
else
echo "OK - Network traffice in is $avg_rx Mbps , out is $avg_tx Mbps ,check interval is $interval s | In=${avg_rx}Mbps;$warn_rx_limit;$critical_rx_limit;0;0 Out=${avg_tx}Mbps;$warn_tx_limit;$critical_tx_limit;0;0 Total=${total}Mbps Time_interval=${interval}s"
echo "$record_time $rx $tx" >$record_file
exit 0
fi
总结
使用方法
./check_net_traffice.sh eth0 10 10 50 50
默认单位是Mbps,如果是nagios配置的时候注意临时记录文件/tmp/net_traffic_×的权限。
本文暂时没有评论,来添加一个吧(●'◡'●)