zabbix alarm information pushed to kafka

Posted by lanas on Tue, 26 May 2020 19:11:42 +0200

Application scenario

Due to the high security requirements of the company where the friend is located, zabbix's network environment cannot be connected to the Internet, so it is not possible to send the alarm directly to some instant messaging tools through zabbix, which requires sending the alarm message to some middleware and forwarding it through the middleware. Here, kafka is selected, of course, there are not only alarm messages in kafka, but also other needs to be sent That's just a lot of data

Basic environment configuration

kafka cluster has been deployed, installation details will not be introduced here

vim /etc/hosts
192.168.179.133 kafka3
192.168.179.132 kafka2
192.168.179.131 kafka1

Start zookeeper and kafka

In order to enable consumers to consume the data in the broker evenly, I set 6 partitions and 3 replicas here

start-up 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 topics
bin/kafka-topics.sh --create --zookeeper kafka1:2181,kafka2:2181,kafka3:2181 --replication-factor 3 --partitions 6 --topic zabbix-alert
bin/kafka-topics.sh --list --bootstrap-server 192.168.179.132:9092

Write a script to send the alarm information to kafka

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()

Write a script to receive the alarm information and send it to the enterprise wechat

vim /usr/lib/zabbix/alertscripts/receive.py
#!/usr/bin/python
#coding=utf-8
from kafka import KafkaConsumer
import json,requests
import sys
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
reload(sys)
sys.setdefaultencoding('utf-8')
corpid='ww95f3a6ffd22fee8b8'
secret='iJjHQxTCjIMyW7ZjziaULcxqA7Hg2fjcLM2ssdvwY1Zc'
agentid='1000002' 
'''
    //Consume data from ZABBIX alert topics
    //Note: to read data in json format, add value_deserializer parameter
'''
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)
        token = r.json()['access_token']
        return token
def sendweixin(token):
        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=json.dumps(message.value,ensure_ascii=False)
        messages=message.value
        sendweixin(token)

Put this script in the background to run continuously

nohup python alert_receive.py & >> alert.log &

ZABBIX action configuration

Create alarm media

Configure user alarm media

The recipient is the user id of the enterprise wechat

Configure actions


Trigger alarm for test after configuration

The effect is as follows

Welcome to pay attention to the "operation and maintenance development story" of personal company

Topics: Linux kafka Zabbix JSON Zookeeper