Zabbix配置邮箱报警
打开邮箱SMTP服务(163邮箱为例)


创建报警媒介

消息模板



# 告警模板
主题:
监控告警: {EVENT.NAME}
消息:
主机: {HOST.NAME1}
时间: {EVENT.DATE} {EVENT.TIME}
级别: {TRIGGER.SEVERITY}
触发: {TRIGGER.NAME}
详情: {ITEM.NAME1}:{ITEM.KEY1}:{ITEM.VALUE1}
状态: {TRIGGER.STATUS}
项目:{TRIGGER.KEY1}
事件ID:{EVENT.ID}
# 恢复模板
主题:
告警恢复: {EVENT.DURATION}: {EVENT.NAME}
消息:
主机: {HOST.NAME1}
时间: {EVENT.DATE} {EVENT.TIME}
级别: {TRIGGER.SEVERITY}
触发: {TRIGGER.NAME}
详情: {ITEM.NAME1}:{ITEM.KEY1}:{ITEM.VALUE1}
状态: {TRIGGER.STATUS}
测试告警媒介




媒介绑定用户


告警媒介绑定动作




测试
停止agent端的nginx进行
systemctl stop nginx

systemctl start nginx


Zabbix配置钉钉报警
创建钉钉机器人
查找本地IP

在钉钉群聊中创建自定义机器人


添加报警脚本
vim /usr/lib/zabbix/alertscripts/dingding.py
#!/usr/bin/env python3
#coding:utf-8
#zabbix报警
import requests,json,sys,os,datetime
# 更换机器人webhook
webhook="https://oapi.dingtalk.com/robot/send?access_token=13b4395253b507cb6281c48d5677a0c05a5d9b4ad2fd4b4140adc49d5f5955e"
user=sys.argv[1]
text=sys.argv[3]
data={
"msgtype": "text",
"text": {
"content": text
},
"at": {
"atMobiles": [
user
],
"isAtAll": False
}
}
headers = {'Content-Type': 'application/json'}
x=requests.post(url=webhook,data=json.dumps(data),headers=headers)
if os.path.exists("/var/log/zabbix/dingding.log"):
f=open("/var/log/zabbix/dingding.log","a+")
else:
f=open("/var/log/zabbix/dingding.log","w+")
f.write("\n"+"--"*30)
if x.json()["errcode"] == 0:
f.write("\n"+str(datetime.datetime.now())+" "+str(user)+" "+"发送成功"+"\n"+str(text))
f.close()
else:
f.write("\n"+str(datetime.datetime.now()) + " " + str(user) + " " + "发送失败" + "\n" + str(text))
f.close()
chmod +x /usr/lib/zabbix/alertscripts/dingding.py
touch /var/log/zabbix/dingding.log
chown zabbix.zabbix /var/log/zabbix/dingding.log
安装python3环境
yum -y install phthon3 epel-release python-pip
pip3 install requests

测试脚本
cd /usr/lib/zabbix/alertscripts
./dingding.py test test "钉钉"

Zabbix创建告警媒介

脚本参数
{ALERT.SENDTO}
{ALERT.SUBJECT}
{ALERT.MESSAGE}
消息模板

# 主题
告警问题: {EVENT.NAME}
# 消息:
主机: {HOST.NAME1}
时间: {EVENT.DATE} {EVENT.TIME}
级别: {TRIGGER.SEVERITY}
触发: {TRIGGER.NAME}
详情: {ITEM.NAME1}:{ITEM.KEY1}:{ITEM.VALUE1}
状态: {TRIGGER.STATUS}
项目: {TRIGGER.KEY1}
事件ID:{EVENT.ID}

# 主题
告警恢复 {EVENT.DURATION}: {EVENT.NAME}
# 消息
主机: {HOST.NAME1}
时间: {EVENT.DATE} {EVENT.TIME}
级别: {TRIGGER.SEVERITY}
触发: {TRIGGER.NAME}
详情: {ITEM.NAME1}:{ITEM.KEY1}:{ITEM.VALUE1}
状态: {TRIGGER.STATUS}
项目: {TRIGGER.KEY1}
事件ID:{EVENT.ID}
测试告警媒介



告警媒介绑定用户

告警媒介绑定触发器动作





测试
来到agent端停止nginx
systemctl stop nginx


Zabbix配置企业微信报警
企业微信依然采用机器人的方式
创建企业微信机器人

添加报警脚本
vim /usr/lib/zabbix/alertscripts/weixin.py
#!/usr/bin/env python3
#coding:utf-8
#zabbix报警
import requests,json,sys,os,datetime
# 更改机器人webhook
webhook="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=64c2858d-efab-4bba-acb0-41f0e034eb3"
user=sys.argv[1]
text=sys.argv[3]
data={
"msgtype": "text",
"text": {
"content": text
},
"at": {
"atMobiles": [
user
],
"isAtAll": False
}
}
headers = {'Content-Type': 'application/json'}
x=requests.post(url=webhook,data=json.dumps(data),headers=headers)
if os.path.exists("/var/log/zabbix/weixin.log"):
f=open("/var/log/zabbix/weixin.log","a+")
else:
f=open("/var/log/zabbix/weixin.log","w+")
f.write("\n"+"--"*30)
if x.json()["errcode"] == 0:
f.write("\n"+str(datetime.datetime.now())+" "+str(user)+" "+"发送成功"+"\n"+str(text))
f.close()
else:
f.write("\n"+str(datetime.datetime.now()) + " " + str(user) + " " + "发送失败" + "\n" + str(text))
f.close()
chmod +x /usr/lib/zabbix/alertscripts/weixin.py
touch /var/log/zabbix/weixin.log
chown zabbix.zabbix /var/log/zabbix/weixin.log
“+“发送成功”+”\n"+str(text))
f.close()
else:
f.write(“\n”+str(datetime.datetime.now()) + " " + str(user) + " " + “发送失败” + “\n” + str(text))
f.close()
```bash
chmod +x /usr/lib/zabbix/alertscripts/weixin.py
touch /var/log/zabbix/weixin.log
chown zabbix.zabbix /var/log/zabbix/weixin.log
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战