网卡流量统计

网卡流量统计

!/bin/sh

Declare: Statics the Network interface rate flow

By: Chenxin

Date:20100323

echo -n "输入需要查询的网卡接口:"

read eth

echo "要查询的网卡接口为"$eth

echo -n "输入需要等到的时间(秒):"

read sec

echo "计算的是"$sec"秒内的平均流量,请等待."

定义网络接口

eth=eth0;

定义统计时间(Second)

sec=5;

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))

echo $sec"秒内总流量为:"$sum "bytes"

aver=$(($sum/$sec))

echo "平均流量为:"$aver "bytes/sec"

echo $aver "bytes/sec"

/proc/net/dev文件格式如下:

Inter-| Receive | Transmit

face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed

lo:4162 61 0 0 0 0 0 0 4162 61 0 0 0 0 0 0

eth0:1974 976 49 49 0 49 0 6 2033 409 0 0 0 0 0 0

eth1:9939 87 0 0 0 0 0 0 0 0 0 0 0 0 0 0

eth2:6911 7367 0 0 0 0 0 897 675 371 0 0 0 0 0 0

bond0:1975 1317 49 49 0 49 0 6 3224 5409 0 0 0 0 0 0

sit0:0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

远程:

!/bin/sh

Declare: Statics the Network interface rate flow

By: Chenxin

Date:20100408

echo -n "输入需要查询的网卡接口:"

read eth

echo "要查询的网卡接口为"$eth

echo -n "输入需要等到的时间(秒):"

read sec

echo "计算的是"$sec"秒内的平均流量,请等待."

rm -rf ./result.tmp;

echo "### Wait for 10 secs, all results will be wrote in the result.tmp file!";
echo "############## 以下为服务器 eth0 接口流量情况 ###############" >> ./result.tmp

eth0接口流量统计

for i in cat ./ip.txt
do
{
eth=eth0;
sec=5;
infirst=$(/usr/bin/ssh -i /home/web/.ssh/id_dsa web@$i "awk '/'$eth'/{print $1 }' /proc/net/dev " |awk '{print $1}'|sed 's/'$eth'😕/')
outfirst=$(/usr/bin/ssh -i /home/web/.ssh/id_dsa web@$i "awk '/'$eth'/{print $1 }' /proc/net/dev" |awk '{print $9}')
sumfirst=$(($infirst+$outfirst+0));
sleep $sec"s"
inend=$(/usr/bin/ssh -i /home/web/.ssh/id_dsa web@$i "awk '/'$eth'/{print $1 }' /proc/net/dev " |awk '{print $1}'|sed 's/'$eth'😕/')
outend=$(/usr/bin/ssh -i /home/web/.ssh/id_dsa web@$i "awk '/'$eth'/{print $1 }' /proc/net/dev" |awk '{print $9}')
sumend=$(($inend+$outend+0))
sum=$(($sumend-$sumfirst))
aver_total=$((($sum/$sec)/1024))
aver_in=$(((($inend-$infirst+0)/$sec)/1024))
aver_out=$(((($outend-$outfirst+0)/$sec)/1024))
echo "服务器IP地址是 $i 接口:$eth 平均总流量: $aver_total KB/sec 平均进网卡流量:$aver_in KB/sec 平均出网卡流量:$aver_out KB/sec" >> ./result.tmp
}&
done

wait;

echo "";
echo "### Wait for 5 secs, all results will be wrote in the result.tmp file!";
echo "############## 以下为服务器 eth1 接口流量情况 ###############" >> ./result.tmp

eth1接口流量统计

for i in cat ./ip.txt
do
{
eth=eth1;
sec=5;
infirst=$(/usr/bin/ssh -i /home/web/.ssh/id_dsa web@$i "awk '/'$eth'/{print $1 }' /proc/net/dev " |awk '{print $1}'|sed 's/'$eth'😕/')
outfirst=$(/usr/bin/ssh -i /home/web/.ssh/id_dsa web@$i "awk '/'$eth'/{print $1 }' /proc/net/dev" |awk '{print $9}')
sumfirst=$(($infirst+$outfirst+0));
sleep $sec"s"
inend=$(/usr/bin/ssh -i /home/web/.ssh/id_dsa web@$i "awk '/'$eth'/{print $1 }' /proc/net/dev " |awk '{print $1}'|sed 's/'$eth'😕/')
outend=$(/usr/bin/ssh -i /home/web/.ssh/id_dsa web@$i "awk '/'$eth'/{print $1 }' /proc/net/dev" |awk '{print $9}')
sumend=$(($inend+$outend+0))
sum=$(($sumend-$sumfirst))
aver_total=$((($sum/$sec)/1024))
aver_in=$(((($inend-$infirst+0)/$sec)/1024))
aver_out=$(((($outend-$outfirst+0)/$sec)/1024))
echo "服务器IP地址是 $i 接口:$eth 平均总流量: $aver_total KB/sec 平均进网卡流量:$aver_in KB/sec 平均出网卡流量:$aver_out KB/sec" >> ./result.tmp
}&
done

wait;

echo "";
echo "";
echo "### Sort the traffic; ###"
cat ./result.tmp |sort --key=5n |grep -v "#" |tee ./result.sort;
rm -rf ./result.*;

