您的位置:首页 > 运维架构 > Shell

EveryDay-Shell之"ifconfig输出详解“——netspeed.sh网速监控脚本

2014-06-27 22:44 387 查看
对应文件中,比如p1p1设备对应目录:

$cd /sys/class/net/p1p1/statistics

$ls -l

-r--r--r-- 1 root root 4096 2月 4 14:35 collisions

-r--r--r-- 1 root root 4096 2月 4 14:35 multicast

-r--r--r-- 1 root root 4096 2月 4 14:35 rx_bytes

-r--r--r-- 1 root root 4096 2月 4 14:35 rx_compressed

-r--r--r-- 1 root root 4096 2月 4 14:35 rx_crc_errors

-r--r--r-- 1 root root 4096 2月 4 14:35 rx_dropped

-r--r--r-- 1 root root 4096 2月 4 14:35 rx_errors

-r--r--r-- 1 root root 4096 2月 4 14:35 rx_fifo_errors

-r--r--r-- 1 root root 4096 2月 4 14:35 rx_frame_errors

-r--r--r-- 1 root root 4096 2月 4 14:35 rx_length_errors

-r--r--r-- 1 root root 4096 2月 4 14:35 rx_missed_errors

-r--r--r-- 1 root root 4096 2月 4 14:35 rx_over_errors

-r--r--r-- 1 root root 4096 2月 4 14:35 rx_packets

-r--r--r-- 1 root root 4096 2月 4 14:35 tx_aborted_errors

-r--r--r-- 1 root root 4096 2月 4 14:35 tx_bytes

-r--r--r-- 1 root root 4096 2月 4 14:35 tx_carrier_errors

-r--r--r-- 1 root root 4096 2月 4 14:35 tx_compressed

-r--r--r-- 1 root root 4096 2月 4 14:35 tx_dropped

-r--r--r-- 1 root root 4096 2月 4 14:35 tx_errors

-r--r--r-- 1 root root 4096 2月 4 14:35 tx_fifo_errors

-r--r--r-- 1 root root 4096 2月 4 14:35 tx_heartbeat_errors

-r--r--r-- 1 root root 4096 2月 4 14:35 tx_packets

-r--r--r-- 1 root root 4096 2月 4 14:35 tx_window_errors

$cat rx_bytes(接收总数据大小B为单位,即下行)

226698240

$cat tx_bytes(发送总数据打包B为单位,即上行)

10534137

根据这些文件存储的数据,可以设计一个监控网速的shell脚本:

#!/usr/bin/env bash

# Program: netspeed.sh

# Author: LeslieChu

# Date: 2012-02-04

# Version: 1.0

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

export PATH

if [ -z "$1" ];

then

echo

echo "Usage: $0 eth0";

exit 1

fi

while true;

do

R_1=`cat /sys/class/net/$1/statistics/rx_bytes`;

T_1=`cat /sys/class/net/$1/statistics/tx_bytes`;

sleep 1;

R_2=`cat /sys/class/net/$1/statistics/rx_bytes`;

T_2=`cat /sys/class/net/$1/statistics/tx_bytes`;

R_Byte_PerSec=$(expr $R_2 - $R_1);

T_Byte_PerSec=$(expr $T_2 - $T_1);

R_KB_PerSec=$(expr ${R_Byte_PerSec} / 1024); #记得“/”左右需要空格,否则无效

T_KB_PerSec=$(expr ${T_Byte_PerSec} / 1024);

R_MB_PerSec=$(expr ${R_KB_PerSec} / 1024);

T_MB_PerSec=$(expr ${T_KB_PerSec} / 1024);

echo -en "$1->\tTX:\t${T_KB_PerSec} KB/s, \t${T_MB_PerSec}MB/s; "

echo -e " \tRX:\t${R_KB_PerSec}KB/s, \t${R_MB_PerSec}MB/s."

done



执行结果:

$./netspeed.sh

Usage: ./netspeed.sh eth0



$./netspeed.sh p1p1

p1p1-> TX: 0 KB/s, 0MB/s; RX: 0KB/s, 0MB/s.

p1p1-> TX: 0 KB/s, 0MB/s; RX: 0KB/s, 0MB/s.

p1p1-> TX: 3 KB/s, 0MB/s; RX: 0KB/s, 0MB/s.

p1p1-> TX: 0 KB/s, 0MB/s; RX: 0KB/s, 0MB/s.

p1p1-> TX: 0 KB/s, 0MB/s; RX: 0KB/s, 0MB/s.

p1p1-> TX: 0 KB/s, 0MB/s; RX: 0KB/s, 0MB/s.

p1p1-> TX: 0 KB/s, 0MB/s; RX: 0KB/s, 0MB/s.

p1p1-> TX: 8 KB/s, 0MB/s; RX: 20KB/s, 0MB/s.

p1p1-> TX: 10 KB/s, 0MB/s; RX: 21KB/s, 0MB/s.

p1p1-> TX: 0 KB/s, 0MB/s; RX: 0KB/s, 0MB/s.

p1p1-> TX: 0 KB/s, 0MB/s; RX: 0KB/s, 0MB/s.

p1p1-> TX: 5 KB/s, 0MB/s; RX: 1KB/s, 0MB/s.

p1p1-> TX: 0 KB/s, 0MB/s; RX: 0KB/s, 0MB/s.

p1p1-> TX: 0 KB/s, 0MB/s; RX: 0KB/s, 0MB/s.

p1p1-> TX: 0 KB/s, 0MB/s; RX: 3KB/s, 0MB/s.

p1p1-> TX: 0 KB/s, 0MB/s; RX: 1KB/s, 0MB/s.

^C
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