From a046679ea716f6c678c54052b67b0b97c0d673bc Mon Sep 17 00:00:00 2001 From: vapao Date: Thu, 16 Jul 2020 20:16:36 +0800 Subject: [PATCH] =?UTF-8?q?U=20=E9=80=9A=E7=9F=A5=E5=8F=91=E9=80=81?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E6=83=85=E5=86=B5=E5=B0=86=E4=BC=9A=E5=8F=8D?= =?UTF-8?q?=E9=A6=88=E8=87=B3=E7=B3=BB=E7=BB=9F=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spug_api/apps/deploy/utils.py | 9 ++++++++- spug_api/apps/notify/models.py | 1 + spug_api/apps/schedule/utils.py | 17 ++++++++++++----- spug_api/libs/spug.py | 25 +++++++++++++++++++++---- 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/spug_api/apps/deploy/utils.py b/spug_api/apps/deploy/utils.py index 7538652..57926da 100644 --- a/spug_api/apps/deploy/utils.py +++ b/spug_api/apps/deploy/utils.py @@ -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(), [] diff --git a/spug_api/apps/notify/models.py b/spug_api/apps/notify/models.py index b65bb0e..c7f122c 100644 --- a/spug_api/apps/notify/models.py +++ b/spug_api/apps/notify/models.py @@ -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) diff --git a/spug_api/apps/schedule/utils.py b/spug_api/apps/schedule/utils.py index 1d05c22..cfd43ab 100644 --- a/spug_api/apps/schedule/utils.py +++ b/spug_api/apps/schedule/utils.py @@ -2,6 +2,7 @@ # Copyright: (c) # 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 = [ '## 任务执行失败通知', - 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}') diff --git a/spug_api/libs/spug.py b/spug_api/libs/spug.py index f14b13d..3e723bb 100644 --- a/spug_api/libs/spug.py +++ b/spug_api/libs/spug.py @@ -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', '发送报警信息失败', '未找到可用的通知对象,请确保设置了相关报警联系人的企业微信。')