首頁 > 軟體

Zabbix對Kafka topic積壓資料監控的問題(bug優化)

2022-07-01 22:03:19

簡述

《Zabbix對Kafka topic積壓資料監控》一文的目的是通過Zabbix自動發現實現對多個消費者組的Topic及Partition的Lag進行監控。因在實際監控中發現有問題,為給感興趣的讀者不留坑,特通過此文對監控進行優化調整。

分割區自動發現

# 未優化前的計算方式:
# 自動發現組態檔
vim consumer-groups.conf
#按消費者組(Group)|Topic格式,寫入自動發現組態檔
test-group|test
# 執行指令碼自動發現指定消費者和topic的分割區
bash consumer-groups.sh discovery
{
    "data": [
        { "{#GROUP}":"test-group", "{#TOPICP}":"test", "{#PARTITION}":"0" },
        { "{#GROUP}":"test-group", "{#TOPICP}":"test", "{#PARTITION}":"1" },
        { "{#GROUP}":"test-group", "{#TOPICP}":"test", "{#PARTITION}":"3" },
        { "{#GROUP}":"test-group", "{#TOPICP}":"test", "{#PARTITION}":"2" }
    ]
}

經過上線驗證,當自動發現組態檔只有一個test-group|test是沒有問題的,但當我們按需求再接入test-group|test1 (即test-group消費者組的第二個Topic)時,自動發現的結果如下:

# 未優化前的計算方式:
vim consumer-groups.conf
#按消費者組(Group)|Topic格式,寫入自動發現組態檔
test-group|test
test-group|test1

# 執行指令碼自動發現指定消費者和topic的分割區
bash consumer-groups.sh discovery
{
    "data": [
        { "{#GROUP}":"test-group", "{#TOPICP}":"test", "{#PARTITION}":"0" },
        { "{#GROUP}":"test-group", "{#TOPICP}":"test", "{#PARTITION}":"1" },
        { "{#GROUP}":"test-group", "{#TOPICP}":"test", "{#PARTITION}":"3" },
        { "{#GROUP}":"test-group", "{#TOPICP}":"test", "{#PARTITION}":"2" }
        { "{#GROUP}":"test-group", "{#TOPICP}":"test1", "{#PARTITION}":"0" },
        { "{#GROUP}":"test-group", "{#TOPICP}":"test2", "{#PARTITION}":"1" },
        { "{#GROUP}":"test-group", "{#TOPICP}":"test3", "{#PARTITION}":"2" }
    ]
}

瞭解Zabbix自動發現格式的同學會發現,每個Topic的Partition會出現',',這種格式是不符合規範,這就是導致我們的監控項會出現問題,因此我們需要進一步修改指令碼。

經修改後,最終效果應該如下:

# 優化後的計算方式:
vim consumer-groups.conf
#按消費者組(Group)|Topic格式,寫入自動發現組態檔
test-group|test
test-group|test1

# 執行指令碼自動發現指定消費者和topic的分割區
bash consumer-groups.sh discovery
{
    "data": [
        { "{#GROUP}":"test-group", "{#TOPICP}":"test", "{#PARTITION}":"0" },
        { "{#GROUP}":"test-group", "{#TOPICP}":"test", "{#PARTITION}":"1" },
        { "{#GROUP}":"test-group", "{#TOPICP}":"test", "{#PARTITION}":"3" },
        { "{#GROUP}":"test-group", "{#TOPICP}":"test", "{#PARTITION}":"2" },
        { "{#GROUP}":"test-group", "{#TOPICP}":"test1", "{#PARTITION}":"0" },
        { "{#GROUP}":"test-group", "{#TOPICP}":"test1", "{#PARTITION}":"1" },
        { "{#GROUP}":"test-group", "{#TOPICP}":"test1", "{#PARTITION}":"2" }
    ]
}

獲取監控項“test-group/test/分割區X”的Lag

經過自動發現後的資料,我們可以進一步獲取不同分割區的lag

# 優化後的計算方式:
# test-group test分割區0 lag
bash consumer-groups.sh lag test-group test 0
# test-group test分割區1 lag
bash consumer-groups.sh lag test-group test 1
# test-group test1分割區0 lag
bash consumer-groups.sh lag test-group test1 0

