A 添加钉钉告警通知支持

pull/22/head
vapao 2020-01-16 17:24:52 +08:00
parent d4aec21556
commit 3f5e8c0dd9
3 changed files with 30 additions and 7 deletions

View File

@ -42,6 +42,8 @@ class Scheduler:
for mode in json.loads(obj.notify_mode):
if mode == '1':
spug.notify_by_wx(event, obj.name, grp)
elif mode == '3':
spug.notify_by_dd(event, obj.name, grp)
elif mode == '4':
spug.notify_by_email(event, obj.name, grp)

View File

@ -5,6 +5,7 @@ from apps.alarm.models import Group, Contact
from apps.setting.utils import AppSetting
from apps.notify.models import Notify
from libs.mail import Mail
from libs.utils import human_datetime
import requests
import json
@ -12,12 +13,12 @@ spug_server = 'http://spug-wx.qbangmang.com'
notify_source = 'info-circle'
def _parse_args(n_grp):
def _parse_args(grp):
spug_key = AppSetting.get_default('spug_key')
if not spug_key:
Notify.make_notify(notify_source, '1', '发送报警信息失败', '未配置报警服务调用凭据,请在系统管理/系统设置/报警服务设置中配置。')
return None, None
return spug_key, sum([json.loads(x.contacts) for x in Group.objects.filter(id__in=n_grp)], [])
return spug_key, sum([json.loads(x.contacts) for x in Group.objects.filter(id__in=grp)], [])
def notify_by_wx(event, subject, n_grp):
@ -35,8 +36,8 @@ def notify_by_wx(event, subject, n_grp):
requests.post(f'{spug_server}/apis/notify/wx/', json=data)
def notify_by_email(event, subject, n_grp):
spug_key, u_ids = _parse_args(n_grp)
def notify_by_email(event, subject, grp):
spug_key, u_ids = _parse_args(grp)
if u_ids is None:
return
users = set(x.email for x in Contact.objects.filter(id__in=u_ids, email__isnull=False))
@ -57,5 +58,25 @@ def notify_by_email(event, subject, n_grp):
requests.post(f'{spug_server}/apis/notify/mail/', json=data)
def notify_by_sms():
pass
def notify_by_dd(event, subject, grp):
spug_key, u_ids = _parse_args(grp)
if u_ids is None:
return
users = set(x.ding for x in Contact.objects.filter(id__in=u_ids, email__isnull=False))
if users:
texts = [
'## %s ## ' % '监控告警通知' if event == '1' else '告警恢复通知',
f'**告警名称:** <font color="#{"f90202" if event == "1" else "8ece60"}">{subject}</font> ',
f'**告警时间:** {human_datetime()} ',
'**告警描述:** %s ' % '请在运维平台监控中心查看详情' if event == '1' else '告警已恢复',
'> ###### 来自 Spug运维平台'
]
data = {
'msgtype': 'markdown',
'markdown': {
'title': '监控告警通知',
'text': '\n\n'.join(texts)
}
}
for url in users:
requests.post(url, json=data)

View File

@ -28,7 +28,7 @@ class ComForm extends React.Component {
modeOptions: [
{label: '微信', 'value': '1'},
{label: '短信', 'value': '2', disabled: true},
{label: '钉钉', 'value': '3', disabled: true},
{label: '钉钉', 'value': '3'},
{label: '邮件', 'value': '4'}]
}
}