zabbix远程告警


邮件告警

添加报警媒介类型---->  用户添加报警媒介 -----> 动作触发,发送消息
添加报警媒介类型

image-20240802154947127

image-20240802155008083

用户添加报警媒介

image-20240802155049175

image-20240802155206205

image-20240802155214970

动作触发报警

image-20240802155257350

image-20240802155313524

image-20240802155406859

#### 告警模板 ####
故障{TRIGGER.STATUS},服务器:{HOSTNAME1}发生: {TRIGGER.NAME}故障!

告警地址:{HOST.IP}
告警主机:{HOSTNAME1}
告警时间:{EVENT.DATE} {EVENT.TIME}
告警等级:{TRIGGER.SEVERITY}
告警信息: {TRIGGER.NAME}
告警项目:{TRIGGER.KEY1}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1}
事件ID:{EVENT.ID}

恢复{TRIGGER.STATUS}, 服务器:{HOSTNAME1}: {TRIGGER.NAME}已恢复!

告警地址:{HOST.IP}
告警主机:{HOSTNAME1}
告警时间:{EVENT.DATE} {EVENT.TIME}
告警等级:{TRIGGER.SEVERITY}
告警信息: {TRIGGER.NAME}
告警项目:{TRIGGER.KEY1}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1}
事件ID:{EVENT.ID}


<head>
<style type="text/css">
body{
background:url('https://seopic.699pic.com/photo/40007/7490.jpg_wh1200.jpg');
}
</style>
</head>
<body>
<img src="http://pic1.win4000.com/wallpaper/8/573acd203bc77.jpg" alt="">
<style type="text/css">
table .guzhang {color: red;}
</style>
<table border="3" bordercolor="black" cellspacing="0px" cellpadding="4px"
width="500px">
<tr class="guzhang" bgcolor="#0C1B3D" ><th colspan=2>
{TRIGGER.STATUS} 故障!!!故障!!!
<div>&#128514; &#128514; &#128514;</div>
</tr>
<tr >
<td bgcolor="#F9B602" width="20%">告警主机</td>
<td bgcolor="#F9B602">{HOSTNAME1}</td>
</tr>
<tr >
<td bgcolor="#F9B602">告警别名</td>
<td bgcolor="#F9B602">{HOST.NAME} </td>
<tr >
<tr >
<td bgcolor="#F9B602">告警地址</td>
<td bgcolor="#F9B602">{HOST.IP}</td>
</tr>
<tr>
<td bgcolor="#F9B602">告警时间</td>
<td bgcolor="#F9B602">{EVENT.DATE} {EVENT.TIME}</td>
</tr>
<tr>
<td bgcolor="#F9B602">告警等级</td>
<td bgcolor="#F9B602">{TRIGGER.SEVERITY}</td>
</tr>
<tr>
<td bgcolor="#F9B602">告警信息</td>
<td bgcolor="#F9B602">{TRIGGER.NAME}</td>
</tr>
<tr>
<td bgcolor="#F9B602">告警项目</td>
<td bgcolor="#F9B602">{TRIGGER.KEY1}</td>
</tr>
<tr >
<td class='guzhang2' bgcolor="#FF3333">问题详情</td>
<td class='guzhang3' bgcolor="#FF3333">{ITEM.NAME}:&nbsp;
{ITEM.VALUE} &#128520; &#128520; &#128520;</td>
</tr>
<tr>
	<td bgcolor="#F9B602">当前状态</td>
	<td bgcolor="#F9B602">{TRIGGER.STATUS}:&nbsp;{ITEM.VALUE1}</td>
</tr>
<tr>
<td bgcolor="#F9B602">事件ID</td>
<td bgcolor="#F9B602">{EVENT.ID}</td>
</tr>
</table>
</body>


