zabbix报警信息发送--python脚本

创建报警媒介

脚本路径可以在server的配置文件中配置

[root@localhost]# cat /etc/zabbix/zabbix_server.conf |grep "alertscripts"
# AlertScriptsPath=${datadir}/zabbix/alertscripts
AlertScriptsPath=/usr/lib/zabbix/alertscripts

[root@localhost]# ls /usr/lib/zabbix/alertscripts/
zabbix_alert.sh

输入脚本名称,类型选择脚本并添加以下3个参数,分别.sh脚本需要的3个参数:收件人地址、主题、详细内容

{ALERT.SENDTO}  
{ALERT.SUBJECT}  
{ALERT.MESSAGE}

2.为用户添加报警媒介,这里就不创建新的用户,而是用admin用户报警

3.创建动作并进行配置

配置->动作->创建动作

 

 

动作参数说明

"host":"{HOST.NAME}"
"time":"{EVENT.DATE} {EVENT.TIME}"
"alert_degree":"{TRIGGER.SEVERITY}"
"alert_massage": "{HOST.NAME}:{TRIGGER.NAME}"
"alert_project":"{TRIGGER.KEY1}"
"details":"{ITEM.NAME}:{ITEM.VALUE}"
"current_state":"{TRIGGER.STATUS}:{ITEM.VALUE1}"
"description":"{TRIGGER.DESCRIPTION}"
"event_ID":"{EVENT.ID}"

二.脚本的编写以及说明

zabbix_alter.sh

#!/bin/bash
python /usr/lib/zabbix/alertscripts/send_message.py "$1" "$2" "$3"

send_message.py

#!/usr/bin/env python3
import requests
import json
import sys

submit_url='http://192.168.1.45:8080/alarm_notice'
sendto = sys.argv[1]
subject = sys.argv[2]
message = sys.argv[3]
headers = {
        'Content-Type':'application/json;charset=UTF-8'
}
data = {
    "subject":subject,
    "message":message
    }
response = requests.post(url=submit_url,headers=headers,data=json.dumps(data))
print(response.text)

给脚本添加运行权限:

chmod +x zabbix_alter.sh

chmod +x send_message.py

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于 Python 实现的 Zabbix 微信报警脚本,需要先安装 WeixinBot 和 requests 库: ```python #!/usr/bin/env python # -*- coding: utf-8 -*- import requests import json import sys class WeChat(object): def __init__(self, corpid, corpsecret): self.url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken' self.corpid = corpid self.corpsecret = corpsecret self.params = { 'corpid': self.corpid, 'corpsecret': self.corpsecret } self.access_token = self.get_access_token() def get_access_token(self): r = requests.get(self.url, params=self.params) return r.json()['access_token'] def send_message(self, message, agentid, touser=None, toparty=None): url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send' data = { "touser": touser, "toparty": toparty, "msgtype": "text", "agentid": agentid, "text": { "content": message }, "safe": 0 } params = { 'access_token': self.access_token } headers = {'Content-Type': 'application/json;charset=utf-8'} r = requests.post(url, params=params, data=json.dumps(data), headers=headers) return r.json() if __name__ == "__main__": corpid = 'YOUR_CORPID' corpsecret = 'YOUR_CORPSECRET' agentid = 'YOUR_AGENTID' touser = sys.argv[1] subject = sys.argv[2] content = sys.argv[3] message = u"收件人:%s\n主题:%s\n内容:%s" % (touser, subject, content) wechat = WeChat(corpid, corpsecret) response = wechat.send_message(message, agentid, touser=touser) print(response) ``` 使用方法: 1. 用企业微信扫描二维码,加入企业; 2. 创建一个应用,并记录下应用的 `corpid`、`corpsecret`和 `agentid`; 3. 安装 WeixinBot 和 requests 库; 4. 将上述代码保存到文件 `wechat.py` 中; 5. 在 Zabbix 中配置告警媒介,将 `wechat.py` 的路径作为脚本名,并在参数中依次填入收件人、主题和内容。例如:`/usr/bin/python /path/to/wechat.py {ALERT.SENDTO} "{ALERT.SUBJECT}" "{ALERT.MESSAGE}"`; 6. 测试告警是否正常发送

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值