Skip to content

Commit f0e69fc

Browse files
committed
Add healthcheck for kraft controller/update jmx rules
1 parent f25605c commit f0e69fc

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
3+
# This script returns a successful exit code (0) if the controller is a follower or leader. For any other state, it returns a failure exit code (1).
4+
5+
JMX_ENDPOINT="http://localhost:9020/metrics"
6+
METRIC_PREFIX="kafka_server_raft_metrics_current_state_"
7+
8+
# Fetch the matching current-state metric with value of 1.0 from the JMX endpoint
9+
MATCHING_METRIC=$(curl -s "$JMX_ENDPOINT" | grep "^${METRIC_PREFIX}" | awk '$2 == 1.0 {print $1}')
10+
11+
# If it's not empty, it means we found a metric with a value of 1.0.
12+
if [ -n "$MATCHING_METRIC" ]; then
13+
# Determine the state of the controller using the last field name of the metric
14+
# Possible values are leader, candidate, voted, follower, unattached, observer
15+
STATE=$(echo "$MATCHING_METRIC" | rev | cut -d'_' -f1 | rev)
16+
17+
# Check if the extracted state is 'leader' or 'follower'
18+
if [ "$STATE" == "leader" ] || [ "$STATE" == "follower" ]; then
19+
echo "The controller is in a healthy quorum state."
20+
exit 0
21+
else
22+
# Any other state (e.g., 'candidate', 'unattached', 'observer') is not considered healthy
23+
echo "Warning: The controller is not in a healthy state for this check."
24+
exit 1
25+
fi
26+
else
27+
echo "Failure: No active Kraft controller state found with a value of 1.0."
28+
exit 1
29+
fi

0 commit comments

Comments
 (0)