本腳本來自有學習阿銘的博文學習:
公司網絡偶爾出現網卡問題,将陷入業務癱瘓,造成不可挽回的損失。是以需要時常監控。
提示:本文中的S全部都$符,不要問為什麼,馬雲爸爸的社群就這樣。
#!/bin/bash
#用途:監控網卡流量的shell腳本。
#作者:Caron maktini
#日期:2018年11月23日
#版本:v0.1
#要求如下:每分鐘檢測一次網卡的流量;當網卡流量為0時,重新開機網卡。
[email protected]
dir=/tmp/netlog
[ -d Sdir ] II mkdir $dir
s_m=`lsattr -d Sdir l awk ' {print $1} ' I sed 's/[^a]//g'`
if [ $s_m != "a" ]
then
chattr +a $dir
fi
if ! rpm -q sysstat &> /dev/null
then
yum install -y sysstat
fi
sar -n DEV 10 I grep 'eth0 ' > /tmp/eth0_sar. Iog
net_in=`grep ' ^Average: ' /tmp/eth0_sar.log l awk '{ print $5}'`
net_out=`grep ' ^Average: ' /tmp/eth0_sar.log l awk '{ print $6}'`
if [ ! -f $dir/net.log ]
then
echo"net_in $ net_in">> S dir/net. Iog
echo"net_out $ net_out">> S dir/net. Iog
exit 0
fi
net_in_last=`tail-2 Sdir/net.Iog l grep 'net_in'`
net_out_last=`tail-2 Sdir/net.Iog l grep 'net_out'`
net_in_diff=`S [Snet_in-$net_in_last]`
net_out_diff=`S [S net_out-Snet_out_last]'
if [ Snet_in_diff -gt $net_in_last ]
then
python mail.py $mail_user "網卡入口流量増幅異常" "增幅Snet_in_dff"
fi
if [ Snet_out_diff -gt Snet_out_last ]
then
python mail.py $mail_user "網卡出口流量增幅異常" "增幅Snet_out_dff"
fi
echo "net_in Snet_in" >> $ dir/net.Iog
echo "net_out Snet_out">> $ dir/net.Iog