配置Zabbix告警-飞书机器人
在飞书PC端创建群组,打开群组,点击左右边一列最后的设置按钮,群机器人,添加机器人,自定义机器人,添加
设置机器人名称以及描述,点击添加,会生成webhook地址,复制保存下来
创建编辑飞书脚本文件
vim /usr/lib/zabbix/alertscripts/feishu.py
填写以下内容并保存
#!/usr/bin/python3
import requests
import json
import sys
import os
import datetime
url = "webhook地址" #你复制的webhook地址粘贴进url内
def send_message(message):
payload_message = {
"msg_type": "text",
"content": {
"text": message
}
}
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=json.dumps(payload_message))
return response
if __name__ == '__main__':
text = sys.argv[1]
send_message(text)
添加feishu.py执行权限
chmod +x /usr/lib/zabbix/alertscripts/feishu.py
Zabbix设置报警
创建报警媒介类型,参数添加:{ALERT.MESSAGE}
用户内添加报警媒介
根据需求配置需要告警的级别
添加完成后点击更新!!!
创建动作
设置通知用户,触发的脚本名称,消息内容
操作
异常通知: {EVENT.NAME}
告警主机:{HOSTNAME1}
告警时间:{EVENT.TIME}
告警等级:{TRIGGER.SEVERITY}
告警信息:{EVENT.NAME}
告警项目:{TRIGGER.KEY1}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1}
事件ID:{EVENT.ID}
恢复操作
恢复通知: {EVENT.NAME}
告警主机:{HOSTNAME1}
告警时间:{EVENT.TIME}
告警等级:{TRIGGER.SEVERITY}
告警信息:{EVENT.NAME}
告警项目:{TRIGGER.KEY1}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1}
事件ID:{EVENT.ID}
点击保存即可
下面代码是利用飞书消息卡片推送的。
代码如下:
#!/usr/bin/python3
# -*- coding:utf-8 -*-
import requests
import json
import sys
from datetime import datetime
# 获取当前时间
current_time = datetime.now()
# 将你复制的webhook地址粘贴进url内
url = "https://open.feishu.cn/open-apis/bot/v2/hook/a0f438c3-Xxxxxxxxxx4"
if __name__ == '__main__':
xianhao_user = sys.argv[1]
xianhao_title = sys.argv[2]
xianhao_message = sys.argv[3]
xianhao_time = current_time.strftime("%Y-%m-%d %H:%M")
# 判断字符串中是否包含 "地址:" 和 "状态OK"
if "地址:" in xianhao_title and "状态" in xianhao_title:
start_index = xianhao_title.index("地址:") + len("地址:")
end_index = xianhao_title.index("状态")
xianhao_host = xianhao_title[start_index:end_index].strip()
else:
xianhao_host = xianhao_title
# 创建卡片的JSON结构
card_content = {
"elements": [
{
"tag": "column_set",
"flex_mode": "none",
"background_style": "default",
"columns": [
{
"tag": "column",
"width": "weighted",
"weight": 1,
"vertical_align": "top",
"elements": [
{
"tag": "div",
"text": {
"content": "**? 问题系统:**\n" + xianhao_host,
"tag": "lark_md"
}
}
]
},
{
"tag": "column",
"width": "weighted",
"weight": 1,
"vertical_align": "top",
"elements": [
{
"tag": "div",
"text": {
"content": "**? 恢复时间:**\n" + xianhao_time,
"tag": "lark_md"
}
}
]
}
]
},
{
"tag": "div",
"text": {
"content": xianhao_message,
"tag": "lark_md"
}
},
{
"tag": "hr"
},
{
"tag": "column_set",
"flex_mode": "none",
"background_style": "default",
"columns": [
{
"tag": "column",
"width": "weighted",
"weight": 1,
"vertical_align": "top",
"elements": [
{
"tag": "div",
"text": {
"content": "**? 通知人员:**\n<at id=\"" + xianhao_user + "\"></at>",
"tag": "lark_md"
}
}
]
}
]
}
],
"header": {
"template": "turquoise",
"title": {
"content": xianhao_title,
"tag": "plain_text"
}
}
}
# 构造请求体
body = json.dumps({
"msg_type": "interactive",
"card": card_content
})
headers = {"Content-Type": "application/json"}
# 发送请求
res = requests.post(url=url, data=body, headers=headers)
print(res.text)
#!/usr/bin/python3
# -*- coding:utf-8 -*-
import requests
import json
import sys
from datetime import datetime
# 获取当前时间
current_time = datetime.now()
# 将你复制的webhook地址粘贴进url内
url = "https://open.feishu.cn/open-apis/bot/v2/hook/a0fxxxxxxxx"
if __name__ == '__main__':
xianhao_user = sys.argv[1]
xianhao_title = sys.argv[2]
xianhao_message = sys.argv[3]
xianhao_time = current_time.strftime("%Y-%m-%d %H:%M")
# 判断字符串中是否包含 "地址:" 和 "状态OK"
if "地址:" in xianhao_title and "状态" in xianhao_title:
start_index = xianhao_title.index("地址:") + len("地址:")
end_index = xianhao_title.index("状态")
xianhao_host = xianhao_title[start_index:end_index].strip()
else:
xianhao_host = xianhao_title
# 创建卡片的JSON结构
card_content = {
"elements": [
{
"tag": "column_set",
"flex_mode": "none",
"background_style": "default",
"columns": [
{
"tag": "column",
"width": "weighted",
"weight": 1,
"vertical_align": "top",
"elements": [
{
"tag": "div",
"text": {
"content": "**? 异常系统:**\n" + xianhao_host,
"tag": "lark_md"
}
}
]
},
{
"tag": "column",
"width": "weighted",
"weight": 1,
"vertical_align": "top",
"elements": [
{
"tag": "div",
"text": {
"content": "**? 告警时间:**\n" + xianhao_time,
"tag": "lark_md"
}
}
]
}
]
},
{
"tag": "div",
"text": {
"content": xianhao_message,
"tag": "lark_md"
}
},
{
"tag": "hr"
},
{
"tag": "column_set",
"flex_mode": "none",
"background_style": "default",
"columns": [
{
"tag": "column",
"width": "weighted",
"weight": 1,
"vertical_align": "top",
"elements": [
{
"tag": "div",
"text": {
"content": "**? 通知人员:**\n<at id=\"" + xianhao_user + "\"></at>",
"tag": "lark_md"
}
}
]
}
]
}
],
"header": {
"template": "red",
"title": {
"content": xianhao_title,
"tag": "plain_text"
}
}
}
# 构造请求体
body = json.dumps({
"msg_type": "interactive",
"card": card_content
})
headers = {"Content-Type": "application/json"}
# 发送请求
res = requests.post(url=url, data=body, headers=headers)
print(res.text)
2 comments
Hgguhgff
21351466146