mirror of https://github.com/openspug/spug
60 lines
1.9 KiB
Python
60 lines
1.9 KiB
Python
# Copyright: (c) OpenSpug Organization. https://github.com/openspug/spug
|
|
# Copyright: (c) <spug.dev@gmail.com>
|
|
# Released under the AGPL-3.0 License.
|
|
from django.db import close_old_connections
|
|
from apps.alarm.models import Alarm
|
|
from apps.monitor.models import Detection
|
|
from apps.schedule.models import Task
|
|
from apps.schedule.scheduler import Scheduler
|
|
from libs.spug import Notification
|
|
import json
|
|
|
|
|
|
def seconds_to_human(seconds):
|
|
text = ''
|
|
if seconds > 3600:
|
|
text = f'{int(seconds / 3600)}小时'
|
|
seconds = seconds % 3600
|
|
if seconds > 60:
|
|
text += f'{int(seconds / 60)}分钟'
|
|
seconds = seconds % 60
|
|
if seconds:
|
|
text += f'{seconds}秒'
|
|
return text
|
|
|
|
|
|
def _record_alarm(det, target, duration, status):
|
|
Alarm.objects.create(
|
|
name=det.name,
|
|
type=det.get_type_display(),
|
|
target=target,
|
|
status=status,
|
|
duration=duration,
|
|
notify_grp=det.notify_grp,
|
|
notify_mode=det.notify_mode)
|
|
|
|
|
|
def handle_notify(task_id, target, is_ok, out, fault_times):
|
|
close_old_connections()
|
|
det = Detection.objects.get(pk=task_id)
|
|
duration = seconds_to_human(det.rate * fault_times * 60)
|
|
event = '2' if is_ok else '1'
|
|
_record_alarm(det, target, duration, event)
|
|
grp = json.loads(det.notify_grp)
|
|
notify = Notification(grp, event, target, det.name, out, duration)
|
|
notify.dispatch_monitor(json.loads(det.notify_mode))
|
|
|
|
|
|
def handle_trigger_event(task_id, target):
|
|
query = dict(is_active=True, trigger='monitor', trigger_args__regex=fr'[^0-9]{task_id}[^0-9]')
|
|
for item in Task.objects.filter(**query):
|
|
targets = []
|
|
for t in json.loads(item.targets):
|
|
if t == 'monitor':
|
|
if target:
|
|
targets.append(target)
|
|
else:
|
|
targets.append(t)
|
|
if targets:
|
|
Scheduler.dispatch(item.id, item.interpreter, item.command, targets)
|