From 89c26d0606e43383dc138918687f037950faff49 Mon Sep 17 00:00:00 2001 From: cindy oneill Date: Mon, 9 Jun 2014 10:53:59 -0600 Subject: [PATCH] Alarms History added --- monitoring/alarms/tables.py | 26 +++++++++++++++----------- monitoring/alarms/urls.py | 2 +- monitoring/alarms/views.py | 26 +++++++++++++++++++++++--- monitoring/api/monitor.py | 4 ++++ 4 files changed, 43 insertions(+), 15 deletions(-) diff --git a/monitoring/alarms/tables.py b/monitoring/alarms/tables.py index 929a15f6..c37e5453 100644 --- a/monitoring/alarms/tables.py +++ b/monitoring/alarms/tables.py @@ -81,9 +81,16 @@ def show_host(data): class ShowAlarmHistory(tables.LinkAction): name = 'history' verbose_name = _('Show History') - url = constants.URL_PREFIX + 'history' + #url = constants.URL_PREFIX + 'history' classes = ('btn-edit',) + def get_link_url(self, datum): + return reverse(constants.URL_PREFIX + 'history', + args=(datum['name'], datum['id'], )) + + def allowed(self, request, datum=None): + return True + class ShowAlarmMeters(tables.LinkAction): name = 'meters' @@ -187,18 +194,15 @@ class AlarmsTable(tables.DataTable): class AlarmHistoryTable(tables.DataTable): - status = tables.Column('Status', verbose_name=_('Status'), - status_choices={(show_status('OK'), True)}, - filters=[show_status, template.defaultfilters.safe]) - target = tables.Column('Host', verbose_name=_('Host')) - name = tables.Column('Service', verbose_name=_('Service')) - lastCheck = tables.Column('Last_Check', verbose_name=_('Last Check')) - time = tables.Column('Duration', verbose_name=_('Duration')) - detail = tables.Column('Status_Information', - verbose_name=_('Status_Information')) + name = tables.Column('name', verbose_name=_('Name')) + old_state = tables.Column('old_state', verbose_name=_('Old State')) + new_state = tables.Column('new_state', verbose_name=_('New State')) + timestamp = tables.Column('timestamp', verbose_name=_('Timestamp')) + reason = tables.Column('reason', verbose_name=_('Reason')) + reason_data = tables.Column('reason_data', verbose_name=_('Reason Data')) def get_object_id(self, obj): - return obj['Last_Check'] + obj['Service'] + return obj['alarm_id'] + obj['timestamp'] class Meta: name = "users" diff --git a/monitoring/alarms/urls.py b/monitoring/alarms/urls.py index 84cf8294..628da501 100644 --- a/monitoring/alarms/urls.py +++ b/monitoring/alarms/urls.py @@ -25,7 +25,7 @@ urlpatterns = patterns( url(r'^alarm/(?P[^/]+)/$', views.AlarmServiceView.as_view(), name='alarm'), - url(r'^history/(?P[^/]+)$', + url(r'^history/(?P[^/]+)/(?P[^/]+)$', views.AlarmHistoryView.as_view(), name='history'), url(r'^meters/(?P[^/]+)$', diff --git a/monitoring/alarms/views.py b/monitoring/alarms/views.py index 520729ff..07c0e621 100644 --- a/monitoring/alarms/views.py +++ b/monitoring/alarms/views.py @@ -232,6 +232,21 @@ def transform_alarm_data(obj): 'notifications': getattr(obj, 'alarm_actions', None), } +def transform_alarm_history(results, name): + newlist = [] + for item in results: + temp = {} + temp['alarm_id'] = item['alarm_id'] + temp['name'] = name + temp['old_state'] = item['old_state'] + temp['new_state'] = item['new_state'] + temp['timestamp'] = item['timestamp'] + temp['reason'] = item['reason'] + temp['reason_data'] = item['reason_data'] + newlist.append(temp) + return newlist + + class AlarmDetailView(forms.ModalFormView): form_class = alarm_forms.DetailAlarmForm template_name = constants.TEMPLATE_PREFIX + 'alarms/detail.html' @@ -344,10 +359,15 @@ class AlarmHistoryView(tables.DataTableView): return super(AlarmHistoryView, self).dispatch(*args, **kwargs) def get_data(self): - # to be implemented + id = self.kwargs['id'] + name = self.kwargs['name'] results = [] - - return results + try: + results = api.monitor.alarm_history(self.request, id) + except: + messages.error(self.request, + _("Could not retrieve alarm history for %s") % id) + return transform_alarm_history(results, name) def get_context_data(self, **kwargs): context = super(AlarmHistoryView, self).get_context_data(**kwargs) diff --git a/monitoring/api/monitor.py b/monitoring/api/monitor.py index 28629bee..66a3b068 100644 --- a/monitoring/api/monitor.py +++ b/monitoring/api/monitor.py @@ -67,6 +67,10 @@ def alarm_delete(request, alarm_id): return monclient(request).alarms.delete(alarm_id=alarm_id) +def alarm_history(request, alarm_id): + return monclient(request).alarms.history(alarm_id=alarm_id) + + def alarm_get(request, alarm_id): return monclient(request).alarms.get(alarm_id=alarm_id)