U 完善内置微信告警

pull/22/head
vapao 2020-01-16 00:07:49 +08:00
parent 4a3b47f2c2
commit 7661ea252c
6 changed files with 76 additions and 12 deletions

View File

@ -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())

View File

@ -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)

42
spug_api/libs/spug.py Normal file
View File

@ -0,0 +1,42 @@
# Copyright: (c) OpenSpug Organization. https://github.com/openspug/spug
# Copyright: (c) <spug.dev@gmail.com>
# 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

View File

@ -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}]
}
}

View File

@ -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 (
<React.Fragment>
<div className={styles.title}>报警服务设置</div>
<Form style={{maxWidth: 320}}>
<Form.Item colon={false} label="短信报警调用凭据">
<Input placeholder="请输入"/>
<Form.Item
colon={false}
label="调用凭据"
help={<span>该凭据用于调用spug内置的报警服务请关注公众号<span style={{color: '#008dff'}}>Spug运维</span></span>}>
<Input
value={lds.get(store.settings, 'spug_key.value')}
onChange={e => lds.set(store.settings, 'spug_key.value', e.target.value)}
placeholder="请输入"/>
</Form.Item>
<Button type="primary">保存设置</Button>
<Button type="primary" loading={store.loading} onClick={handleSubmit}>保存设置</Button>
</Form>
</React.Fragment>
)
}
})

View File

@ -24,7 +24,6 @@ export default observer(function () {
.finally(() => store.loading = false)
}
return (
<React.Fragment>
<div className={styles.title}>开放服务设置</div>