A 添加邮件报警支持

pull/22/head
vapao 2020-01-16 14:37:03 +08:00
parent ca754e9933
commit 2ed2ce5272
4 changed files with 66 additions and 10 deletions

View File

@ -37,12 +37,20 @@ class Scheduler:
notify_grp=obj.notify_grp, notify_grp=obj.notify_grp,
notify_mode=obj.notify_mode) notify_mode=obj.notify_mode)
def _do_notify(self, event, obj):
grp = json.loads(obj.notify_grp)
for mode in json.loads(obj.notify_mode):
if mode == '1':
spug.notify_by_wx(event, obj.name, grp)
elif mode == '4':
spug.notify_by_email(event, obj.name, grp)
def _handle_notify(self, obj, old_status): def _handle_notify(self, obj, old_status):
if obj.latest_status == 0: if obj.latest_status == 0:
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)) self._do_notify('2', obj)
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:
@ -50,7 +58,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)) self._do_notify('1', obj)
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())

29
spug_api/libs/mail.py Normal file
View File

@ -0,0 +1,29 @@
from email.message import EmailMessage
from email.utils import formataddr
import smtplib
class Mail:
def __init__(self, server, port, username, password, nickname=None):
self.host = server
self.port = port
self.user = username
self.password = password
self.nickname = nickname
def _get_server(self):
server = smtplib.SMTP_SSL(self.host, self.port)
server.login(self.user, self.password)
return server
def send_text_mail(self, to_addrs, subject, body):
if isinstance(to_addrs, (list, tuple)):
to_addrs = ', '.join(to_addrs)
server = self._get_server()
msg = EmailMessage()
msg.set_content(body)
msg['Subject'] = subject
msg['From'] = formataddr((self.nickname, self.user)) if self.nickname else self.user
msg['To'] = to_addrs
server.send_message(msg)
server.quit()

View File

@ -4,6 +4,7 @@
from apps.alarm.models import Group, Contact from apps.alarm.models import Group, Contact
from apps.setting.utils import AppSetting from apps.setting.utils import AppSetting
from apps.notify.models import Notify from apps.notify.models import Notify
from libs.mail import Mail
import requests import requests
import json import json
@ -16,27 +17,45 @@ def _parse_args(n_grp):
if not spug_key: if not spug_key:
Notify.make_notify(notify_source, '1', '发送报警信息失败', '未配置报警服务调用凭据,请在系统管理/系统设置/报警服务设置中配置。') Notify.make_notify(notify_source, '1', '发送报警信息失败', '未配置报警服务调用凭据,请在系统管理/系统设置/报警服务设置中配置。')
return None, None return None, None
return spug_key, [json.loads(x.contacts) for x in Group.objects.filter(id__in=n_grp)] return spug_key, sum([json.loads(x.contacts) for x in Group.objects.filter(id__in=n_grp)], [])
def notify_by_wx(event, subject, n_grp): def notify_by_wx(event, subject, n_grp):
spug_key, u_ids = _parse_args(n_grp) spug_key, u_ids = _parse_args(n_grp)
if u_ids is None: if u_ids is None:
return return
users = [x.wx_token for x in Contact.objects.filter(id__in=sum(u_ids, []), wx_token__isnull=False)] users = set(x.wx_token for x in Contact.objects.filter(id__in=u_ids, wx_token__isnull=False))
if users: if users:
data = { data = {
'token': spug_key, 'token': spug_key,
'event': event, 'event': event,
'subject': subject, 'subject': subject,
'users': users 'users': list(users)
} }
requests.post(f'{spug_server}/apis/notify/wx/', json=data) requests.post(f'{spug_server}/apis/notify/wx/', json=data)
def notify_by_email(event, subject, n_grp):
spug_key, u_ids = _parse_args(n_grp)
if u_ids is None:
return
users = set(x.email for x in Contact.objects.filter(id__in=u_ids, email__isnull=False))
if users:
mail_service = json.loads(AppSetting.get_default('mail_service', '{}'))
if mail_service.get('server'):
event_map = {'1': '告警', '2': '恢复'}
subject = f'{event_map[event]}-{subject}'
mail = Mail(**mail_service)
mail.send_text_mail(users, subject, f'{subject}\r\n\r\n自动发送,请勿回复。')
else:
data = {
'token': spug_key,
'event': event,
'subject': subject,
'users': list(users)
}
requests.post(f'{spug_server}/apis/notify/mail/', json=data)
def notify_by_sms(): def notify_by_sms():
pass pass
def notify_by_email():
pass

View File

@ -29,7 +29,7 @@ class ComForm extends React.Component {
{label: '微信', 'value': '1'}, {label: '微信', 'value': '1'},
{label: '短信', 'value': '2', disabled: true}, {label: '短信', 'value': '2', disabled: true},
{label: '钉钉', 'value': '3', disabled: true}, {label: '钉钉', 'value': '3', disabled: true},
{label: '邮件', 'value': '4', disabled: true}] {label: '邮件', 'value': '4'}]
} }
} }