监控linux流量shell版
2015-11-13 09:09
411 查看
想要实时查看linux流量情况,又不想再去下第三方工具,可以直接写脚步运行!
系统:centos 6.5
原理:从/proc/net/dev中获取到流量情况,再通过换算并除以间隔时间来得到流量单位M
效果如下:
在centos 7中,/proc/net/dev显示与centos 6有点差距,所以如果在centos 7中使用,需要将以下两个地方更改:
如使用有问题,请联系:
362299908@qq.com
系统:centos 6.5
原理:从/proc/net/dev中获取到流量情况,再通过换算并除以间隔时间来得到流量单位M
#------------------ #Author:Administrator #Created:2015-04-10 #------------------ #!/bin/bash #使用方法 usage() { echo "useage: $0 ethname intervel" exit 0 } #网口配置 if [ $# -gt 1 ];then ethname=$1 num=`ifconfig|grep ${ethname}|wc -l` if [ $num -gt 0 ];then ethname=$1 else echo "Can't find $ethname,Please Check" exit 1 fi fi #时间配置 intervel=1 if [ $# -gt 0 ]; then interval=$2 test=`expr "$interval" \* 0` if [ "$test" != "0" ]; then echo "Invalid <interval> param '$interval' . It should be a integer number. Usage: $0 ethname intervel" exit -1 fi if [ $interval -lt 1 ]; then echo "Invalid <interval> param '$interval' . It should be > 0 (seconds). Usage: $0 ethname intervel" exit -1 fi fi #函数判断 if [ $# -ne 2 ] then usage fi echo "$0 is starting,Device is $1,Intervel is $2" echo -e "\n" typeset Rev old_Rev eth=$1 intervel=$2 old_Rev=`cat /proc/net/dev|grep $eth| awk '{print $1}'| awk -F : '{print $2}'` old_Send=`cat /proc/net/dev|grep $eth|awk '{print $9}'` #echo "old_Rev is $old_Rev" #echo "Old_send is $old_Send" while true do sleep $intervel Rev=`cat /proc/net/dev|grep $eth|awk '{print $1}'|awk -F : '{print $2}'` Send=`cat /proc/net/dev|grep $eth|awk '{print $9}'` #echo "Rev is $Rev" #echo "Send is $Send" diff_1=`awk -v Rev=$Rev -v old_Rev=$old_Rev -v intervel=$intervel 'BEGIN{printf "Receive is %6.2f", ( Rev - old_Rev ) / 1024 / 1024 / intervel * 8}'` diff_2=`awk -v Send=$Send -v old_Send=$old_Send -v intervel=$intervel 'BEGIN{printf "Send is %6.2f", ( Send - old_Send ) / 1024 / 1024 / intervel * 8}'` echo "`date +'%Y-%m-%d %H:%M:%S'` Speed: IN ${diff_1} mbps OUT ${diff_2} mbps" old_Rev=$Rev old_Send=$Send done
效果如下:
[root@localhost tools]# ./bandwidth.sh eth0 1 ./bandwith.sh is starting,Device is eth0,Intervel is 1 Ctrl+C To Stop! 2015-11-13 16:49:34 Speed: IN Receive is 0.02 mbps OUT Send is 0.01 mbps 2015-11-13 16:49:35 Speed: IN Receive is 0.01 mbps OUT Send is 0.00 mbps 2015-11-13 16:49:36 Speed: IN Receive is 0.01 mbps OUT Send is 0.00 mbps
在centos 7中,/proc/net/dev显示与centos 6有点差距,所以如果在centos 7中使用,需要将以下两个地方更改:
old_Rev=`cat /proc/net/dev|grep $eth| awk '{print $2}'` old_Send=`cat /proc/net/dev|grep $eth|awk '{print $10}'` #--------------------------------------------------------------------- Rev=`cat /proc/net/dev|grep $eth| awk '{print $2}'` Send=`cat /proc/net/dev|grep $eth|awk '{print $10}'`
如使用有问题,请联系:
362299908@qq.com
相关文章推荐
- 运行shell脚本提示找不到该文件的错误,解决办法
- rails+unicorn+nginx配置、shell启动脚本
- Li ux shell 四则运算
- ulimit -c 999出现 -bash: ulimit: core file size: cannot modify limit: Operation not permitted的解决办法
- How to Run a Custom Shell Script During an XCODE Build
- linux下设计一个Shell脚本:判断当前工作目录下所有的文件类型。
- linux下设计一个Shell脚本:求命令行上所有整数和
- PowerShell 之 CA Server
- Linux C编程与Shell编程在开发实用工具方面的相同点总结
- Linux Bash Shell快速入门 在线教程
- Bash Shell 快捷键
- shell脚本:一键安装LAMP、LNMP脚本
- ADB shell出现error:device offline提示
- Shell实现每日自动导出AWR报表
- PowerShell 之 DHCP server
- shell中不可不知的叹号
- 常用shell例子
- DELL服务器硬件信息采集SHELL脚本
- 在Windows7环境下使用GIT BASH免输入密码
- Bash 什么时候会给 HOME 赋初始值