diff --git a/spug_api/apps/monitor/scheduler.py b/spug_api/apps/monitor/scheduler.py index 59dce32..8115fbb 100644 --- a/spug_api/apps/monitor/scheduler.py +++ b/spug_api/apps/monitor/scheduler.py @@ -12,7 +12,7 @@ from apps.monitor.executors import dispatch from apps.monitor.utils import seconds_to_human from apps.notify.models import Notify from django.conf import settings -from libs import AttrDict, human_datetime +from libs import spug, AttrDict, human_datetime import logging import json import time @@ -42,6 +42,7 @@ class Scheduler: if old_status == 1: self._record_alarm(obj, '2') logger.info(f'{human_datetime()} recover job_id: {obj.id}') + spug.notify_by_wx('2', obj.name, json.loads(obj.notify_grp)) else: if obj.fault_times >= obj.threshold: if time.time() - obj.latest_notify_time >= obj.quiet * 60: @@ -49,6 +50,7 @@ class Scheduler: obj.save() self._record_alarm(obj, '1') logger.info(f'{human_datetime()} notify job_id: {obj.id}') + spug.notify_by_wx('1', obj.name, json.loads(obj.notify_grp)) def _handle_event(self, event): obj = SimpleLazyObject(lambda: Detection.objects.filter(pk=event.job_id).first()) diff --git a/spug_api/apps/setting/utils.py b/spug_api/apps/setting/utils.py index 142d90a..8bcd5bf 100644 --- a/spug_api/apps/setting/utils.py +++ b/spug_api/apps/setting/utils.py @@ -6,7 +6,7 @@ from apps.setting.models import Setting class AppSetting: - keys = ('public_key', 'private_key', 'mail_service', 'api_key') + keys = ('public_key', 'private_key', 'mail_service', 'api_key', 'spug_key') @classmethod @lru_cache(maxsize=64) diff --git a/spug_api/libs/spug.py b/spug_api/libs/spug.py new file mode 100644 index 0000000..2ac189c --- /dev/null +++ b/spug_api/libs/spug.py @@ -0,0 +1,42 @@ +# Copyright: (c) OpenSpug Organization. https://github.com/openspug/spug +# Copyright: (c) +# Released under the MIT License. +from apps.alarm.models import Group, Contact +from apps.setting.utils import AppSetting +from apps.notify.models import Notify +import requests +import json + +spug_server = 'http://spug-wx.qbangmang.com' +notify_source = 'info-circle' + + +def _parse_args(n_grp): + spug_key = AppSetting.get_default('spug_key') + if not spug_key: + Notify.make_notify(notify_source, '1', '发送报警信息失败', '未配置报警服务调用凭据,请在系统管理/系统设置/报警服务设置中配置。') + return None, None + return spug_key, [json.loads(x.contacts) for x in Group.objects.filter(id__in=n_grp)] + + +def notify_by_wx(event, subject, n_grp): + spug_key, u_ids = _parse_args(n_grp) + if u_ids is None: + return + users = [x.wx_token for x in Contact.objects.filter(id__in=sum(u_ids, []), wx_token__isnull=False)] + if users: + data = { + 'token': spug_key, + 'event': event, + 'subject': subject, + 'users': users + } + requests.post(f'{spug_server}/apis/notify/wx/', json=data) + + +def notify_by_sms(): + pass + + +def notify_by_email(): + pass diff --git a/spug_web/src/pages/monitor/Form.js b/spug_web/src/pages/monitor/Form.js index c37ee75..771cc25 100644 --- a/spug_web/src/pages/monitor/Form.js +++ b/spug_web/src/pages/monitor/Form.js @@ -27,9 +27,9 @@ class ComForm extends React.Component { page: 0, modeOptions: [ {label: '微信', 'value': '1'}, - {label: '短信', 'value': '2'}, - {label: '钉钉', 'value': '3'}, - {label: '邮件', 'value': '4'}] + {label: '短信', 'value': '2', disabled: true}, + {label: '钉钉', 'value': '3', disabled: true}, + {label: '邮件', 'value': '4', disabled: true}] } } diff --git a/spug_web/src/pages/system/setting/AlarmSetting.js b/spug_web/src/pages/system/setting/AlarmSetting.js index 1bef588..128a85e 100644 --- a/spug_web/src/pages/system/setting/AlarmSetting.js +++ b/spug_web/src/pages/system/setting/AlarmSetting.js @@ -4,20 +4,41 @@ * Released under the MIT License. */ import React from 'react'; -import { Button, Form, Input } from 'antd'; +import { observer } from 'mobx-react'; +import { Button, Form, Input, message } from 'antd'; import styles from './index.module.css'; +import { http } from 'libs'; +import store from './store'; +import lds from 'lodash'; -export default function AlarmSetting(props) { +export default observer(function () { + function handleSubmit() { + store.loading = true; + const value = lds.get(store.settings, 'spug_key.value'); + http.post('/api/setting/', {data: [{key: 'spug_key', value}]}) + .then(() => { + message.success('保存成功'); + store.fetchSettings() + }) + .finally(() => store.loading = false) + } + return (
报警服务设置
- - + 该凭据用于调用spug内置的报警服务,请关注公众号Spug运维在我的页面查看调用凭据。}> + lds.set(store.settings, 'spug_key.value', e.target.value)} + placeholder="请输入"/> - +
) -} +}) diff --git a/spug_web/src/pages/system/setting/OpenService.js b/spug_web/src/pages/system/setting/OpenService.js index b09b9b9..46b57b8 100644 --- a/spug_web/src/pages/system/setting/OpenService.js +++ b/spug_web/src/pages/system/setting/OpenService.js @@ -24,7 +24,6 @@ export default observer(function () { .finally(() => store.loading = false) } - return (
开放服务设置