LINUX下的流量监控shell脚本
程序员文章站
2023-11-25 13:39:58
最近比较忙,好久没更新博客了,今天刚好不忙写了一个流量监控脚本.测试在centos下已通过,有需要的朋友可以试试,有bug或者需要添加其他功能话可以留言哦.一、脚本源码#...
最近比较忙,好久没更新博客了,今天刚好不忙写了一个流量监控脚本.测试在centos下已通过,有需要的朋友可以试试,有bug或者需要添加其他功能话可以留言哦.
一、脚本源码
# vi /etc/rc.d/traffic_monitor.sh
----------------------------------------------
----------------------------------------------
二、执行效果
一、脚本源码
# vi /etc/rc.d/traffic_monitor.sh
----------------------------------------------
复制代码 代码如下:
#!/bin/bash
path=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin;
export path
function traffic_monitor {
# 系统版本
os_name=$(sed -n '1p' /etc/issue)
# 网口名
eth=$1
#判断网卡存在与否,不存在则退出
if [ ! -d /sys/class/net/$eth ];then
echo -e "network-interface not found"
echo -e "you system have network-interface:\n`ls /sys/class/net`"
exit 5
fi
while [ "1" ]
do
# 状态
status="fine"
# 获取当前时刻网口接收与发送的流量
rxpre=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $2}')
txpre=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $10}')
# 获取1秒后网口接收与发送的流量
sleep 1
rxnext=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $2}')
txnext=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $10}')
clear
# 获取这1秒钟实际的进出流量
rx=$((${rxnext}-${rxpre}))
tx=$((${txnext}-${txpre}))
# 判断接收流量如果大于mb数量级则显示mb单位,否则显示kb数量级
if [[ $rx -lt 1024 ]];then
rx="${rx}b/s"
elif [[ $rx -gt 1048576 ]];then
rx=$(echo $rx | awk '{print $1/1048576 "mb/s"}')
$status="busy"
else
rx=$(echo $rx | awk '{print $1/1024 "kb/s"}')
fi
# 判断发送流量如果大于mb数量级则显示mb单位,否则显示kb数量级
if [[ $tx -lt 1024 ]];then
tx="${tx}b/s"
elif [[ $tx -gt 1048576 ]];then
tx=$(echo $tx | awk '{print $1/1048576 "mb/s"}')
else
tx=$(echo $tx | awk '{print $1/1024 "kb/s"}')
fi
# 打印信息
echo -e "==================================="
echo -e "welcome to traffic_monitor stage"
echo -e "version 1.0"
echo -e "since 2014.2.26"
echo -e "created by showerlee"
echo -e "blog: http://www.showerlee.com"
echo -e "==================================="
echo -e "system: $os_name"
echo -e "date: `date +%f`"
echo -e "time: `date +%k:%m:%s`"
echo -e "port: $1"
echo -e "status: $status"
echo -e " \t rx \ttx"
echo "------------------------------"
# 打印实时流量
echo -e "$eth \t $rx $tx "
echo "------------------------------"
# 退出信息
echo -e "press 'ctrl+c' to exit"
done
}
# 判断执行参数
if [[ -n "$1" ]];then
# 执行函数
traffic_monitor $1
else
echo -e "none parameter,please add system netport after run the script! \nexample: 'sh traffic_monitor eth0'"
fi
path=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin;
export path
function traffic_monitor {
# 系统版本
os_name=$(sed -n '1p' /etc/issue)
# 网口名
eth=$1
#判断网卡存在与否,不存在则退出
if [ ! -d /sys/class/net/$eth ];then
echo -e "network-interface not found"
echo -e "you system have network-interface:\n`ls /sys/class/net`"
exit 5
fi
while [ "1" ]
do
# 状态
status="fine"
# 获取当前时刻网口接收与发送的流量
rxpre=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $2}')
txpre=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $10}')
# 获取1秒后网口接收与发送的流量
sleep 1
rxnext=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $2}')
txnext=$(cat /proc/net/dev | grep $eth | tr : " " | awk '{print $10}')
clear
# 获取这1秒钟实际的进出流量
rx=$((${rxnext}-${rxpre}))
tx=$((${txnext}-${txpre}))
# 判断接收流量如果大于mb数量级则显示mb单位,否则显示kb数量级
if [[ $rx -lt 1024 ]];then
rx="${rx}b/s"
elif [[ $rx -gt 1048576 ]];then
rx=$(echo $rx | awk '{print $1/1048576 "mb/s"}')
$status="busy"
else
rx=$(echo $rx | awk '{print $1/1024 "kb/s"}')
fi
# 判断发送流量如果大于mb数量级则显示mb单位,否则显示kb数量级
if [[ $tx -lt 1024 ]];then
tx="${tx}b/s"
elif [[ $tx -gt 1048576 ]];then
tx=$(echo $tx | awk '{print $1/1048576 "mb/s"}')
else
tx=$(echo $tx | awk '{print $1/1024 "kb/s"}')
fi
# 打印信息
echo -e "==================================="
echo -e "welcome to traffic_monitor stage"
echo -e "version 1.0"
echo -e "since 2014.2.26"
echo -e "created by showerlee"
echo -e "blog: http://www.showerlee.com"
echo -e "==================================="
echo -e "system: $os_name"
echo -e "date: `date +%f`"
echo -e "time: `date +%k:%m:%s`"
echo -e "port: $1"
echo -e "status: $status"
echo -e " \t rx \ttx"
echo "------------------------------"
# 打印实时流量
echo -e "$eth \t $rx $tx "
echo "------------------------------"
# 退出信息
echo -e "press 'ctrl+c' to exit"
done
}
# 判断执行参数
if [[ -n "$1" ]];then
# 执行函数
traffic_monitor $1
else
echo -e "none parameter,please add system netport after run the script! \nexample: 'sh traffic_monitor eth0'"
fi
----------------------------------------------
二、执行效果
复制代码 代码如下:
# sh traffic_monitor.sh eth0