标签:rod host exception use verify 环境 params broker param
应用场景由于朋友所在公司对安全性要求较高,zabbix所在的网络环境不能上外网,因此不能通过zabbix将告警直接发送至一些即时通讯工具,这就需要将报警消息发送至一些中间件,并通过中间件转发出去,这里选择使用了kafka,当然kafka中不只有报警信息,也有其他需要发送的数据,这里就不过多透漏
kafka集群已部署好,这里不介绍安装细节
vim /etc/hosts
192.168.179.133 kafka3
192.168.179.132 kafka2
192.168.179.131 kafka1
为了能让消费者均匀消费broker里的数据,这里我设置了6分区三副本
启动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
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()
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‘
‘‘‘
消费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)
将此脚本放入后台持续运行
nohup python alert_receive.py & >> alert.log &
收件人为企业微信的用户id
效果如下
欢迎关注个人公号“运维开发故事”
标签:rod host exception use verify 环境 params broker param
原文地址:https://blog.51cto.com/12970189/2498411