可以使用Linux系统中的命令如"ifconfig"和"grep"编写脚本来监控网络流量。示例脚本如下:
#!/bin/bash
# 阈值,超过10M时进行告警
THRESHOLD=10485760
# 获取网卡名称
NIC=`ifconfig | grep -B1 "inet addr" | awk '{ if ( $1 == "inet" ) { print $1; } else if ( $2 == "Link" ) { printf "%s\n",$1; } }' | grep -v "lo"`
# 获取网卡流量
RX=`ifconfig $NIC | grep "RX bytes" | awk '{print $2}' | awk -F : '{print $2}'`
TX=`ifconfig $NIC | grep "TX bytes" | awk '{print $6}'`
# 判断流量是否超过阈值
if [ $RX -gt $THRESHOLD ] || [ $TX -gt $THRESHOLD ]; then
# 发送QQ邮件告警
echo "Network traffic exceeded threshold!" | mail -s "Network Alert" your@qq.com
fi