码迷,mamicode.com
首页 > 系统相关 > 详细

网络流量监控shell脚本

时间:2015-01-12 22:10:09      阅读:309      评论:0      收藏:0      [点我收藏+]

标签:

网络收发包计数记录在 /proc/net/dev 文件中, 要取得流量, 只需要读取里面的内容两次, 然后相减, 再除以时间间隔即可.

#!/bin/bash

#Usage1,record in file: netmonitor <MinKB> <Interval> <FileName>
#Usage2,print on terminal: netmonitor <MinKB> <Interval>
#Record the value great than 100KB, 3 second Interval:
#Example1,record: netmonitor 100 3 xxx.txt
#Example2,print: netmonitor 100 3

#参数说明
#第一个是速度记录的下限,单位是KB,即超过多少KB才记录,0为全部记录
#第二个参数是采样间隔,即隔多少秒记录一次
#第三个参数是输出的文件名,为空的话就直接在终端打印

#xieqianli
#2011-4-25

#同时修改下列变量可以固定参数,使用脚本时就无需参数
MinKB="$1"
Interval="$2"
FileName="$3"
NetDev="wlan0"

#检查参数完整性
if [ "$Interval" == "" ];then
echo "Usage1,record in file: netmonitor <MinKB> <Interval> <FileName>"
echo "Usage2,print on terminal: netmonitor <MinKB> <Interval>"
echo "Record the values great than 100KB, 3 seconds Interval:"
echo "Example1,record: netmonitor 100 3 xxx.txt"
echo "Example2,print: netmonitor 100 3"
exit 1
fi

#检查 MinKB 参数的合法性
check=`echo $MinKB | grep "[^0-9]"`
if [ "$check" != "" ];then
echo "$MinKB is not a number"
exit 0
fi

len=${#MinKB}
char1=`echo $MinKB | cut -c 1`
if [ $(($len)) -gt $((1)) -a "$char1"=="0" ];then
echo "$MinKB is not a number"
exit 0
fi

#检查 Interval 参数的合法性
check=`echo $Interval | grep "[^0-9]"`
if [ "$check" != "" ];then
echo "$Interval is not a number"
exit 0
fi

len=${#Interval}
char1=`echo $Interval | cut -c 1`
if [ $(($len)) -gt $((1)) -a "$char1"=="0" ];then
echo "$Interval is not a number"
exit 0
fi

while [ 1 ]
do
#读取已接收和已发送字节数,间隔一秒
REC1=`cat /proc/net/dev | grep $NetDev | sed s/:/ / | tr -s " " | cut -d " " -f 3`
SEND1=`cat /proc/net/dev | grep $NetDev | sed s/:/ / | tr -s " " | cut -d " " -f 11`
sleep $Interval
REC2=`cat /proc/net/dev | grep $NetDev | sed s/:/ / | tr -s " " | cut -d " " -f 3`
SEND2=`cat /proc/net/dev | grep $NetDev | sed s/:/ / | tr -s " " | cut -d " " -f 11`

if [ $((REC2)) -lt $((REC1)) ];then
exit 1
fi

if [ $((SEND2)) -lt $((SEND1)) ];then
exit 1
fi

RESULT_REC=$((($REC2-$REC1)/$Interval))
RESULT_SENT=$((($SEND2-$SEND1)/$Interval))

#进行B,KB和MB的单位换算
if [ $RESULT_REC -lt $((1024)) ]; then
SPEED_REC="$RESULT_REC B "
elif [ $RESULT_REC -gt $((1024)) -a $RESULT_REC -lt $((1048576)) ]; then
SPEED_REC="$(($RESULT_REC/1024)) KB"
elif [ $RESULT_REC -gt $((1048576)) ];then
SPEED_REC="$(($RESULT_REC/1048576)) MB"
fi

if [ $RESULT_SENT -lt $((1024)) ]; then
SPEED_SENT="$RESULT_SENT B "
elif [ $RESULT_SENT -gt $((1024)) -a $RESULT_SENT -lt $((1048576)) ]; then
SPEED_SENT="$(($RESULT_SENT/1024)) KB"
elif [ $RESULT_SENT -gt $((1048576)) ];then
SPEED_SENT="$(($RESULT_SENT/1048576)) MB"
fi

#求时间
TIME=`date`

#左边填充空格,把结果进行右对齐
ZERO=$((8-${#SPEED_REC}))
for (( i=0; i<$ZERO; i=i+1 ))
do
SPEED_REC=" $SPEED_REC"
done

ZERO=$((8-${#SPEED_SENT}))
for (( i=0; i<$ZERO; i=i+1 ))
do
SPEED_SENT=" $SPEED_SENT"
done

REC_KB="$(($RESULT_REC/1024))"
SEND_KB="$(($RESULT_SENT/1024))"

#过滤掉小于 MinKB 的记录
if [ "$MinKB" != "0" -a $((REC_KB)) -lt $MinKB -a $((SEND_KB)) -lt $MinKB ];then
continue
fi

#输出结果
if [ "$FileName" = "" ];then
echo "$TIME Receive:$SPEED_REC Send:$SPEED_SENT"
else
echo "$TIME Receive:$SPEED_REC Send:$SPEED_SENT" >> $FileName
fi
done

网络流量监控shell脚本

标签:

原文地址:http://www.cnblogs.com/xieqianli/p/4219690.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!