-
Notifications
You must be signed in to change notification settings - Fork 5
/
kafka-consumer-lag.sh
256 lines (238 loc) · 8.26 KB
/
kafka-consumer-lag.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/env bash
#
# NRPE plugin to check kafka lag for new consumer groups
#
AUTHOR="Rohit Gupta - @rohit01"
PROGNAME=`basename $0`
VERSION="1.0"
## Global static variables
ST_OK=0
ST_WR=1
ST_CR=2
ST_UK=3
DEFAULT_KAFKA_HOME="/usr/local/kafka"
KAFKA_HOME="${DEFAULT_KAFKA_HOME}"
if [ -f /etc/default/kafka ]; then
home="$(grep 'export KAFKA_HOME=' /etc/default/kafka | cut -d"=" -f 2 | sed 's/"//g')"
if [ "${home}" != "" ]; then
KAFKA_HOME="${home}"
fi
fi
## Global Defaults
warning=50000
critical=150000
brokers=""
zookeeper=""
ctype="new"
print_version() {
echo "$PROGNAME - version: $VERSION, author: $AUTHOR"
}
print_help() {
echo "$PROGNAME is a NRPE plugin to check kafka lag for new consumer groups"
echo ""
echo "Usage: $PROGNAME -b <kafka brokers> -g <consumer group> -w <threshold> -c <threshold>"
echo ""
echo "Options:"
echo " -b/--brokers)"
echo " Kafka brokers server string (for new consumers). For eg: kafka1.local:9092,kafka2.local:9092"
echo " -z/--zookeeper)"
echo " Zookeeper string (for old consumers). For eg: zk1.local:2181,zk2.local:2181"
echo " -t/--topic)"
echo " Kafka topic (applicable only for old consumers)"
echo " -T/--ctype)"
echo " Kafka consumer type. Possible values: old & new. default: new"
echo " -g/--group)"
echo " Kafka consumer group name"
echo " -w/--warning)"
echo " Message lag for warning alert"
echo " -c/--critical)"
echo " Message lag for critical alert"
echo " -h/--help)"
echo " Print this help message & exit"
echo " -v/--version)"
echo " Print version of this script & exit"
echo ""
echo "Examples:"
echo " $PROGNAME -b kafka1:9092 -g my-group -w 50000 -c 80000"
}
#### Get command line arguments ###############################################
while test -n "$1"; do
case "$1" in
--brokers|-b)
brokers=$2
shift
;;
--zookeeper|-z)
zookeeper=$2
shift
;;
--ctype|-T)
ctype=$2
shift
;;
--topic|-t)
topic=$2
shift
;;
--group|-g)
group=$2
shift
;;
--warning|-w)
warning=$2
shift
;;
--critical|-c)
critical=$2
shift
;;
--help|-h)
print_help
exit "${ST_UK}"
;;
--version|-v)
print_version
exit "${ST_UK}"
;;
*)
echo "Unknown argument: $1"
echo ""
print_help
exit "${ST_UK}"
;;
esac
shift
done
#### Argument validations #####################################################
if [ "${ctype}" == "" ]; then
echo "Argument Error: -t/--ctype missing"
exit "${ST_UK}"
elif [ "${ctype}" != "old" ] && [ "${ctype}" != "new" ]; then
echo "Invalid value '${ctype}' for argument -t/--ctype. Possible values are old & new"
exit "${ST_UK}"
elif [ "${brokers}" == "" ] && [ "${ctype}" == "new" ]; then
echo "Argument Error: -b/--brokers missing. Kafka consumer type is configured as 'new'"
exit "${ST_UK}"
elif [ "${zookeeper}" == "" ] && [ "${ctype}" == "old" ]; then
echo "Argument Error: -z/--zookeeper missing. Kafka consumer type is configured as 'old'"
exit "${ST_UK}"
elif [ "${topic}" == "" ] && [ "${ctype}" == "old" ]; then
echo "Argument Error: -t/--topic missing. Kafka consumer type is configured as 'old'"
exit "${ST_UK}"
elif [ "${group}" == "" ]; then
echo "Argument Error: -g/--group missing"
exit "${ST_UK}"
elif [ "${warning}" == "" ]; then
echo "Argument Error: -w/--warning missing"
exit "${ST_UK}"
elif [ "${critical}" == "" ]; then
echo "Argument Error: -c/--critical missing"
exit "${ST_UK}"
elif echo "${warning}" | grep -v "^[0-9]*$"; then
echo "Argument Error: -w/--warning must be a number, given: ${warning}"
exit "${ST_UK}"
elif echo "${critical}" | grep -v "^[0-9]*$"; then
echo "Argument Error: -c/--critical must be a number, given: ${critical}"
exit "${ST_UK}"
elif [ "${warning}" -gt "${critical}" ]; then
echo "Argument Error: value for warning (${warning}) must be less than or equal to critical (${critical})"
exit "${ST_UK}"
fi
#### Function Definitions #####################################################
fetch_new_consumer_details() {
# Argument: None
# Returns:
# Kafka consumer details for the given consumer group
"${KAFKA_HOME}/bin/kafka-consumer-groups.sh" \
--new-consumer \
--bootstrap-server "${brokers}" \
--group "${group}" \
--describe 2>/dev/null
}
fetch_old_consumer_details() {
# Argument: None
# Returns:
# Kafka consumer offset details for the given consumer group & topic
if ! [ -f "${KAFKA_HOME}/bin/kafka-consumer-offset-checker.sh" ]; then
KAFKA_HOME="${DEFAULT_KAFKA_HOME}"
fi
"${KAFKA_HOME}/bin/kafka-consumer-offset-checker.sh" \
--group "${group}" \
--topic "${topic}" \
--zookeeper "${zookeeper}" 2>/dev/null
}
echo_details() {
msg="${1}"
if [ "${topic}" != "" ]; then
msg="${msg}, topic '${topic}'"
fi
echo "${msg}, group '${group}'"
}
calculate_consumer_lag() {
# Argument: consumer details
# <result from fetch_new_consumer_details or fetch_old_consumer_details function>
# Returns:
# Sum of lag from all partitions
expression=""
if [ "${ctype}" == "new" ]; then
expression="$(echo "$1" | grep -v -e "^TOPIC\s" -e "^Group\s" -e "is rebalancing" -e "^\s*$" | awk '{print $5}' | grep -v -e "^\s*$" -e "^\s*-[0-9]*\s*$" | tr "\n" "+" | sed 's/+$/\n/')"
else
expression="$(echo "$1" | grep -v -e "^TOPIC\s" -e "^Group\s" -e "is rebalancing" -e 'org.apache.zookeeper.KeeperException$NoNodeException' -e "^\s*$" | awk '{print $6}' | grep -v -e "^\s*$" -e "^\s*-[0-9]*\s*$" | tr "\n" "+" | sed 's/+$/\n/')"
fi
if [ "${expression}" == "" ]; then
echo_details "WARNING- no numeric lag value found for lag calculation"
exit "${ST_WR}"
elif echo "${expression}" | grep -v "+"; then
echo "${expression}"
else
echo "${expression}" | bc
fi
}
#### EXECUTE CHECK ############################################################
if [ "${ctype}" == "new" ]; then
c_details="$(fetch_new_consumer_details)"
if [ $? != 0 ]; then
echo_details "CRITICAL- kafka-consumer-groups.sh script returned non-zero exit status"
exit "${ST_CR}"
elif echo "${c_details}" | grep "^Error: Executing consumer group command failed due to Error reading field 'version': java.nio.BufferUnderflowException" >/dev/null; then
echo_details "WARNING- fetch error. ${c_details}"
exit "${ST_WR}"
elif echo "${c_details}" | grep "^Error: Consumer group '[a-zA-Z0-9_-]*' does not exist" >/dev/null; then
echo_details "WARNING- fetch error. ${c_details}"
exit "${ST_WR}"
elif echo "${c_details}" | grep "^Error: " >/dev/null; then
echo_details "CRITICAL- fetch error. ${c_details}"
exit "${ST_CR}"
fi
else
c_details="$(fetch_old_consumer_details)"
if [ $? != 0 ]; then
echo_details "CRITICAL- kafka-consumer-offset-checker.sh script returned non-zero exit status"
exit "${ST_CR}"
elif echo "${c_details}" | grep "^Error: " >/dev/null; then
echo_details "CRITICAL- fetch error. ${c_details}"
exit "${ST_CR}"
elif echo "${c_details}" | grep "^Exiting due to: Unable to connect to" >/dev/null; then
echo_details "CRITICAL- fetch error. ${c_details}"
exit "${ST_CR}"
fi
fi
lag="$(calculate_consumer_lag "${c_details}")"
exit_status=$?
if ([ ${exit_status} == ${ST_CR} ] || [ ${exit_status} == ${ST_WR} ]) && [ "${lag}" != "" ]; then
echo "${lag}"
exit ${exit_status}
elif [ ${exit_status} != 0 ] || [ "${lag}" == "" ]; then
echo_details "CRITICAL- error while calculating consumer lag"
exit "${ST_CR}"
fi
if [ "${lag}" -ge "${critical}" ]; then
echo_details "CRITICAL- kafka consumer lag is ${lag} (> ${critical})"
exit "${ST_CR}"
elif [ "${lag}" -ge "${warning}" ]; then
echo_details "WARNING- kafka consumer lag is ${lag} (> ${warning})"
exit "${ST_WR}"
else
echo_details "OK- kafka consumer lag is ${lag}"
exit "${ST_OK}"
fi