LINUX下的流量监控shell脚本
(编辑:jimmy 日期: 2024/11/3 浏览:3 次 )
最近比较忙,好久没更新博客了,今天刚好不忙写了一个流量监控脚本.测试在centos下已通过,有需要的朋友可以试试,有BUG或者需要添加其他功能话可以留言哦.
一、脚本源码
# 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
----------------------------------------------
二、执行效果
复制代码 代码如下:# sh traffic_monitor.sh eth0
一、脚本源码
# 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
----------------------------------------------
二、执行效果
复制代码 代码如下:# sh traffic_monitor.sh eth0
下一篇:shell脚本操作oracle删除表空间、创建表空间、删除用户