mirror of https://github.com/openspug/spug
U 完善内置微信告警
parent
4a3b47f2c2
commit
7661ea252c
|
@ -12,7 +12,7 @@ from apps.monitor.executors import dispatch
|
||||||
from apps.monitor.utils import seconds_to_human
|
from apps.monitor.utils import seconds_to_human
|
||||||
from apps.notify.models import Notify
|
from apps.notify.models import Notify
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from libs import AttrDict, human_datetime
|
from libs import spug, AttrDict, human_datetime
|
||||||
import logging
|
import logging
|
||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
|
@ -42,6 +42,7 @@ class Scheduler:
|
||||||
if old_status == 1:
|
if old_status == 1:
|
||||||
self._record_alarm(obj, '2')
|
self._record_alarm(obj, '2')
|
||||||
logger.info(f'{human_datetime()} recover job_id: {obj.id}')
|
logger.info(f'{human_datetime()} recover job_id: {obj.id}')
|
||||||
|
spug.notify_by_wx('2', obj.name, json.loads(obj.notify_grp))
|
||||||
else:
|
else:
|
||||||
if obj.fault_times >= obj.threshold:
|
if obj.fault_times >= obj.threshold:
|
||||||
if time.time() - obj.latest_notify_time >= obj.quiet * 60:
|
if time.time() - obj.latest_notify_time >= obj.quiet * 60:
|
||||||
|
@ -49,6 +50,7 @@ class Scheduler:
|
||||||
obj.save()
|
obj.save()
|
||||||
self._record_alarm(obj, '1')
|
self._record_alarm(obj, '1')
|
||||||
logger.info(f'{human_datetime()} notify job_id: {obj.id}')
|
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):
|
def _handle_event(self, event):
|
||||||
obj = SimpleLazyObject(lambda: Detection.objects.filter(pk=event.job_id).first())
|
obj = SimpleLazyObject(lambda: Detection.objects.filter(pk=event.job_id).first())
|
||||||
|
|
|
@ -6,7 +6,7 @@ from apps.setting.models import Setting
|
||||||
|
|
||||||
|
|
||||||
class AppSetting:
|
class AppSetting:
|
||||||
keys = ('public_key', 'private_key', 'mail_service', 'api_key')
|
keys = ('public_key', 'private_key', 'mail_service', 'api_key', 'spug_key')
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@lru_cache(maxsize=64)
|
@lru_cache(maxsize=64)
|
||||||
|
|
|
@ -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
|
|
@ -27,9 +27,9 @@ class ComForm extends React.Component {
|
||||||
page: 0,
|
page: 0,
|
||||||
modeOptions: [
|
modeOptions: [
|
||||||
{label: '微信', 'value': '1'},
|
{label: '微信', 'value': '1'},
|
||||||
{label: '短信', 'value': '2'},
|
{label: '短信', 'value': '2', disabled: true},
|
||||||
{label: '钉钉', 'value': '3'},
|
{label: '钉钉', 'value': '3', disabled: true},
|
||||||
{label: '邮件', 'value': '4'}]
|
{label: '邮件', 'value': '4', disabled: true}]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,20 +4,41 @@
|
||||||
* Released under the MIT License.
|
* Released under the MIT License.
|
||||||
*/
|
*/
|
||||||
import React from 'react';
|
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 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 (
|
return (
|
||||||
<React.Fragment>
|
<React.Fragment>
|
||||||
<div className={styles.title}>报警服务设置</div>
|
<div className={styles.title}>报警服务设置</div>
|
||||||
<Form style={{maxWidth: 320}}>
|
<Form style={{maxWidth: 320}}>
|
||||||
<Form.Item colon={false} label="短信报警调用凭据">
|
<Form.Item
|
||||||
<Input placeholder="请输入"/>
|
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>
|
</Form.Item>
|
||||||
<Button type="primary">保存设置</Button>
|
<Button type="primary" loading={store.loading} onClick={handleSubmit}>保存设置</Button>
|
||||||
</Form>
|
</Form>
|
||||||
</React.Fragment>
|
</React.Fragment>
|
||||||
)
|
)
|
||||||
}
|
})
|
||||||
|
|
|
@ -24,7 +24,6 @@ export default observer(function () {
|
||||||
.finally(() => store.loading = false)
|
.finally(() => store.loading = false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<React.Fragment>
|
<React.Fragment>
|
||||||
<div className={styles.title}>开放服务设置</div>
|
<div className={styles.title}>开放服务设置</div>
|
||||||
|
|
Loading…
Reference in New Issue