forked from jonschipp/nagios-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_pps.sh
executable file
·166 lines (138 loc) · 3.84 KB
/
check_pps.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env bash
# Author: Jon Schipp
########
# Examples:
# 1.) Return critical if there's more than 10k PPS
# $ ./check_pps.sh -i eth0 -w 8000 -c 10000 -p
#
# 2.) Return critical if there's more than 1m BPS
# $ ./check_pps.sh -i eth0 -w 500000 -c 1000000 -b
#
# 2.) Return critical if we've reach 70% of the NIC's line-rate capacity
# $ ./check_pps.sh -i eth0 -w 50 -c 70 -r
#
# Nagios Exit Codes
OK=0
WARNING=1
CRITICAL=2
UNKNOWN=3
usage()
{
cat <<EOF
Nagios plug-in that calculates receiving PPS, BPS, and percentage of line-rate (LR)
from Linux kernel statistics by reading from procfs and reports if above a given threshold.
Options:
-i Network interface
-p Use PPS as criteria
-b Use BPS as criteria
-r Use percentage of line-rate as criteria
-t <int> Time interval in seconds (def: 1)
-w <int> Warning threshold
-c <int> Critical threshold
EOF
}
argcheck() {
if [ $ARGC -lt $1 ]; then
echo "Please specify an argument!, try $0 -h for more information"
exit 1
fi
}
get_network_data() {
# Get speed of NIC
speed=$(cat /sys/class/net/$INT/speed)
# Get number of packets for interface
rxppsold=$(awk "/$INT/ "'{ sub(":", " "); print $3 }' /proc/net/dev)
txppsold=$(awk "/$INT/ "'{ sub(":", " "); print $11 }' /proc/net/dev)
# Get number of bytes for interface
rxbytesold=$(awk "/$INT/ "'{ sub(":", " "); print $2 }' /proc/net/dev)
txbytesold=$(awk "/$INT/ "'{ sub(":", " "); print $10 }' /proc/net/dev)
sleep $INTERVAL
# Get number of packets for interface again and subtract from old
rxppsnew=$(awk -v rxppsold="$rxppsold" "/$INT/ "'{ \
sub(":", " "); rxppsnew = $3; print rxppsnew - rxppsold }' /proc/net/dev)
txppsnew=$(awk -v txppsold="$txppsold" "/$INT/ "'{ \
sub(":", " "); txppsnew = $11; print txppsnew - txppsold }' /proc/net/dev)
# Get number of bytes for interface again and subtract from old
rxbytesnew=$(awk -v rxbytesold="$rxbytesold" "/$INT/ "'{ \
sub(":", " "); rxbytesnew = $2; print rxbytesnew - rxbytesold }' /proc/net/dev)
txbytesnew=$(awk -v txbytesold="$txbytesold" "/$INT/ "'{ \
sub(":", " "); txbytesnew = $10; print txbytesnew - txbytesold }' /proc/net/dev)
# Calculate percentage of line-rate from number of bytes per second.
rxlinerate=$(echo "$rxbytesnew / 125000 / $speed * 100" | bc -l)
txlinerate=$(echo "$txbytesnew / 125000 / $speed * 100" | bc -l)
# Format line-rate values by truncating after the 1000th decimal place.
rxlr=$(printf "%1.3f" $rxlinerate)
txlr=$(printf "%1.3f" $txlinerate)
rxlrint=$(printf "%.0f" $rxlinerate)
txlrint=$(printf "%.0f" $txlinerate)
# Print the results
echo -e "Int: ${INT} | [RX] PPS: ${rxppsnew} | BPS: ${rxbytesnew} | % of LR: $rxlr -- [TX] PPS: ${txppsnew} | BPS: $txbytesnew | % of LR: $txlr"
}
threshold_calculation() {
if [ $1 -gt $CRIT ]; then
exit $CRITICAL
elif [ $1 -gt $WARN ]; then
exit $WARNING
else
exit $OK
fi
}
PPS=0
BPS=0
LINERATE=0
INTERVAL=1
WARN=0
CRIT=0
ARGC=$#
# Check for dependency filesystems
if ! [ -d /sys ] || ! [ -d /proc ]; then
echo "$0 requires sysfs and procfs"
exit $UNKNOWN
fi
# Print warning and exit if less than n arguments specified
argcheck 1
# option and argument handling
while getopts "hi:c:w:t:pbr" OPTION
do
case $OPTION in
h)
usage
exit
;;
i)
INT=$OPTARG
;;
p)
PPS=1
;;
b)
BPS=1
;;
r)
LINERATE=1
;;
t)
INTERVAL=$OPTARG
;;
c)
CRIT=$OPTARG
;;
w)
WARN=$OPTARG
;;
*)
exit $UNKNOWN
;;
esac
done
get_network_data
if [ $PPS -eq 1 ]; then
threshold_calculation $rxppsnew
elif [ $BPS -eq 1 ]; then
threshold_calculation $rxbytesnew
elif [ $LINERATE -eq 1 ]; then
threshold_calculation $rxlrint
else
echo "Error: Criteria required!"
exit $UNKNOWN
fi