Last active
December 28, 2021 15:14
-
-
Save Mortimerp9/62277f79682ec612b75d to your computer and use it in GitHub Desktop.
Check kafka lag and queue growth with nagios/nrpe and KafkaOffsetMonitor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#inspired by https://github.com/vide/nagios-scripts/blob/master/check_kafka_lag.sh | |
function printHelp() { | |
cat >&2 <<EOF | |
$@ | |
--warning|-w Warning threshold | |
--critical|-c Critical threshold | |
--zk|-z Zookeper connection string | |
--group|-g Set Kafka group to monitor | |
--jar|-j path to the KafkaOffsetMonitor assembly | |
EOF | |
exit 2 | |
} | |
# main starts here | |
while [ $# -gt 0 ] | |
do | |
case "$1" in | |
--warning|-w) WARNING="$2"; shift 2;; | |
--critical|-c) CRITICAL="$2"; shift 2;; | |
--zk|-z) ZK="$2"; shift 2;; | |
--group|-g) GROUP="$2"; shift 2;; | |
--jar|-j) JAR="$2"; shift 2;; | |
*) printHelp "Missing parameter" ;; | |
esac | |
done | |
if [ -z "${WARNING}" ]; | |
then | |
printHelp "Please specify a warning threshold" | |
fi | |
if [ -z "${CRITICAL}" ]; | |
then | |
printHelp "Please specify a critical threshold" | |
fi | |
if [ -z "${ZK}" ]; | |
then | |
printHelp "Please specify a Zookeper connect string" | |
fi | |
if [ -z "${GROUP}" ]; | |
then | |
printHelp "Please specify a Kafka group" | |
fi | |
OFFSETS=`java -cp $JAR com.quantifind.kafka.offsetapp.OffsetGetterApp --group $GROUP --zk $ZK --sumPart true --onlyOffsets true 2> /dev/null | grep -e '\([1-9]\|[0-9]0\) *$'` | |
MAX=$(echo "$OFFSETS"|awk '$5 > max {max=$5; maxline=$0}; END{ print maxline}') | |
MAX_LAG=$(echo $MAX | cut -d ' ' -f5) | |
MAX_TOPIC=$(echo $MAX | cut -d ' ' -f1) | |
if [ -z "$MAX_LAG" ]; | |
then | |
echo "UNKNOW" | |
exit 3 | |
fi | |
if (( MAX_LAG >= ${CRITICAL} )) | |
then | |
echo "$MAX_TOPIC HAS CRITICAL LAG|$MAX_TOPIC=$MAX_LAG" | |
echo "$OFFSETS" | |
exit 2 | |
else | |
if (( MAX_LAG >= ${WARNING} )) | |
then | |
echo "$MAX_TOPIC HAS WORRISOME LAG|$MAX_TOPIC=$MAX_LAG" | |
echo "$OFFSETS" | |
exit 1 | |
fi | |
echo "LAG IS FINE|$MAX_TOPIC=$MAX_LAG" | |
echo "$OFFSETS" | |
exit 0 | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#inspired by https://github.com/vide/nagios-scripts/blob/master/check_kafka_lag.sh | |
function printHelp() { | |
cat >&2 <<EOF | |
$@ | |
--warning|-w Warning threshold | |
--critical|-c Critical threshold | |
--zk|-z Zookeper connection string | |
--group|-g Set Kafka group to monitor | |
--topics|-t Set Kafka topics to monitor | |
--jar|-j path to the KafkaOffsetMonitor assembly | |
--dat|-d filname for dat file | |
EOF | |
exit 2 | |
} | |
# main starts here | |
while [ $# -gt 0 ] | |
do | |
case "$1" in | |
--warning|-w) WARNING="$2"; shift 2;; | |
--critical|-c) CRITICAL="$2"; shift 2;; | |
--zk|-z) ZK="$2"; shift 2;; | |
--group|-g) GROUP="$2"; shift 2;; | |
--topics|-t) TOPICS="$2"; shift 2;; | |
--jar|-j) JAR="$2"; shift 2;; | |
--dat|-d) DAT="$2"; shift 2;; | |
*) printHelp "Missing parameter" ;; | |
esac | |
done | |
if [ -z "${WARNING}" ]; | |
then | |
printHelp "Please specify a warning threshold" | |
fi | |
if [ -z "${CRITICAL}" ]; | |
then | |
printHelp "Please specify a critical threshold" | |
fi | |
if [ -z "${ZK}" ]; | |
then | |
printHelp "Please specify a Zookeper connect string" | |
fi | |
if [ -z "${GROUP}" ]; | |
then | |
printHelp "Please specify a Kafka group" | |
fi | |
if [ -z "${DAT}" ]; | |
then | |
printHelp "Please specify a temp filepattern" | |
fi | |
if [ -z "${TOPICS}" ]; | |
then | |
printHelp "Please specify a list of Kafka topics" | |
fi | |
DATFILE="/tmp/${DAT}_logsize.dat" | |
OLD_SIZE=`cat $DATFILE` | |
OFFSETS=`java -cp $JAR com.quantifind.kafka.offsetapp.OffsetGetterApp --group $GROUP --zk $ZK --sumPart true --onlyOffsets true --topics $TOPICS 2> /dev/null | awk '{print $2,$4}' | sort` | |
if [ -z "$OLD_SIZE" ]; | |
then | |
echo "$OFFSETS"| sort > $DATFILE; | |
echo "UNKNOWN -- first run"; | |
exit 3; | |
else | |
JOIN=`echo "$OFFSETS" | join -1 1 -2 1 -a 1 -e 0 - $DATFILE | awk '{print $1,$2-$3}'` | |
echo "$OFFSETS" | sort > $DATFILE; | |
MIN=$(echo "$JOIN" | awk '$2 >= min {min=$2; minline=$0}; END{ print minline}') | |
MIN_SIZE=$(echo $MIN | cut -d ' ' -f2) | |
MIN_TOPIC=$(echo $MIN | cut -d ' ' -f1) | |
if [ -z "$MIN_SIZE" ]; | |
then | |
echo "UNKNOW" | |
exit 3 | |
fi | |
if (( MIN_SIZE <= ${CRITICAL} )) | |
then | |
echo "$MIN_TOPIC HASN'T BEEN GROWING!|$MIN_TOPIC=$MIN_SIZE" | |
echo "$JOIN" | |
exit 2 | |
else | |
if (( MIN_SIZE <= ${WARNING} )) | |
then | |
echo "$MIN_TOPIC HAS WORRISOME GROWTH|$MIN_TOPIC=$MIN_SIZE" | |
echo "$JOIN" | |
exit 1 | |
fi | |
echo "KAFKA IS GOOD|$MIN_TOPIC=$MIN_SIZE" | |
echo "$JOIN" | |
exit 0 | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment