From 5a7f1ac7a455f5c104bb9d1f42ffe23512e8fa2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=B7=E4=BA=8C=E7=8C=9B?= Date: Thu, 5 Dec 2019 23:45:54 +0800 Subject: [PATCH] A api add record alarm --- spug_api/apps/alarm/models.py | 5 +++-- spug_api/apps/monitor/scheduler.py | 16 +++++++++++++++- spug_api/apps/monitor/utils.py | 9 +++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 spug_api/apps/monitor/utils.py diff --git a/spug_api/apps/alarm/models.py b/spug_api/apps/alarm/models.py index 4cc70dc..8a02cf8 100644 --- a/spug_api/apps/alarm/models.py +++ b/spug_api/apps/alarm/models.py @@ -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 diff --git a/spug_api/apps/monitor/scheduler.py b/spug_api/apps/monitor/scheduler.py index fe68788..774ec52 100644 --- a/spug_api/apps/monitor/scheduler.py +++ b/spug_api/apps/monitor/scheduler.py @@ -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 diff --git a/spug_api/apps/monitor/utils.py b/spug_api/apps/monitor/utils.py new file mode 100644 index 0000000..f8527ec --- /dev/null +++ b/spug_api/apps/monitor/utils.py @@ -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)}秒'