<head>
<style type="text/css">
table .guzhang {
color: red;
}
body{
background:url('https://seopic.699pic.com/photo/40007/7490.jpg_wh1200.jpg');
}
</style>
</head>
<body>
<img src="http://pic1.win4000.com/wallpaper/8/573acd203bc77.jpg" alt="">
<table border="1" bordercolor="black" cellspacing="0px" cellpadding="4px"
width="500px">
<tr bgcolor="#49c208"><th colspan=2>
{TRIGGER.STATUS} 哈哈哈哈哈哈,好了
<div>&#128512; &#128512; &#128512;</div>
</tr>
<tr >
<td bgcolor="lightgreen" width="20%">恢复主机</td>
<td bgcolor="yellow">{HOSTNAME1}</td>
</tr>
<tr>
<td bgcolor="lightgreen">恢复别名</td>
<td bgcolor="yellow">{HOST.NAME} </td>
<tr >
<td bgcolor="lightgreen">恢复地址</td>
<td bgcolor="yellow">{HOST.IP}</td>
</tr>
<tr>
<td bgcolor="lightgreen">恢复时间</td>
<td bgcolor="yellow">{EVENT.DATE} {EVENT.RECOVERY.TIME}</td>
</tr>
<tr>
<td bgcolor="lightgreen">恢复等级</td>
<td bgcolor="yellow">{TRIGGER.SEVERITY}</td>
</tr>
<tr>
<td bgcolor="lightgreen">恢复信息</td>
<td bgcolor="yellow">{TRIGGER.NAME}</td>
</tr>
<tr>
<td bgcolor="lightgreen">恢复项目</td>
<td bgcolor="yellow">{TRIGGER.KEY1}</td>
</tr>
<tr >
<td bgcolor="#49c208">恢复详情</td>
<td bgcolor="#49c208">{ITEM.NAME}:&nbsp;{ITEM.VALUE} &#9889; &#9889;
&#9889;</td>
</tr>
<tr>
<td bgcolor="lightgreen">当前状态</td>
<td bgcolor="yellow">{TRIGGER.STATUS}:&nbsp;{ITEM.VALUE1}</td>
</tr>
<tr>
<td bgcolor="lightgreen">事件ID</td>
<td bgcolor="yellow">{EVENT.ID}</td>
</tr>
</table>
</body>

企业微信告警webhook---shell

注册企业微信 加群   添加机器人
https://developer.work.weixin.qq.com/document/path/99110
### 编写py脚本 ####
# 1.安装python环境
[root@zabbix5-server alertscripts]# yum install -y python36-devel
# 2.安装Python的requests模块
[root@zabbix5-server alertscripts]# pip3 install requests -i
https://pypi.tuna.tsinghua.edu.cn/simple
# 3.编写python脚本
[root@web01 ~]# vim wechat.py
#!/usr/bin/python3
#-*- coding: utf-8 -*-
import requests
import json
import sys
import os
headers = {'Content-Type': 'application/json;charset=utf-8'}
api_url = "webhook地址"

def msg(text):
    json_text= {
     "msgtype": "text",
     "text": {
     "content": text
     },
}
print(requests.post(api_url,json.dumps(json_text),headers=headers).content)

if __name__ == '__main__':
    text = sys.argv[1]
    msg(text)

# 4.将脚本放在/usr/lib/zabbix/alertscripts
[root@zabbix5-server ~]# vim /usr/lib/zabbix/alertscripts/wechat.py
# 5.授权执行权限
[root@zabbix5-server ~]# chmod +x /usr/lib/zabbix/alertscripts/wechat.py
#### 编写shell脚本 ####
# 1.编写shell脚本在指定目录下
[root@zabbix5-server alertscripts]# vim /usr/lib/zabbix/alertscripts/wechat.sh
[root@zabbix5-server alertscripts]# cat wechat.sh
#!/bin/bash
web_hook="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=a8484db4-46be-
4840-905f-0b5c12fd5c7b"
headers="Content-Type: application/json"
json_file="/usr/lib/zabbix/alertscripts/msg.json"
/bin/cat > ${json_file} <<EOF
{
"msgtype": "text",
"text": {
"content": "$1"
}
}
EOF
/usr/bin/curl "${web_hook}" \
-H "${headers}" \
-d "$(cat ${json_file})"
# 2.授权执行权限
[root@zabbix5-server alertscripts]# chmod +x
/usr/lib/zabbix/alertscripts/wechat.sh

image-20240802160611345

image-20240802160625059

image-20240802160632734