mirror of https://github.com/openspug/spug
A api add record alarm
parent
fb7b7efd0f
commit
5a7f1ac7a4
|
@ -17,7 +17,7 @@ class Alarm(models.Model, ModelMixin):
|
|||
)
|
||||
name = models.CharField(max_length=50)
|
||||
type = models.CharField(max_length=50)
|
||||
notify_mode = models.CharField(max_length=2, choices=MODES)
|
||||
notify_mode = models.CharField(max_length=255)
|
||||
notify_grp = models.CharField(max_length=255)
|
||||
status = models.CharField(max_length=2, choices=STATUS)
|
||||
duration = models.CharField(max_length=50)
|
||||
|
@ -25,7 +25,8 @@ class Alarm(models.Model, ModelMixin):
|
|||
|
||||
def to_dict(self, *args, **kwargs):
|
||||
tmp = super().to_dict(*args, **kwargs)
|
||||
tmp['notify_mode_alias'] = self.get_notify_mode_display()
|
||||
tmp['notify_mode'] = json.loads(self.notify_mode)
|
||||
tmp['notify_grp'] = json.loads(self.notify_grp)
|
||||
tmp['status_alias'] = self.get_status_display()
|
||||
return tmp
|
||||
|
||||
|
|
|
@ -3,7 +3,9 @@ from apscheduler.triggers.interval import IntervalTrigger
|
|||
from apscheduler import events
|
||||
from django_redis import get_redis_connection
|
||||
from apps.monitor.models import Detection
|
||||
from apps.alarm.models import Alarm
|
||||
from apps.monitor.executors import dispatch
|
||||
from apps.monitor.utils import seconds_to_human
|
||||
from django.conf import settings
|
||||
from libs import AttrDict, human_time
|
||||
import logging
|
||||
|
@ -20,15 +22,27 @@ class Scheduler:
|
|||
self.scheduler = BackgroundScheduler(timezone=self.timezone)
|
||||
self.scheduler.add_listener(self._handle_event, )
|
||||
|
||||
def _record_alarm(self, obj, status):
|
||||
duration = seconds_to_human(time.time() - obj.latest_fault_time)
|
||||
Alarm.objects.create(
|
||||
name=obj.name,
|
||||
type=obj.type,
|
||||
status=status,
|
||||
duration=duration,
|
||||
notify_grp=obj.notify_grp,
|
||||
notify_mode=obj.notify_mode)
|
||||
|
||||
def _handle_notify(self, obj, old_status):
|
||||
if obj.latest_status == 0:
|
||||
if old_status == 1:
|
||||
self._record_alarm(obj, '2')
|
||||
logger.info(f'{human_time()} recover job_id: {obj.id}')
|
||||
else:
|
||||
if obj.fault_times >= obj.threshold:
|
||||
if time.time() - obj.latest_notify_time >= obj.quiet * 60:
|
||||
obj.latest_notify_time = int(time.time())
|
||||
obj.save()
|
||||
self._record_alarm(obj, '1')
|
||||
logger.info(f'{human_time()} notify job_id: {obj.id}')
|
||||
|
||||
def _handle_event(self, event):
|
||||
|
@ -46,7 +60,7 @@ class Scheduler:
|
|||
old_status = obj.latest_status
|
||||
obj.latest_status = 0 if event.retval else 1
|
||||
obj.latest_run_time = human_time(event.scheduled_run_time)
|
||||
if old_status == 0 and event.retval is False:
|
||||
if old_status in [0, None] and event.retval is False:
|
||||
obj.latest_fault_time = int(time.time())
|
||||
if obj.latest_status == 0:
|
||||
obj.latest_notify_time = 0
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
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
|
||||
return f'{text}{int(seconds)}秒'
|
Loading…
Reference in New Issue