!/bin/sh

Declare: Statics the Network interface rate flow

By: Chenxin

Date:20100323

echo -n "输入需要查询的网卡接口:"

read eth

echo "要查询的网卡接口为"$eth

echo -n "输入需要等到的时间(秒):"

read sec

echo "计算的是"$sec"秒内的平均流量,请等待."

定义网络接口

eth=eth0;

定义统计时间(Second)

sec=5;

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))

echo $sec"秒内总流量为:"$sum "bytes"

aver=$(($sum/$sec))

echo "平均流量为:"$aver "bytes/sec"

echo $aver "bytes/sec"

/proc/net/dev文件格式如下:

Inter-| Receive | Transmit

face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed

lo:4162 61 0 0 0 0 0 0 4162 61 0 0 0 0 0 0

eth0:1974 976 49 49 0 49 0 6 2033 409 0 0 0 0 0 0

eth1:9939 87 0 0 0 0 0 0 0 0 0 0 0 0 0 0

eth2:6911 7367 0 0 0 0 0 897 675 371 0 0 0 0 0 0

bond0:1975 1317 49 49 0 49 0 6 3224 5409 0 0 0 0 0 0

sit0:0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

远程:

!/bin/sh

Declare: Statics the Network interface rate flow

By: Chenxin

Date:20100408

echo -n "输入需要查询的网卡接口:"

read eth

echo "要查询的网卡接口为"$eth

echo -n "输入需要等到的时间(秒):"

read sec

echo "计算的是"$sec"秒内的平均流量,请等待."

rm -rf ./result.tmp;

echo "### Wait for 10 secs, all results will be wrote in the result.tmp file!";
echo "############## 以下为服务器 eth0 接口流量情况 ###############" >> ./result.tmp

eth0接口流量统计

for i in cat ./ip.txt
do
{
eth=eth0;
sec=5;
infirst=$(/usr/bin/ssh -i /home/web/.ssh/id_dsa web@$i "awk '/'$eth'/{print $1 }' /proc/net/dev " |awk '{print $1}'|sed 's/'$eth'😕/')
outfirst=$(/usr/bin/ssh -i /home/web/.ssh/id_dsa web@$i "awk '/'$eth'/{print $1 }' /proc/net/dev" |awk '{print $9}')
sumfirst=$(($infirst+$outfirst));
sleep $sec"s"
inend=$(/usr/bin/ssh -i /home/web/.ssh/id_dsa web@$i "awk '/'$eth'/{print $1 }' /proc/net/dev " |awk '{print $1}'|sed 's/'$eth'😕/')
outend=$(/usr/bin/ssh -i /home/web/.ssh/id_dsa web@$i "awk '/'$eth'/{print $1 }' /proc/net/dev" |awk '{print $9}')
sumend=$(($inend+$outend))
sum=$(($sumend-$sumfirst))
aver_total=$((($sum/$sec)/1024))
aver_in=$(((($inend-$infirst)/$sec)/1024))
aver_out=$(((($outend-$outfirst)/$sec)/1024))
echo "服务器IP地址是 $i 接口:$eth 平均总流量: $aver_total KB/sec 平均进网卡流量:$aver_in KB/sec 平均出网卡流量:$aver_out KB/sec" >> ./result.tmp
}&
done

wait;

echo "";
echo "### Wait for 5 secs, all results will be wrote in the result.tmp file!";
echo "############## 以下为服务器 eth1 接口流量情况 ###############" >> ./result.tmp

eth1接口流量统计

for i in cat ./ip.txt
do
{
eth=eth1;
sec=5;
infirst=$(/usr/bin/ssh -i /home/web/.ssh/id_dsa web@$i "awk '/'$eth'/{print $1 }' /proc/net/dev " |awk '{print $1}'|sed 's/'$eth'😕/')
outfirst=$(/usr/bin/ssh -i /home/web/.ssh/id_dsa web@$i "awk '/'$eth'/{print $1 }' /proc/net/dev" |awk '{print $9}')
sumfirst=$(($infirst+$outfirst));
sleep $sec"s"
inend=$(/usr/bin/ssh -i /home/web/.ssh/id_dsa web@$i "awk '/'$eth'/{print $1 }' /proc/net/dev " |awk '{print $1}'|sed 's/'$eth'😕/')
outend=$(/usr/bin/ssh -i /home/web/.ssh/id_dsa web@$i "awk '/'$eth'/{print $1 }' /proc/net/dev" |awk '{print $9}')
sumend=$(($inend+$outend))
sum=$(($sumend-$sumfirst))
aver_total=$((($sum/$sec)/1024))
aver_in=$(((($inend-$infirst+0)/$sec)/1024))
aver_out=$(((($outend-$outfirst+0)/$sec)/1024))
echo "服务器IP地址是 $i 接口:$eth 平均总流量: $aver_total KB/sec 平均进网卡流量:$aver_in KB/sec 平均出网卡流量:$aver_out KB/sec" >> ./result.tmp
}&
done

wait;

echo "";
echo "";
echo "### Sort the traffic; ###"
cat ./result.tmp |sort --key=5n |grep -v "#" |tee ./result.sort;

posted @ 2020-04-21 13:48  ChanixChen  阅读(202)  评论(0)    收藏  举报