一、邮件告警(外部方式)
1.安装发送工具
yum install mail.rc -y
2.配置zabbix服务端外部邮箱
vim /etc/mail.rc //注意邮箱需要开启客户端授权码进行第三方登录
————文件末尾新增————
set from=xxxx@163.com\xxxx@qq.com
set smtp=smtp.163.com\smtp.qq.com
set smtp-auth-user=xxxx@163.com\xxxx@qq.com
set smtp-auth-password=xxxx//授权码
set smtp-auth=login
注:网易邮箱或QQ邮箱的授权码,POP3/SMTP必须打开

3.配置发送脚本
邮件必须在alertscripts目录下,否则识别不出来
目录: /usr/lib/zabbix/alertscripts

创建邮件脚本
vim mailx.sh
#!/bin/bash
#send mail
messages=`echo $3 | tr '\r\n' '\n'`
subject=`echo $2 | tr '\r\n' '\n'`
echo "${messages}" | mail -s "${subject}" $1 >>/tmp/mailx.log 2>&1
————注释————
$1:收件人邮箱地址;
$2:邮件标题;
$3:邮件内容

目录赋权
[root@zabbix-server alertscripts]# touch /tmp/mailx.log
[root@zabbix-server alertscripts]# chown -R zabbix.zabbix /tmp/mailx.log
[root@zabbix-server alertscripts]# chmod +x /usr/lib/zabbix/alertscripts/mailx.sh
[root@zabbix-server alertscripts]# chown -R zabbix.zabbix /usr/lib/zabbix
4.测试脚本
[root@zabbix-server alertscripts]# ./mailx.sh xxxx@qq.com “name” ”pyy”

5.配置 web 界面
点击 左侧 告警---媒介 右上角---创建媒介类型
脚本参数:
{ALERT.SENDTO} //宏变量_代表告警接收者地址
{ALERT.SUBJECT} //宏变量_告警主题
{ALERT.MESSAGE} //宏变量_表示告警详细信息

点击 消息模板 编辑

-=【告警通知】=-
事件名称:{EVENT.NAME}
告警地址:{HOST.NAME}
监控项目:{ITEM.NAME}
监控取值:{ITEM.LASTVALUE}
告警严重性:{EVENT.SEVERITY}
当前状态:{ITEM.LASTVALUE} {EVENT.NAME}
告警时间:{EVENT.DATE} {EVENT.TIME}
告警平台:http://192.168.97.150:8080
-=【恢复通知】=-
事件名称:{EVENT.NAME}
告警地址:{HOST.NAME}
监控项目:{ITEM.NAME}
告警严重性:{EVENT.SEVERITY}
当前状态:{ITEM.LASTVALUE}
告警时间:{EVENT.DATE} {EVENT.TIME}
恢复时间:{EVENT.RECOVERY.TIME}
持续时间:{EVENT.AGE}
告警平台:http://192.168.97.150:8080
二、钉钉告警(Webhook方式)
参数:
Key //宏变量_钉钉地址
SUBJECT {ALERT.SUBJECT} //宏变量_告警主题
MESSAGE {ALERT.MESSAGE} //宏变量_表示告警详细信息

脚本:
var dingding = {
key: null,
message: null,
msgtype: "markdown",
proxy: null,
sendMessage: function () {
var params = {
msgtype: dingding.msgtype,
markdown: {
title: "告警",
text: dingding.message
},
},
data,
response,
request = new CurlHttpRequest(),
url =
"https://oapi.dingtalk.com/robot/send?access_token=" +
dingding.key;
if (dingding.proxy) {
request.setProxy(dingding.proxy);
}
request.AddHeader("Content-Type: application/json");
data = JSON.stringify(params);
// Remove replace() function if you want to see the exposed key in the log file.
Zabbix.Log(
4,
"[dingding Webhook] URL: " + url.replace(dingding.key, "<BOT KEY>")
);
Zabbix.Log(4, "[dingding Webhook] params: " + data);
response = request.Post(url, data);
Zabbix.Log(4, "[dingding Webhook] HTTP code: " + request.Status());
try {
response = JSON.parse(response);
} catch (error) {
response = null;
}
if (request.Status() !== 200 || response.errcode !== 0) {
if (typeof response.errmsg === "string") {
throw response.errmsg;
} else {
throw "Unknown error. Check debug log for more information.";
}
}
},
};
try {
var params = JSON.parse(value);
if (typeof params.Key === "undefined") {
throw 'Incorrect value is given for parameter "Key": parameter is missing';
}
dingding.key = params.Key;
if (params.HTTPProxy) {
dingding.proxy = params.HTTPProxy;
}
dingding.to = params.To;
dingding.message = params.Subject + "\n" + params.Message;
dingding.sendMessage();
return "OK";
} catch (error) {
Zabbix.Log(4, "[dingding Webhook] notification failed: " + error);
throw "Sending failed: " + error + ".";
三、钉钉告警(python方式)
1.钉钉告警机器人关键词设置

2、完整复制webhook

3、配置钉钉脚本
安装pthon3.8
[root@zabbix-server ~]# yum install python3.8 //安装python
[root@zabbix-server ~]# whereis python3.8 //查看python3.8位置

进入python目录,配置软连接
[root@zabbix-server ~]# cd /usr/bin //进入python目录
[root@zabbix-server bin]# ln -s /usr/bin/python3.8 python //配置软连接

使用pip命令安装脚本中缺失模块
[root@zabbix-server ~]#pip3.8 install requests //安装requests模块
#至此 python前置完成
将脚本放在/usr/lib/zabbix/alertscripts/目录下
[root@zabbix-server ~]# cd /usr/lib/zabbix/alertscripts/
[root@zabbix-server alertscripts]# vim dingding.py
zabbix容器安装python
zabbix镜像基于alpine系统,更改仓库地址,并安装python
1.进入容器目录
docker ps //查看zabbix/zabbix-server-mysql的容器ID
docker exec -it --user root 容器ID /bin/bash
2.更换阿里源
vi /etc/apk/repositories
将里面 dl-cdn.alpinelinux.org 的 改成 mirrors.aliyun.com ; 保存退出即可
3.安装python
apk update
apk add python3 py3-pip
apk add py3-requests
4.编写告警脚本
cd /usr/lib/zabbix/alertscripts/
alertscripts]# vi dingding.py
编辑python代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import json
import sys
import os
import datetime
webhook = "修改为自己机器人的webhook地址即可"
user=sys.argv[1]
subject=sys.argv[2]
text=sys.argv[3]
data={
"msgtype": "text",
"text": {
"content": "%s%s"%(subject,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("/usr/lib/zabbix/alertscripts/log/dingding.log"):
f=open("/usr/lib/zabbix/alertscripts/log/dingding.log","a+")
else:
f=open("/usr/lib/zabbix/alertscripts/log/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()

赋权目录
#为脚本添加执行权限
[root@zabbix alertscripts]# chmod +x dingding.py
#修改脚本的属主和属组:
[root@zabbix alertscripts]# chown zabbix.zabbix dingding.py
创建日志文件:
[root@zabbix alertscripts]# mkdir -p /usr/lib/zabbix/alertscripts/log/
[root@zabbix alertscripts]# touch /usr/lib/zabbix/alertscripts/log/dingding.log
[root@zabbix alertscripts]# chown zabbix.zabbix -R /usr/lib/zabbix/alertscripts/log/
测试钉钉是否能接收到告警信息
#py脚本 手机号 关键词 告警信息
./dingding.py xxxxxxx 告警 test