U 通知发送异常情况将会反馈至系统通知

pull/154/head
vapao 2020-07-16 20:16:36 +08:00
parent 92a67b7bcf
commit a046679ea7
4 changed files with 42 additions and 10 deletions

View File

@ -5,6 +5,7 @@ from django_redis import get_redis_connection
from django.conf import settings
from libs.utils import AttrDict, human_time, human_datetime
from apps.host.models import Host
from apps.notify.models import Notify
from concurrent import futures
import requests
import socket
@ -329,7 +330,13 @@ class Helper:
data = cls._make_wx_notify(action, req, version, host_str)
else:
raise NotImplementedError
requests.post(rst_notify['value'], json=data)
res = requests.post(rst_notify['value'], json=data)
if res.status_code != 200:
Notify.make_notify('flag', '1', '发布通知发送失败', f'返回状态码:{res.status_code}, 请求URL{res.url}')
if rst_notify['mode'] in ['1', '3']:
res = res.json()
if res.get('errcode') != 0:
Notify.make_notify('flag', '1', '发布通知发送失败', f'返回数据:{res}')
def parse_filter_rule(self, data: str):
data, files = data.strip(), []

View File

@ -15,6 +15,7 @@ class Notify(models.Model, ModelMixin):
SOURCES = (
('monitor', '监控中心'),
('schedule', '任务计划'),
('flag', '应用发布'),
)
title = models.CharField(max_length=255)
source = models.CharField(max_length=10, choices=SOURCES)

View File

@ -2,6 +2,7 @@
# Copyright: (c) <spug.dev@gmail.com>
# Released under the AGPL-3.0 License.
from apps.schedule.models import Task, History
from apps.notify.models import Notify
from libs.utils import human_datetime
from threading import Thread
import requests
@ -55,10 +56,10 @@ def _do_notify(task, mode, url, msg):
elif mode == '3':
texts = [
'## <font color="warning">任务执行失败通知</font>',
f'**任务名称:** {task.name} ',
f'**任务类型:** {task.type} ',
f'**描述信息:** {msg or "请在任务计划执行历史中查看详情"} ',
f'**发生时间:** {human_datetime()} ',
f'任务名称: {task.name}',
f'任务类型: {task.type}',
f'描述信息: {msg or "请在任务计划执行历史中查看详情"}',
f'发生时间: {human_datetime()}',
'> 来自 Spug运维平台'
]
data = {
@ -67,4 +68,10 @@ def _do_notify(task, mode, url, msg):
'content': '\n'.join(texts)
}
}
requests.post(url, json=data)
res = requests.post(url, json=data)
if res.status_code != 200:
Notify.make_notify('schedule', '1', '任务执行通知发送失败', f'返回状态码:{res.status_code}, 请求URL{url}')
if mode in ['1', '3']:
res = res.json()
if res.get('errcode') != 0:
Notify.make_notify('schedule', '1', '任务执行通知发送失败', f'返回数据:{res}')

View File

@ -18,6 +18,19 @@ def _parse_args(grp):
return spug_key, sum([json.loads(x.contacts) for x in Group.objects.filter(id__in=grp)], [])
def _handle_response(res, mode):
if res.status_code != 200:
Notify.make_notify(notify_source, '1', '告警通知发送失败', f'返回状态码:{res.status_code}, 请求URL{res.url}')
if mode in ['dd', 'wx']:
res = res.json()
if res.get('errcode') != 0:
Notify.make_notify(notify_source, '1', '告警通知发送失败', f'返回数据:{res}')
if mode == 'spug':
res = res.json()
if res.get('error'):
Notify.make_notify(notify_source, '1', '告警通知发送失败', f'错误信息:{res}')
def notify_by_wx(event, obj):
spug_key, u_ids = _parse_args(obj.grp)
if not spug_key:
@ -33,7 +46,8 @@ def notify_by_wx(event, obj):
'remark': f'故障持续{obj.duration}' if event == '2' else None,
'users': list(users)
}
requests.post(f'{spug_server}/apis/notify/wx/', json=data)
res = requests.post(f'{spug_server}/apis/notify/wx/', json=data)
_handle_response(res, 'spug')
else:
Notify.make_notify(notify_source, '1', '发送报警信息失败', '未找到可用的通知对象请确保设置了相关报警联系人的微信Token。')
@ -59,7 +73,8 @@ def notify_by_email(event, obj):
'body': '\r\n'.join(body),
'users': list(users)
}
requests.post(f'{spug_server}/apis/notify/mail/', json=data)
res = requests.post(f'{spug_server}/apis/notify/mail/', json=data)
_handle_response(res, 'spug')
else:
Notify.make_notify(notify_source, '1', '发送报警信息失败', '未配置报警服务调用凭据,请在系统管理/系统设置/报警服务设置中配置。')
else:
@ -86,7 +101,8 @@ def notify_by_dd(event, obj):
}
}
for url in users:
requests.post(url, json=data)
res = requests.post(url, json=data)
_handle_response(res, 'dd')
else:
Notify.make_notify(notify_source, '1', '发送报警信息失败', '未找到可用的通知对象,请确保设置了相关报警联系人的钉钉。')
@ -111,6 +127,7 @@ def notify_by_qy_wx(event, obj):
}
}
for url in users:
requests.post(url, json=data)
res = requests.post(url, json=data)
_handle_response(res, 'wx')
else:
Notify.make_notify(notify_source, '1', '发送报警信息失败', '未找到可用的通知对象,请确保设置了相关报警联系人的企业微信。')