-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_pfctl.sh
158 lines (139 loc) · 3.67 KB
/
check_pfctl.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
#!/bin/sh
#
# Check PF counter "current entries" with hard limit
#
# Last Modified: 13-03-2017
#
# Usage: ./check_pfctl -w <value> -c <value>
#
# Description:
#
# Example: check_pfctl.sh -w 80 -c 90
#
# Output: PF OK - states: 3743 (37% - limit: 10000)|states=3743;8000;9000;0;10000
#
# Paths to commands used in this script
# (You may have to modify this based on your system configuration)
PROGNAME=$(basename "$0")
PROGPATH=$(echo "$0" | sed -e 's,[\\/][^\\/][^\\/]*$,,')
REVISION="@NP_VERSION@"
VERSION="Version 1.0,"
AUTHOR="2017, Alexis VACHETTE"
. "$PROGPATH"/utils.sh
# Commands
pfctl="/sbin/pfctl"
# List of arrays
set -A counters "current entries" searches inserts removals
set -A offset 3 2 2 2
set -A limits;
set -A results;
i="0";
# Functions
print_version() {
echo "$VERSION $AUTHOR"
}
print_help() {
print_version $PROGNAME $VERSION
echo ""
echo "./$PROGNAME -w <value> -c <value>"
echo ""
echo "Options:"
echo " -w|--warning)"
echo " Warning thresholds"
echo " -c|--critical)"
echo " Critical thresholds"
}
pfctl_counters() {
local output=$($pfctl -si 2>&1)
while [[ $i -ne ${#counters[@]} ]]
do
local temp=$(echo "$output" | awk '
BEGIN { counter=0; }
/'"${counters[$i]}"'/ { counter = $'"${offset[$i]}"' }
END { print counter }
')
results[${#results[*]}]=$temp
i=$(($i + 1))
done
}
pfctl_limit() {
local output=$($pfctl -sm 2>&1)
local limit=$(echo "$output" | awk '
BEGIN { limit=0 }
/states/ { limit = $4 }
END { print limit }
')
limits[${#limits[*]}]=$limit
}
pfctl_print() {
local warning=$((${limits[0]}*$MAX_WARNING/100))
local critical=$((${limits[0]}*$MAX_CRITICAL/100))
local used=$((${results[0]}*100/${limits[0]}))
if [ ${results[0]} -lt $warning ]; then
echo "PF OK - states: ${results[0]} ($used% - limit: ${limits[0]})|states=${results[0]};$warning;$critical;0;${limits[0]}"
exit "$STATE_OK"
elif [[ ${results[0]} -ge $warning && ${results[0]} -lt $critical ]]; then
echo "PF WARNING - states: ${results[0]} ($used% - limit: ${limits[0]})|states=${results[0]};$warning;$critical;0;${limits[0]}"
exit "$STATE_WARNING"
elif [ ${results[0]} -ge $critical ]; then
echo "PF CRITICAL - states: ${results[0]} ($used% - limit: ${limits[0]})|states=${results[0]};$warning;$critical;0;${limits[0]}"
exit "$STATE_CRITICAL"
fi
}
pfctl_stats() {
pfctl_counters ${counters} ${offset}
pfctl_limit
pfctl_print ${results} ${limits}
}
# Make sure the correct number of command line
# arguments have been supplied
if [ $# -lt 2 ]; then
print_help
exit "$STATE_UNKNOWN"
fi
# Grab command line arguments
while test -n "$1"; do
case "$1" in
--help)
print_help
exit "$STATE_OK"
;;
-h)
print_help
exit "$STATE_OK"
;;
--version)
print_revision "$PROGNAME" $REVISION
exit "$STATE_OK"
;;
-V)
print_revision "$PROGNAME" $REVISION
exit "$STATE_OK"
;;
-c)
MAX_CRITICAL=$2
shift
;;
--critical)
MAX_CRITICAL=$2
shift
;;
-w)
MAX_WARNING=$2
shift
;;
--warning)
MAX_WARNING=$2
shift
;;
*)
echo "Unknown argument: $1"
echo ""
print_help
exit "$STATE_UNKNOWN"
;;
esac
shift
done
pfctl_stats
exit "$exitstatus"