zabbix告警发送到企业微信

[root@linux conf]# grep alertscripts /etc/zabbix/zabbix_server.conf
# AlertScriptsPath=${datadir}/zabbix/alertscripts
AlertScriptsPath=/usr/lib/zabbix/alertscripts
[root@linux ~]# cd /usr/lib/zabbix/
[root@linux zabbix]# cd alertscripts/
[root@linux alertscripts]# ls
wechat.py 
[root@linux alertscripts]# cat wechat.py 
#!/usr/bin/python
# -*- coding: utf-8 -*-
# zabbix notification confirmation script
# python2.7 or above
  
import requests
import json
import os
import sys
  
Toparty = "1"   #部门id
AgentID = 1000002     #应用id
  
#修改为企业CropID和Secret
CropID = 'ww4471cc7xxxcc005e5'
Secret = 'VbHecyMwT2qjfyV5Yn5xxxxHHE0mnATGtCT9BdxvY'
  
#获取Token
Gtoken ="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+ CropID + "&corpsecret=" + Secret
headers = {'Content-Type': 'application/json'}
json_data = json.loads(requests.get(Gtoken).content.decode())
token = json_data["access_token"]
  
#消息发送接口
Purl = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + token
  
#消息发送函数
def msg(title,message):
    weixin_msg = {
         "toparty" : Toparty,
         "msgtype" : "textcard",
         "agentid" : AgentID,
         "textcard" : {
             "title" : title,
             "description" : message,
             "url" : "www.wzlinux.com",
             "btntxt":"更多"
          }
      }
    print requests.post(Purl,json.dumps(weixin_msg),headers=headers)
  
if __name__ == '__main__':
    title = sys.argv[1]        #获取第一个参数
    message = sys.argv[2]      #获取第二个参数
    msg(title,message)

[root@linux alertscripts]# ls -ld wechat.py 
-rwxr-xr-x. 1 root root 1269 1月   9 10:54 wechat.py
[root@linux alertscripts]# ./wechat.py test test
<Response [200]>

[root@linux ~]# ss -tunlp |grep agentd
tcp    LISTEN     0      128       *:10050                 *:*                   users:(("zabbix_agentd",pid=19900,fd=4),("zabbix_agentd",pid=19899,fd=4),("zabbix_agentd",pid=19898,fd=4),("zabbix_agentd",pid=19897,fd=4),("zabbix_agentd",pid=19896,fd=4),("zabbix_agentd",pid=19895,fd=4))
tcp    LISTEN     0      128      :::10050                :::*                   users:(("zabbix_agentd",pid=19900,fd=5),("zabbix_agentd",pid=19899,fd=5),("zabbix_agentd",pid=19898,fd=5),("zabbix_agentd",pid=19897,fd=5),("zabbix_agentd",pid=19896,fd=5),("zabbix_agentd",pid=19895,fd=5))
[root@linux ~]# kill 19896
[root@linux ~]# ss -tunlp |grep agentd

服务器:{HOST.NAME}发生: {TRIGGER.NAME}故障!
{
告警主机:{HOST.NAME}
告警地址:{HOST.IP}
监控项目:{ITEM.NAME}
监控取值:{ITEM.LASTVALUE}
告警等级:{TRIGGER.SEVERITY}
当前状态:{TRIGGER.STATUS}
告警信息:{TRIGGER.NAME}
告警时间:{EVENT.DATE} {EVENT.TIME}
事件ID:{EVENT.ID}
}

服务器:{HOST.NAME}: {TRIGGER.NAME}已恢复!
{
告警主机:{HOST.NAME}
告警地址:{HOST.IP}
监控项目:{ITEM.NAME}
监控取值:{ITEM.LASTVALUE}
告警等级:{TRIGGER.SEVERITY}
当前状态:{TRIGGER.STATUS}
告警信息:{TRIGGER.NAME}
告警时间:{EVENT.DATE} {EVENT.TIME}
恢复时间:{EVENT.RECOVERY.DATE} {EVENT.RECOVERY.TIME}
持续时间:{EVENT.AGE}
事件ID:{EVENT.ID}
}

服务器:{HOST.NAME}: 报警确认
{
确认人:{USER.FULLNAME} 
时间:{ACK.DATE} {ACK.TIME} 
确认信息如下:
"{ACK.MESSAGE}"
问题服务器IP:{HOSTNAME1}
问题ID:{EVENT.ID}
当前的问题是: {TRIGGER.NAME}
}


猜你喜欢

转载自blog.csdn.net/weixin_40018205/article/details/86151496