通過命令可以看到,我們的引數通過消費者組、Topic、Partition來獲取最終的lag值,如果不加消費者區分,那麼無法區分不同消費者組和不同Topic相應的lag結果:

# 未優化前的計算方式:
# 獲取分割區0 lag
bash consumer-groups.sh lag 0
# 獲取分割區1 lag
bash consumer-groups.sh lag 1
# 獲取分割區2 lag
bash consumer-groups.sh lag 2
# 獲取分割區3 lag
bash consumer-groups.sh lag 3

最終優化後指令碼

# 自動發現組態檔
vim consumer-groups.conf
#按消費者組(Group)|Topic格式,寫入自動發現組態檔
test-group|test
test-group|test1

# 自動發現、lag計算指令碼
vim consumer-groups.sh
#!/bin/bash
##comment: 根據消費者組監控topic lag,進行監控告警
#組態檔說明
#消費者組|Topic
#test-group|test

#獲取topic 資訊
cal_topic() {
    if [ $# -ne 2 ]; then
        echo "parameter num error, 讀取topic資訊失敗"
        exit 1
    else
        /usr/local/kafka/bin/./kafka-consumer-groups.sh --bootstrap-server 192.168.3.55:9092 --describe --group $1 |grep -w $2|grep -v none 
    fi
}
#topic+分割區自動發現
topic_discovery() {
    printf "{n"
    printf "t"data": [n"
    m=0
    num=`cat /etc/zabbix/monitor_scripts/consumer-groups.conf|wc -l`
    for line in `cat /etc/zabbix/monitor_scripts/consumer-groups.conf`
    do  
        m=`expr $m + 1`
        group=`echo ${line} | awk -F'|' '{print $1}'`
        topic=`echo ${line} | awk -F'|' '{print $2}'`
        cal_topic $group $topic > /tmp/consumer-group-tmp
        count=`cat /tmp/consumer-group-tmp|wc -l`
        n=0
        while read line
        do
             n=`expr  $n + 1`
             #判斷最後一行
             if [ $n -eq $count ] && [ $m -eq $num ]; then
                 topicp=`echo $line | awk '{print $1}'`
                 partition=`echo $line  | awk '{print $2}'`
                 printf "tt{ "{#GROUP}":"${group}", "{#TOPICP}":"${topicp}", "{#PARTITION}":"${partition}" }n"
             else
                 topicp=`echo $line | awk '{print $1}'`
                 partition=`echo $line  | awk '{print $2}'`
                 printf "tt{ "{#GROUP}":"${group}", "{#TOPICP}":"${topicp}", "{#PARTITION}":"${partition}" },n"
             fi
        done < /tmp/consumer-group-tmp
    done
    printf "t]n"
    printf "}n"
}


if [ $1 == "discovery" ]; then
    topic_discovery
elif [ $1 == "lag" ];then
    cal_topic $2 $3 > /tmp/consumer-group
    cat /tmp/consumer-group |awk -v t=$3 -v p=$4 '{if($1==t && $2==p ){print $5}}'
else
    echo "Usage: /data/scripts/consumer-group.sh discovery | lag"
fi

# 手動執行
## 自動發現
bash consumer-groups.sh discovery
## test-group test分割區0 lag
bash consumer-groups.sh lag test-group test 0

接入Zabbix

1.Zabbix組態檔

vim userparameter_kafka.conf
UserParameter=topic_discovery,bash /data/scripts/consumer-groups.sh discovery
UserParameter=topic_log[*],bash /data/scripts/consumer-groups.sh lag "$1" "$2" "$3"

2.Zabbix自動發現

3.監控項設定

4.告警資訊

告警主機:Kafka_192.168.3.55
主機IP:192.168.3.55
主機組:Kafka
告警時間:2022.03.21 00:23:10
告警等級:Average
告警資訊:test-group/test/分割區1:資料積壓100
告警專案:topic_lag[test-group,test,1]
問題詳情:
test-group/test/1: 62

到此這篇關於Zabbix對Kafka topic積壓資料監控的文章就介紹到這了,更多相關Zabbix積壓資料監控內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com