需求是我们需要对服务器上的流量进行监控,网络上有个流传的check_traffic.sh,它需要被监控机开启snmp。但是感觉都使用上了nagios还要开snmp。。。有点斧子剪刀一起用的感觉,所以就动手写了个监控流量的shell:
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 1#!/bin/sh
2
3usage() { echo "Usage: $0 [-n <eth0>] [-w <tx rx>] [-c <tx rx>]" 1>&2; exit 1; }
4
5foundw=0;
6foundc=0;
7foundn=0;
8
9for item in $@ ; do
10 if [[ $foundn == 1 ]]; then
11 n=$item;
12 foundn=2;
13 continue;
14 fi
15 if [[ $foundw == 1 ]]; then
16 w1=$item;
17 foundw=2;
18 continue;
19 fi
20 if [[ $foundw == 2 ]]; then
21 w2=$item;
22 foundw=3;
23 continue;
24 fi
25 if [[ $foundc == 1 ]]; then
26 c1=$item;
27 foundc=2;
28 continue;
29 fi
30 if [[ $foundc == 2 ]]; then
31 c2=$item;
32 foundc=2;
33 continue;
34 fi
35 if [[ "$item" == "-w" ]]; then
36 foundw=1;
37 continue;
38 fi
39 if [[ "$item" == "-c" ]]; then
40 foundc=1;
41 continue;
42 fi
43 if [[ "$item" == "-n" ]]; then
44 foundn=1;
45 continue;
46 fi
47done
48
49if [ -z "${w1}" ] || [ -z "${w2}" ] || [ -z "${c1}" ] || [ -z "${c2}" ] || [ -z "${n}" ]; then
50 usage
51fi
52
53R1=`cat /sys/class/net/$n/statistics/rx_bytes`
54T1=`cat /sys/class/net/$n/statistics/tx_bytes`
55sleep 1
56R2=`cat /sys/class/net/$n/statistics/rx_bytes`
57T2=`cat /sys/class/net/$n/statistics/tx_bytes`
58TBPS=`expr $T2 - $T1`
59RBPS=`expr $R2 - $R1`
60TMBPS=`expr $TBPS / 1024 / 128`
61RMBPS=`expr $RBPS / 1024 / 128`
62
63if [[ $TMBPS -ge $c1 ]] || [[ $RMBPS -ge $c2 ]] ; then
64 echo "Critical - current is ${TMBPS}, ${RMBPS}";
65 exit 2;
66fi
67if [[ $TMBPS -ge $w1 ]] || [[ $RMBPS -ge $w2 ]] ; then
68 echo "WARNING - current is ${TMBPS}, ${RMBPS}";
69 exit 1;
70fi
71echo "OK - current is ${TMBPS}, ${RMBPS}";
72exit 0;
73
其中的w和c的数值单位都是Mb。
本文转自轩脉刃博客园博客,原文链接:http://www.cnblogs.com/yjf512/p/3704211.html,如需转载请自行联系原作者