以kafka为例,需要先对 topic-parttion 做发现,脚本如下
cat topic_parttion_discovery.py
#!/usr/bin/env python
import json
import os
cmd=os.popen("""sh /usr/local/kafka/bin/kafka-topics.sh --describe --bootstrap-server 127.0.0.1:9092 --topic route001 | awk '/Leader/{print $2"-"$4}'""")
parttions=[]
for parttion in cmd.readlines():
r=parttion.strip()
parttions+=[{'{#PARTTION}':r}]
print json.dumps({'data':parttions},sort_keys=True,indent=4,separators=(',',':'))
集成到 agent 监控脚本
cat kafka_parttion_offset.sh
#!/bin/bash
if [ $# -eq 0 ];
then
python /etc/zabbix/scripts/topic_parttion_discovery.py
else
r="$(/usr/local/kafka/bin/kafka-consumer-groups.sh --bootstrap-server 127.0.0.1:9092 --describe --group route001 2>/dev/null)"
echo "$r" | awk 'NR>2 {print $2"-"$3,$5}' | awk '/'$1'/ {print $2}'
fi
cat kafka_parttion_lag.sh
#!/bin/bash
if [ $# -eq 0 ]
then
python /etc/zabbix/scripts/topic_parttion_discoovery.py
else
r="$(/usr/local/kafka/bin/kafka-consumer-groups.sh --bootstrap-server 127.0.0.1:9092 --describe --group route001 2>/dev/null)"
echo "$r" | awk 'NR>2 {print $2"-"$3,$6}' | awk '/'$1'/ {print $2}'
fi
添加自定义监控项,并重启服务
# kafka - consumer group
UnsafeUserParameters=1
AllowRoot=1
UserParameter=kafka.offset.parttiion[*],/etc/zabbix/scripts/kafka_parttion_offset.sh $1
UserParameter=kafka.lag.parttiion[*],/etc/zabbix/scripts/kafka_parttion_lag.sh $1
agent 端测试命令
/etc/zabbix/sbin/zabbix_agentd -t kafka.lag.parttiion
/etc/zabbix/sbin/zabbix_agentd -t kafka.lag.parttiion[route001-0]
web 端配置
- 添加模板
- 创建Discovery rules
key:
kafka.offset.parttiion
Fiters:
{#PARTTION} Regular expression: ^route001
- 创建监控项原型
key:
kafka.offset.parttiion[{#PARTTION},]
Update interval:
5m
key:
kafka.lag.parttiion[{#PARTTION},]
Update interval:
5m
- 关联模板到主机