教程集 www.jiaochengji.com
教程集 >  脚本编程  >  shell  >  正文 实时监测vps主机流量的shell脚本

实时监测vps主机流量的shell脚本

发布时间:2014-10-19   编辑:jiaochengji.com
本文介绍下,一个用于实时监测vps主机流量的shell脚本,不用iftop工具,采用纯脚本的方式。有需要的朋友参考下。

在linux中,实时流量监控不外乎两种方法。

方法1,可以安装iftop,通过ascii图形化显示实时流量数据,比较直观明显。
方法2,就是本文要分享的这种,用shell脚本采集/proc/net/dev中的实时数据,不依赖任何安装包,对于内网linux服务器很有用。

脚本如下:
 

复制代码 代码示例:

#!/bin/bash
# test network width
# edit by www.jiaochengji.com

# usage
function usage
{
   echo "Usage: $0  "
   echo "    e.g. $0 eth0 2"
   exit 65
}

if [ $# -lt 2 ];then
usage
fi
typeset in in_old dif_in
typeset out out_old dif_out
typeset timer
typeset eth

eth=$1
timer=$2

in_old=$(cat /proc/net/dev | grep $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk ' { print $1 }' )
out_old=$(cat /proc/net/dev | grep $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk ' { print $9 }' )

while true
do
sleep ${timer}
in=$(cat /proc/net/dev | grep $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk ' { print $1 }' )
out=$(cat /proc/net/dev | grep $eth | sed -e "s/\(.*\)\:\(.*\)/\2/g" | awk ' { print $9 }' )
dif_in=$(((in-in_old)/timer))
dif_out=$(((out-out_old)/timer))
echo "IN: ${dif_in} Byte/s OUT: ${dif_out} Byte/s "
in_old=${in}
out_old=${out}
done
exit 0

脚本说明:
本脚本需要二个参数。
参数1, 网卡设备号,一般就是 eth0;参数2,统计间隔的秒数,2 表示2秒计算一次。
流量统计单位是 Byte/s。

您可能感兴趣的文章:
实时监测vps主机流量的shell脚本
实时查看Linux网卡流量的shell脚本分享(图文)
vps自动备份shell脚本代码
监控网卡流量的shell脚本分享
检测网卡流量的shell脚本
统计网卡流量的二个shell脚本(ifconfig)(图文)
一个测试网卡流量的shell脚本
一个自动监控进程的shell脚本
linux下监控网卡流量的shell脚本(实例分享)(图文)
shell磁盘监控及报警的脚本

[关闭]
~ ~