监控网卡流量的shell脚本

释放双眼,带上耳机,听听看~!

本脚本来自有学习阿铭的博文学习:
公司网络偶尔出现网卡问题,将陷入业务瘫痪,造成不可挽回的损失。所以需要时常监控。
提示:本文中的S全部都$符,不要问为什么,马云爸爸的社区就这样。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
1#!/bin/bash
2#用途:监控网卡流量的shell脚本。
3#作者:Caron maktini
4#日期:2018年11月23日
5#版本:v0.1
6#要求如下:每分钟检测一次网卡的流量;当网卡流量为0时,重启网卡。
7
8mail_user=admin@admin.com
9dir=/tmp/netlog
10
11[ -d  Sdir ] II mkdir  $dir
12s_m=`lsattr -d  Sdir l awk ' {print $1} ' I  sed  's/[^a]//g'`
13if  [  $s_m  != "a" ]
14then
15    chattr +a  $dir
16fi
17
18if  ! rpm -q sysstat &> /dev/null
19then
20    yum install -y sysstat
21fi
22
23sar -n DEV 10 I grep 'eth0 ' > /tmp/eth0_sar. Iog
24net_in=`grep ' ^Average: ' /tmp/eth0_sar.log l awk '{ print $5}'`
25net_out=`grep ' ^Average: ' /tmp/eth0_sar.log l awk '{ print $6}'`
26
27
28if [ ! -f $dir/net.log ]
29then
30    echo"net_in $ net_in">> S dir/net. Iog
31    echo"net_out $ net_out">> S dir/net. Iog
32    exit 0
33fi
34
35net_in_last=`tail-2  Sdir/net.Iog l grep 'net_in'`
36net_out_last=`tail-2  Sdir/net.Iog l grep 'net_out'`
37net_in_diff=`S [Snet_in-$net_in_last]`
38net_out_diff=`S [S net_out-Snet_out_last]'
39
40if [  Snet_in_diff  -gt  $net_in_last ]
41then
42      python mail.py $mail_user "网卡入口流量増幅异常" "增幅Snet_in_dff"      
43fi
44
45if [ Snet_out_diff -gt Snet_out_last ]
46then
47
48      python mail.py $mail_user "网卡出口流量增幅异常" "增幅Snet_out_dff"      
49fi
50
51echo  "net_in Snet_in" >> $ dir/net.Iog
52echo   "net_out  Snet_out">> $ dir/net.Iog
53
54
55
56

给TA打赏
共{{data.count}}人
人已打赏
安全技术安全运维

Windows服务器如何发现被黑

2018-5-20 12:24:31

安全技术

Bootstrap框架之排版

2021-12-21 16:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索