应用场景 由于朋友所在公司对安全性要求较高,zabbix所在的网络环境不能上外网,因此不能通过zabbix将告警直接发送至一些即时通讯工具,这就需要将报警消息发送至一些中间件,并通过中间件转发出去,这里选择使用了kafka,当然kafka中不只有报警信息,也有其他需要发送的数据,这里就不过多透漏 基础环境配置 kafka集群已部署好,这里不介绍安装细节
代码语言:javascript复制vim /etc/hosts
192.168.179.133 kafka3
192.168.179.132 kafka2
192.168.179.131 kafka1
启动zookeeper以及kafka 为了能让消费者均匀消费broker里的数据,这里我设置了6分区三副本
代码语言:javascript复制启动zookeeper
cd /root/kafka_2.12-2.4.1
nohup bin/zookeeper-server-start.sh config/zookeeper.properties & >> zookeeper.log &
启动kafka
nohup bin/kafka-server-start.sh config/server.properties & >> kafka.log &
创建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
编写脚本将报警信息发送至kafka中
代码语言:javascript复制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()
编写脚本接收报警信息并发送至企业微信
代码语言:javascript复制关于企业微信的配置可查看专辑的其他文章 Double冬,公众号:没有故事的陈师傅在zabbix中实现发送带有图片的邮件和微信告警
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='ww95f3a6ffd22fiyhe8b8'
secret='iJjHQxTCjIMyW7ZjzityucxqA7Hg2fjcLM2ssdvwY1Zc'
agentid='1000002'
'''
消费zabbix-alert主题中的数据
注意事项:如需以json格式读取数据需加上value_deserializer参数
'''
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)
将此脚本放入后台持续运行
代码语言:javascript复制nohup python alert_receive.py & >> alert.log &
ZABBIX动作配置 创建报警媒介
配置用户告警媒介
收件人为企业微信的用户id
配置动作
配置完成后触发告警进行测试
效果如下