(1)网卡监控脚本
#!/bin/bash
echo -n "请输入需要查询的网卡的接口(eth0):"
read eth
echo "你要查询的网卡接口为"$eth
declare -i sec
echo -n "输入需要等到的时间(秒):"
read sec
echo "你要查看流量的单(b,k,m):"
read type
if [ "$sec" = "0" ]
then
sec=1
fi
echo "你计算的是"$sec"秒内的平均流量"
infirst=$(awk ‘/‘$eth‘/{print $1 }‘ /proc/net/dev |sed ‘s/‘$eth‘://‘)
outfirst=$(awk ‘/‘$eth‘/{print $10 }‘ /proc/net/dev)
sumfirst=$(($infirst+$outfirst))
sleep $sec"s"
inend=$(awk ‘/‘$eth‘/{print $1 }‘ /proc/net/dev |sed ‘s/‘$eth‘://‘)
outend=$(awk ‘/‘$eth‘/{print $10 }‘ /proc/net/dev)
sumend=$(($inend+$outend))
sum=$(($sumend-$sumfirst))
aver=$(($sum/$sec))
ksum=$(($sum/1024/8))
msum=$(($sum/1024/1024/8))
kaver=$(($ksum/$sec))
maver=$(($msum/$sec))
case $type in
m|M) echo $sec"秒内总流量为:"$msum"MB"
echo "平均流量为:"$maver"MB/sec";;
k|K) echo $sec"秒内总流量为:"$ksum"KB"
echo "平均流量为:"$kaver"KB/sec";;
*) echo $sec"秒内总流量为:"$sum"bytes"
echo "平均流量为:"$aver"bytes/sec";;
esac
(2)脚本(常用)
#!/bin/bash
while [ "1" ]
do
eth=$1
RXpre=$(cat /proc/net/dev | grep $eth | tr : " " | awk ‘{print $2}‘)
TXpre=$(cat /proc/net/dev | grep $eth | tr : " " | awk ‘{print $10}‘)
sleep 1
RXnext=$(cat /proc/net/dev | grep $eth | tr : " " | awk ‘{print $2}‘)
TXnext=$(cat /proc/net/dev | grep $eth | tr : " " | awk ‘{print $10}‘)
clear
echo -e "\t RX `date +%k:%M:%S` TX"
RX=$((${RXnext}-${RXpre}))
TX=$((${TXnext}-${TXpre}))
if [[ $RX -lt 1024 ]];then
RX="${RX}B/s"
elif [[ $RX -gt 1048576 ]];then
RX=$(echo $RX | awk ‘{print $1/1048576 "MB/s"}‘)
else
RX=$(echo $RX | awk ‘{print $1/1024 "KB/s"}‘)
fi
if [[ $TX -lt 1024 ]];then
TX="${TX}B/s"
elif [[ $TX -gt 1048576 ]];then
TX=$(echo $TX | awk ‘{print $1/1048576 "MB/s"}‘)
else
TX=$(echo $TX | awk ‘{print $1/1024 "KB/s"}‘)
fi
echo -e "$eth \t $RX $TX "
done
(4)下面是用法
测试网速
/usr/local/nagios/libexec/check_traffic.sh -V 2c -C public -H 127.0.0.1 -I 2 -w 1200,1500 -c 1700,1800 -K -b
/usr/local/nagios/libexec/check_traffic.sh -V 2c -C public -H 127.0.0.1 -I 2 -w 300,350 -c 500,600 -K -B
check_command check_traffic!1 -w 600,600 -c 800,800
netstat -n | awk‘/^tcp/ {++S[$NF]}END{for(a in S) print a,S[a]}‘
77 ftp的网卡结构
Interface index 1 orresponding to lo
Interface index 2 orresponding to em1
Interface index 3 orresponding to em2
Interface index 4 orresponding to em3
Interface index 5 orresponding to em4
snmp查看实时填充流量(15秒的数据)
snmpdelta -c public -v 1 -Cs localhost IF-MIB::ifInUcastPkts.3 IF-MIB::ifOutUcastPkts.3