def get_notifications(self, for_timestamp=None): if for_timestamp is None: for_timestamp = self.get_last_usable_timestamp() notifications = NotificationCheck.check_for( for_timestamp=for_timestamp, active=True) non_empty = [n for n in notifications if n[1]] return non_empty
def create(self, request, *args, **kwargs): f = NotificaitonCheckForm(data=request.POST) if f.is_valid(): d = f.cleaned_data return NotificationCheck.create(**d) self.errors = f.errors
def test_notifications_api(self): capi = CollectorAPI() start = datetime.utcnow().replace(tzinfo=pytz.utc) start_aligned = align_period_start(start, self.service.check_interval) end_aligned = start_aligned + self.service.check_interval # for (metric_name, field_opt, use_service, # use_resource, use_label, use_ows_service, # minimum, maximum, thresholds,) in thresholds: notifications_config = ( 'geonode is not working', 'detects when requests are not handled', ( ( 'request.count', 'min_value', False, False, False, False, 0, 10, None, 'Number of handled requests is lower than', ), ( 'response.time', 'max_value', False, False, False, False, 500, None, None, 'Response time is higher than', ), )) nc = NotificationCheck.create(*notifications_config) self.assertTrue(nc.definitions.all().count() == 2) user = self.u2 pwd = self.passwd2 self.client.login(username=user.username, password=pwd) for nc in NotificationCheck.objects.all(): notifications_config_url = reverse( 'monitoring:api_user_notification_config', args=(nc.id, )) nc_form = nc.get_user_form() self.assertTrue(nc_form) self.assertTrue(nc_form.fields.keys()) vals = [1000000, 100000] data = {'emails': []} data['emails'] = '\n'.join(data['emails']) idx = 0 for fname, field in nc_form.fields.items(): if fname in self.reserved_fields: continue data[fname] = vals[idx] idx += 1 resp = self.client.post(notifications_config_url, data) self.assertEqual(resp.status_code, 400) vals = [7, 600] data = { 'emails': '\n'.join([self.u.email, self.u2.email, '*****@*****.**']) } idx = 0 for fname, field in nc_form.fields.items(): if fname in self.reserved_fields: continue data[fname] = vals[idx] idx += 1 # data['emails'] = '\n'.join(data['emails']) resp = self.client.post(notifications_config_url, data) nc.refresh_from_db() self.assertEqual(resp.status_code, 200, resp) _emails = data['emails'].split('\n')[-1:] _users = data['emails'].split('\n')[:-1] self.assertEqual(set([u.email for u in nc.get_users()]), set(_users)) self.assertEqual(set([email for email in nc.get_emails()]), set(_emails)) metric_rq_count = Metric.objects.get(name='request.count') metric_rq_time = Metric.objects.get(name='response.time') MetricValue.add(metric_rq_count, start_aligned, end_aligned, self.service, label="Count", value_raw=0, value_num=0, value=0) MetricValue.add(metric_rq_time, start_aligned, end_aligned, self.service, label="Count", value_raw=700, value_num=700, value=700) nc = NotificationCheck.objects.get() self.assertTrue(len(nc.get_emails()) > 0) self.assertTrue(len(nc.get_users()) > 0) self.assertEqual(nc.last_send, None) self.assertTrue(nc.can_send) self.assertEqual(len(mail.outbox), 0) # make sure inactive will not trigger anything nc.active = False nc.save() capi.emit_notifications(start) self.assertEqual(len(mail.outbox), 0) nc.active = True nc.save() capi.emit_notifications(start) self.assertTrue(nc.receivers.all().count() > 0) self.assertEqual(len(mail.outbox), nc.receivers.all().count()) nc.refresh_from_db() notifications_url = reverse('monitoring:api_user_notifications') nresp = self.client.get(notifications_url) self.assertEqual(nresp.status_code, 200) ndata = json.loads(nresp.content) self.assertEqual( set([n['id'] for n in ndata['data']]), set(NotificationCheck.objects.all().values_list('id', flat=True))) self.assertTrue(isinstance(nc.last_send, datetime)) self.assertFalse(nc.can_send) mail.outbox = [] self.assertEqual(len(mail.outbox), 0) capi.emit_notifications(start) self.assertEqual(len(mail.outbox), 0) nc.last_send = start - nc.grace_period nc.save() self.assertTrue(nc.can_send) mail.outbox = [] self.assertEqual(len(mail.outbox), 0) capi.emit_notifications(start) self.assertEqual(len(mail.outbox), nc.receivers.all().count())
def test_monitoring_checks(self): start = datetime.utcnow().replace(tzinfo=pytz.utc) start_aligned = align_period_start(start, self.service.check_interval) end_aligned = start_aligned + self.service.check_interval # sanity check self.assertTrue(start_aligned < start < end_aligned) ows_service = OWSService.objects.get(name='WFS') resource, _ = MonitoredResource.objects.get_or_create(type='layer', name='test:test') resource2, _ = MonitoredResource.objects.get_or_create( type='layer', name='test:test2') label, _ = MetricLabel.objects.get_or_create(name='discount') MetricValue.add(self.metric, start_aligned, end_aligned, self.service, label="Count", value_raw=10, value_num=10, value=10) uthreshold = [ (self.metric.name, 'min_value', False, False, False, False, 0, 100, None, "Min number of request"), (self.metric.name, 'max_value', False, False, False, False, 1000, None, None, "Max number of request"), ] notification_data = { 'name': 'check requests name', 'description': 'check requests description', 'severity': 'warning', 'user_threshold': uthreshold } nc = NotificationCheck.create(**notification_data) mc = MetricNotificationCheck.objects.create( notification_check=nc, service=self.service, metric=self.metric, min_value=None, definition=nc.definitions.first(), max_value=None, max_timeout=None) with self.assertRaises(ValueError): mc.check_metric(for_timestamp=start) mc.min_value = 11 mc.save() with self.assertRaises(mc.MetricValueError): mc.check_metric(for_timestamp=start) mc.min_value = 1 mc.max_value = 11 mc.save() self.assertTrue(mc.check_metric(for_timestamp=start)) MetricValue.add(self.metric, start_aligned, end_aligned, self.service, label="discount", value_raw=10, value_num=10, value=10, ows_service=ows_service) mc.min_value = 11 mc.max_value = None mc.ows_service = ows_service mc.save() with self.assertRaises(mc.MetricValueError): mc.check_metric(for_timestamp=start) MetricValue.add(self.metric, start_aligned, end_aligned, self.service, label="discount", value_raw=10, value_num=10, value=10, resource=resource) mc.min_value = 1 mc.max_value = 10 mc.ows_service = None mc.resource = resource mc.save() self.assertTrue(mc.check_metric(for_timestamp=start)) MetricValue.objects.all().delete() MetricValue.add(self.metric, start_aligned, end_aligned, self.service, label="discount", value_raw=10, value_num=10, value=10, resource=resource2) # this should raise ValueError, because MetricValue won't match with self.assertRaises(ValueError): mc.check_metric(for_timestamp=start)
def test_notifications_api(self): capi = CollectorAPI() start = datetime.utcnow().replace(tzinfo=pytz.utc) start_aligned = align_period_start(start, self.service.check_interval) end_aligned = start_aligned + self.service.check_interval # for (metric_name, field_opt, use_service, # use_resource, use_label, use_ows_service, # minimum, maximum, thresholds,) in thresholds: notifications_config = ('geonode is not working', 'detects when requests are not handled', (('request.count', 'min_value', False, False, False, False, 0, 10, None, 'Number of handled requests is lower than',), ('response.time', 'max_value', False, False, False, False, 500, None, None, 'Response time is higher than',),)) nc = NotificationCheck.create(*notifications_config) self.assertTrue(nc.definitions.all().count() == 2) user = self.u2 pwd = self.passwd2 self.client.login(username=user.username, password=pwd) for nc in NotificationCheck.objects.all(): notifications_config_url = reverse( 'monitoring:api_user_notification_config', args=(nc.id,)) nc_form = nc.get_user_form() self.assertTrue(nc_form) self.assertTrue(nc_form.fields.keys()) vals = [1000000, 100000] data = {'emails': []} data['emails'] = '\n'.join(data['emails']) idx = 0 for fname, field in nc_form.fields.items(): if fname in self.reserved_fields: continue data[fname] = vals[idx] idx += 1 resp = self.client.post(notifications_config_url, data) self.assertEqual(resp.status_code, 400) vals = [7, 600] data = {'emails': '\n'.join( [self.u.email, self.u2.email, '*****@*****.**'])} idx = 0 for fname, field in nc_form.fields.items(): if fname in self.reserved_fields: continue data[fname] = vals[idx] idx += 1 # data['emails'] = '\n'.join(data['emails']) resp = self.client.post(notifications_config_url, data) nc.refresh_from_db() self.assertEqual(resp.status_code, 200, resp) _emails = data['emails'].split('\n')[-1:] _users = data['emails'].split('\n')[:-1] self.assertEqual( set([u.email for u in nc.get_users()]), set(_users)) self.assertEqual( set([email for email in nc.get_emails()]), set(_emails)) metric_rq_count = Metric.objects.get(name='request.count') metric_rq_time = Metric.objects.get(name='response.time') MetricValue.add(metric_rq_count, start_aligned, end_aligned, self.service, label="Count", value_raw=0, value_num=0, value=0) MetricValue.add(metric_rq_time, start_aligned, end_aligned, self.service, label="Count", value_raw=700, value_num=700, value=700) nc = NotificationCheck.objects.get() self.assertTrue(len(nc.get_emails()) > 0) self.assertTrue(len(nc.get_users()) > 0) self.assertEqual(nc.last_send, None) self.assertTrue(nc.can_send) self.assertEqual(len(mail.outbox), 0) # make sure inactive will not trigger anything nc.active = False nc.save() capi.emit_notifications(start) self.assertEqual(len(mail.outbox), 0) nc.active = True nc.save() capi.emit_notifications(start) self.assertTrue(nc.receivers.all().count() > 0) self.assertEqual(len(mail.outbox), nc.receivers.all().count()) nc.refresh_from_db() notifications_url = reverse('monitoring:api_user_notifications') nresp = self.client.get(notifications_url) self.assertEqual(nresp.status_code, 200) ndata = json.loads(nresp.content) self.assertEqual(set([n['id'] for n in ndata['data']]), set(NotificationCheck.objects.all().values_list('id', flat=True))) self.assertTrue(isinstance(nc.last_send, datetime)) self.assertFalse(nc.can_send) mail.outbox = [] self.assertEqual(len(mail.outbox), 0) capi.emit_notifications(start) self.assertEqual(len(mail.outbox), 0) nc.last_send = start - nc.grace_period nc.save() self.assertTrue(nc.can_send) mail.outbox = [] self.assertEqual(len(mail.outbox), 0) capi.emit_notifications(start) self.assertEqual(len(mail.outbox), nc.receivers.all().count())
def test_monitoring_checks(self): start = datetime.utcnow().replace(tzinfo=pytz.utc) start_aligned = align_period_start(start, self.service.check_interval) end_aligned = start_aligned + self.service.check_interval # sanity check self.assertTrue(start_aligned < start < end_aligned) ows_service = OWSService.objects.get(name='WFS') resource, _ = MonitoredResource.objects.get_or_create( type='layer', name='test:test') resource2, _ = MonitoredResource.objects.get_or_create( type='layer', name='test:test2') label, _ = MetricLabel.objects.get_or_create(name='discount') MetricValue.add(self.metric, start_aligned, end_aligned, self.service, label="Count", value_raw=10, value_num=10, value=10) uthreshold = [( self.metric.name, 'min_value', False, False, False, False, 0, 100, None, "Min number of request"), (self.metric.name, 'max_value', False, False, False, False, 1000, None, None, "Max number of request"), ] notification_data = {'name': 'check requests name', 'description': 'check requests description', 'severity': 'warning', 'user_threshold': uthreshold} nc = NotificationCheck.create(**notification_data) mc = MetricNotificationCheck.objects.create(notification_check=nc, service=self.service, metric=self.metric, min_value=None, definition=nc.definitions.first( ), max_value=None, max_timeout=None) with self.assertRaises(ValueError): mc.check_metric(for_timestamp=start) mc.min_value = 11 mc.save() with self.assertRaises(mc.MetricValueError): mc.check_metric(for_timestamp=start) mc.min_value = 1 mc.max_value = 11 mc.save() self.assertTrue(mc.check_metric(for_timestamp=start)) MetricValue.add(self.metric, start_aligned, end_aligned, self.service, label="discount", value_raw=10, value_num=10, value=10, ows_service=ows_service) mc.min_value = 11 mc.max_value = None mc.ows_service = ows_service mc.save() with self.assertRaises(mc.MetricValueError): mc.check_metric(for_timestamp=start) MetricValue.add(self.metric, start_aligned, end_aligned, self.service, label="discount", value_raw=10, value_num=10, value=10, resource=resource) mc.min_value = 1 mc.max_value = 10 mc.ows_service = None mc.resource = resource mc.save() self.assertTrue(mc.check_metric(for_timestamp=start)) MetricValue.objects.all().delete() MetricValue.add(self.metric, start_aligned, end_aligned, self.service, label="discount", value_raw=10, value_num=10, value=10, resource=resource2) # this should raise ValueError, because MetricValue won't match with self.assertRaises(ValueError): mc.check_metric(for_timestamp=start)