Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
jobily
Nightingale
提交
8b508fc5
N
Nightingale
项目概览
jobily
/
Nightingale
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
N
Nightingale
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
8b508fc5
编写于
8月 06, 2021
作者:
U
UlricQin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
code refactor
上级
2f060cfb
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
11 addition
and
23 deletion
+11
-23
etc/script/notify.py
etc/script/notify.py
+11
-23
未找到文件。
etc/script/notify.py
浏览文件 @
8b508fc5
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#
# n9e-server把告警事件通过stdin的方式传入notify.py,notify.py从事件中解析出接收人信息、拼出通知内容,发送通知
# 脚本的灵活性高,要接入短信、电话、jira、飞书等,都非常容易,只要有接口,notify.py去调用即可
#
import
sys
import
json
import
os
...
...
@@ -13,18 +17,9 @@ from bottle import template
reload
(
sys
)
# reload 才能调用 setdefaultencoding 方法
sys
.
setdefaultencoding
(
'utf-8'
)
# 设置 'utf-8'
# 希望的demo实现效果:
# 1. 从stdin拿到告警信息之后,格式化为一个有缩进的json写入一个临时文件
# 2. 文件路径和名字是.alerts/${timestamp}_${ruleid}
# 3. 调用SMTP服务器发送告警,微信、钉钉、飞书、slack、jira、短信、电话等等留给社区实现
# 脚本二开指南
# 1. 可以根据下面的 TEST_ALERT_JSON 中的结构修改脚本发送逻辑,定制化告警格式见tpl下的模块文件
# 2. 每个告警会以json文件的格式存储在 LOCAL_EVENT_FILE_DIR 下面,文件名为 filename = '%d_%d_%d' % (rule_id, event_id, trigger_time)
# 3. 告警通道需要自行定义Send类中的send_xxx的方法,反射调用:举例 event.notify_channels = [邮件] 则需要在NOTIFY_CHANNEL_DICT配置 邮件=email,在Send类中实现 send_email 方法
# 4. im发群信息,比如钉钉发群信息需要群的webhook机器人 token,这个信息可以在user的contacts map中,各个send_方法处理即可
# 5. 用户创建一个虚拟的用户保存上述im群 的机器人token信息 user的contacts map中
################################
## 邮件告警,修改下面的配置 ##
################################
mail_host
=
"smtp.163.com"
mail_port
=
994
mail_user
=
"ulricqin"
...
...
@@ -35,13 +30,6 @@ mail_from = "ulricqin@163.com"
LOCAL_EVENT_FILE_DIR
=
".alerts"
NOTIFY_CHANNELS_SPLIT_STR
=
" "
# dingding 群机器人token 配置字段
DINGTALK_ROBOT_TOKEN_NAME
=
"dingtalk_robot_token"
DINGTALK_API
=
"https://oapi.dingtalk.com/robot/send"
WECOM_ROBOT_TOKEN_NAME
=
"wecom_robot_token"
WECOM_API
=
"https://qyapi.weixin.qq.com/cgi-bin/webhook/send"
NOTIFY_CHANNEL_DICT
=
{
"email"
:
"email"
,
"sms"
:
"sms"
,
...
...
@@ -251,12 +239,12 @@ class Send(object):
for
u
in
users
:
contacts
=
u
.
get
(
"contacts"
)
wecom_robot_token
=
contacts
.
get
(
WECOM_ROBOT_TOKEN_NAME
,
""
)
wecom_robot_token
=
contacts
.
get
(
"wecom_robot_token"
,
""
)
if
wecom_robot_token
==
""
:
continue
wecom_api_url
=
"
{}?key={}"
.
format
(
WECOM_API
,
wecom_robot_token
)
wecom_api_url
=
"
https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={}"
.
format
(
wecom_robot_token
)
atMobiles
=
[
u
.
get
(
"phone"
)]
headers
=
{
'Content-Type'
:
'application/json;charset=utf-8'
}
payload
=
{
...
...
@@ -284,13 +272,13 @@ class Send(object):
for
u
in
users
:
contacts
=
u
.
get
(
"contacts"
)
dingtalk_robot_token
=
contacts
.
get
(
DINGTALK_ROBOT_TOKEN_NAME
,
""
)
dingtalk_robot_token
=
contacts
.
get
(
"dingtalk_robot_token"
,
""
)
if
dingtalk_robot_token
==
""
:
print
(
"dingtalk_robot_token_not_found"
)
continue
dingtalk_api_url
=
"
{}?access_token={}"
.
format
(
DINGTALK_API
,
dingtalk_robot_token
)
dingtalk_api_url
=
"
https://oapi.dingtalk.com/robot/send?access_token={}"
.
format
(
dingtalk_robot_token
)
atMobiles
=
[
u
.
get
(
"phone"
)]
headers
=
{
'Content-Type'
:
'application/json;charset=utf-8'
}
payload
=
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录