Forward Zabbix Alerts to WeChat via Kafka – Complete Step‑by‑Step Guide
This guide shows how to route Zabbix alarm messages through a Kafka cluster and then deliver them to Enterprise WeChat using Python scripts, covering host configuration, Kafka/Zookeeper startup, topic creation, alert‑sending scripts, and Zabbix action setup.
Application scenario Because the target company has strict security requirements, the Zabbix server cannot access the Internet, so alerts cannot be sent directly to instant‑messaging tools. The solution is to forward alerts to a middleware—Kafka—and let another component push them to the desired channel.
Basic environment A Kafka cluster is already deployed (installation details omitted).
<code>vim /etc/hosts
192.168.179.133 kafka3
192.168.179.132 kafka2
192.168.179.131 kafka1</code>Start Zookeeper and Kafka To allow consumers to evenly consume data, a topic with 6 partitions and 3 replicas is created.
<code># Start Zookeeper
cd /root/kafka_2.12-2.4.1
nohup bin/zookeeper-server-start.sh config/zookeeper.properties &>> zookeeper.log &
# Start Kafka
nohup bin/kafka-server-start.sh config/server.properties &>> kafka.log &
# Create topic
bin/kafka-topics.sh --create --zookeeper kafka1:2181,kafka2:2181,kafka3:2181 \
--replication-factor 3 --partitions 6 --topic zabbix-alert
# List topics
bin/kafka-topics.sh --list --bootstrap-server 192.168.179.132:9092</code>Script to send Zabbix alerts to Kafka
<code>vim /usr/lib/zabbix/alertscripts/alert_kafka.py
chmod +x /usr/lib/zabbix/alertscripts/alert_kafka.py
#!/usr/bin/python
#coding=utf-8
from kafka import KafkaProducer
import json, sys
receive = sys.argv[1]
message = sys.argv[2]
producer = KafkaProducer(
value_serializer=lambda v: json.dumps(v).encode('utf-8'),
bootstrap_servers=['192.168.179.132:9092','192.168.179.133:9092','192.168.179.134:9092']
)
data = {"receive": receive, "alert": message}
producer.send('zabbix-alert', data)
producer.close()</code>Script to consume alerts from Kafka and push to Enterprise WeChat
<code>vim /usr/lib/zabbix/alertscripts/receive.py
#!/usr/bin/python
#coding=utf-8
from kafka import KafkaConsumer
import json, requests, sys
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
reload(sys)
sys.setdefaultencoding('utf-8')
corpid = 'ww95f3a6ffd22fiyhe8b8'
secret = 'iJjHQxTCjIMyW7ZjzityucxqA7Hg2fjcLM2ssdvwY1Zc'
agentid = '1000002'
# Consume messages from the zabbix-alert topic
consumer = KafkaConsumer('zabbix-alert', group_id='zabbix-alert',
bootstrap_servers=['192.168.179.132:9092','192.168.179.133:9092','192.168.179.134:9092'],
auto_offset_reset='earliest', value_deserializer=json.loads)
def gettoken():
tokenurl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
data = {"corpid": corpid, "corpsecret": secret}
r = requests.get(url=tokenurl, params=data, verify=False)
return r.json()['access_token']
def sendweixin(token, messages):
wechaturl = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % token
data = {
"touser": messages['receive'],
"msgtype": "text",
"agentid": agentid,
"text": {"content": messages['alert']},
"safe": "0"
}
headers = {'content-type': 'application/json'}
req = requests.post(url=wechaturl, headers=headers, json=data, verify=False)
print(req.text)
return req
for message in consumer:
token = gettoken()
messages = message.value
sendweixin(token, messages)</code>Run the consumer script in the background:
<code>nohup python alert_receive.py &>> alert.log &</code>Zabbix action configuration
Create alert media
Configure user media
Set the recipient to the Enterprise WeChat user ID.
Configure action
Test the alert after configuration
The resulting notification appears in Enterprise WeChat as shown below.
Ops Development Stories
Maintained by a like‑minded team, covering both operations and development. Topics span Linux ops, DevOps toolchain, Kubernetes containerization, monitoring, log collection, network security, and Python or Go development. Team members: Qiao Ke, wanger, Dong Ge, Su Xin, Hua Zai, Zheng Ge, Teacher Xia.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.