forked from zhangnq/nagios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_net_traffic.sh
143 lines (122 loc) · 3.95 KB
/
check_net_traffic.sh
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/bin/bash
#set nagios status
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
usage (){
echo -en "Usage: $0 -d [ eth|bond ]\nFor example:\t$0 -d bond0 -w 100[B|K|M|G] -c 200[B|K|M|G]\n" 1>&2
exit ${STATE_WARNING}
}
check_input () {
local str="$1"
echo "${str}"|grep -E '[0-9]+[b|B|k|K|m|M|g|G]$' >/dev/null 2>&1 ||\
eval "echo ${str} is wrong!;usage"
}
replace_str () {
local str="$1"
output=`echo "${str}"|sed -r 's/[k|K]/*1024/;s/[m|M]/*1024*1024/;s/[g|G]/*1024*1024*1024/'`
echo ${output}
}
while getopts w:c:d: opt
do
case "$opt" in
w)
check_input "$OPTARG"
warning_str=`replace_str "$OPTARG"`
warning=`echo "${warning_str}"|bc`
;;
c)
check_input "$OPTARG"
critical_str=`replace_str "$OPTARG"`
critical=`echo "${critical_str}"|bc`
;;
d)
dev_id="$OPTARG"
;;
*)
usage
;;
esac
done
shift $[ $OPTIND - 1 ]
if [ -z "${dev_id}" -o -z "${warning}" -o -z "${critical}" ];then
usage
fi
source_file='/proc/net/dev'
if [ ! -f "${source_file}" ];then
echo "${source_file} not exsit!" 1>&2
exit ${STATE_WARNING}
fi
grep "${dev_id}" ${source_file} >/dev/null 2>&1 || dev_stat='not found'
if [ "${dev_stat}" = 'not found' ];then
echo "${dev_id} ${dev_stat}!" 1>&2
usage
fi
time_now=`date -d now +"%F %T"`
nagios_path='/usr/local/nagios/libexec'
test -d ${nagios_path} || mkdir -p ${nagios_path} && mark="${nagios_path}/net_traffic.${dev_id}"
search_dev=`awk -F':' '/'${dev_id}'/{print $2}' /proc/net/dev|sed -r 's/^[ ]+//'`
info=`echo "${search_dev}"|awk -v date="${time_now}" 'BEGIN{OFS=";"}{print "TIME=\""date"\"","RX="$1,"TX="$9,"DEV='${dev_id}'"}'`
#debug
#eval "${info}"
#echo $info
#echo $TIME $RX $TX && exit
marking () {
echo "$info" > ${mark} || exit ${STATE_WARNING}
chown nagios.nagios ${mark}
}
if [ ! -f "${mark}" ];then
marking
echo "This script is First run! ${info}"
exit ${STATE_OK}
else
old_info=`cat ${mark}`
eval "${old_info}"
OLD_TIME="${TIME}";OLD_RX=${RX};OLD_TX=${TX}
if [ -z "${OLD_RX}" -o -z "${OLD_TX}" ];then
echo "Data Error: ${old_info}" 1>&2
marking
exit ${STATE_WARNING}
fi
fi
if [ -n "${info}" ];then
eval ${info}
sec_now=`date -d "${TIME}" +"%s"`
sec_old=`date -d "${OLD_TIME}" +"%s"`
sec=`echo "${sec_now}-${sec_old}"|bc|sed 's/-//'`
rx=`echo "(${RX}-${OLD_RX})/${sec}"|bc|sed 's/-//'`
tx=`echo "(${TX}-${OLD_TX})/${sec}"|bc|sed 's/-//'`
marking
#debug
# echo $sec $rx $tx
else
echo "Can not read ${source_file}" 1>&2
exit ${STATE_WARNING}
fi
human_read () {
local number="$1"
if [ `echo "(${number}-1073741824) > 0"|bc` -eq 1 ];then
output="`echo "scale=2;${number}/1024/1024/1024"|bc` GB/s"
elif [ `echo "(${number}-1048576) > 0"|bc` -eq 1 ];then
output="`echo "scale=2;${number}/1024/1024"|bc` MB/s"
elif [ `echo "(${number}-1024) > 0"|bc` -eq 1 ];then
output="`echo "scale=2;${number}/1024"|bc` KB/s"
else
output="${number} B/s"
fi
echo "${output}"
}
rx_human_read=`human_read "${rx}"`
tx_human_read=`human_read "${tx}"`
message () {
local stat="$1"
echo "${DEV} Traffic is ${stat} - In: ${rx_human_read} Out: ${tx_human_read} interval: ${sec}s |in=${rx};${warning};${critical};${min};${max} out=${tx};${warning};${critical};${min};${max}"
}
#pnp4nagios setting
min=0
max=1073741824
total_int=`echo "${rx}+${tx}"|bc`
[ `echo "(${total_int}-${warning}) < 0"|bc` -eq 1 ] && message "OK" && exit ${STATE_OK}
[ `echo "(${total_int}-${critical}) >= 0"|bc` -eq 1 ] && message "Critical" && exit ${STATE_CRITICAL}
[ `echo "(${total_int}-${warning}) >= 0"|bc` -eq 1 ] && message "Warning" && exit ${STATE_WARNING}