From 032ceacfaaaae1409bb3145f4098729c5b650f40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20MOHIER?= Date: Thu, 9 Mar 2017 07:34:22 +0100 Subject: [PATCH] Raise a configuration warning log only for hosts that do not have any contact if they have enabled notification Add a test to confirm that notifications are sent to host contacts when a service has no defined contacts --- alignak/objects/host.py | 6 ++++ alignak/objects/schedulingitem.py | 7 ----- test/alignak_test.py | 4 +-- test/cfg/nonotif/services.cfg | 25 ++++++++++++++- test/test_notifications.py | 51 +++++++++++++++++++++++++++++++ 5 files changed, 83 insertions(+), 10 deletions(-) diff --git a/alignak/objects/host.py b/alignak/objects/host.py index f6c7c9be6..116fdcfac 100644 --- a/alignak/objects/host.py +++ b/alignak/objects/host.py @@ -331,6 +331,12 @@ def is_correct(self): self.configuration_errors.append(msg) state = False + # Ok now we manage special cases... + if self.notifications_enabled and self.contacts == []: + msg = "[%s::%s] notifications are enabled but no contacts nor contact_groups " \ + "property is defined for this host" % (self.my_type, self.get_name()) + self.configuration_warnings.append(msg) + return super(Host, self).is_correct() and state def get_services(self): diff --git a/alignak/objects/schedulingitem.py b/alignak/objects/schedulingitem.py index bf39508b0..ffdcdad20 100644 --- a/alignak/objects/schedulingitem.py +++ b/alignak/objects/schedulingitem.py @@ -3035,13 +3035,6 @@ def is_correct(self): if not hasattr(self, 'notification_period'): self.notification_period = None - # Ok now we manage special cases... - if self.notifications_enabled and self.contacts == []: - msg = "[%s::%s] no contacts nor contact_groups property" % ( - self.my_type, self.get_name() - ) - self.configuration_warnings.append(msg) - # If we got an event handler, it should be valid if getattr(self, 'event_handler', None) and not self.event_handler.is_valid(): msg = "[%s::%s] event_handler '%s' is invalid" % ( diff --git a/test/alignak_test.py b/test/alignak_test.py index 2d401b2ef..6f5f1f6d2 100644 --- a/test/alignak_test.py +++ b/test/alignak_test.py @@ -452,9 +452,9 @@ def show_actions(self): else: hst = self.schedulers['scheduler-master'].sched.find_item_by_id(item.host) ref = "host: %s svc: %s" % (hst.get_name(), item.get_name()) - print "NOTIFICATION %s %s %s %s %s" % (a.uuid, ref, a.type, + print "NOTIFICATION %s %s %s %s %s %s" % (a.uuid, ref, a.type, time.asctime(time.localtime(a.t_to_go)), - a.status) + a.status, a.contact_name) elif a.is_a == 'eventhandler': print "EVENTHANDLER:", a print "--- actions >>>----------------------------------" diff --git a/test/cfg/nonotif/services.cfg b/test/cfg/nonotif/services.cfg index b556f3b59..9cad84862 100644 --- a/test/cfg/nonotif/services.cfg +++ b/test/cfg/nonotif/services.cfg @@ -3,7 +3,6 @@ define service{ check_freshness 0 check_interval 1 check_period 24x7 - contact_groups test_contact event_handler_enabled 0 failure_prediction_enabled 1 flap_detection_enabled 1 @@ -25,6 +24,8 @@ define service{ } define service{ + contact_groups test_contact + active_checks_enabled 1 check_command check_service!ok check_interval 1 @@ -41,3 +42,25 @@ define service{ action_url /alignak/pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$ _custname custvalue } + +define service{ + ; No defined contact nor contacts group + ; but notifications are enabled + notifications_enabled 1 + + active_checks_enabled 1 + check_command check_service!ok + check_interval 1 + host_name test_host_0 + icon_image ../../docs/images/tip.gif?host=$HOSTNAME$&srv=$SERVICEDESC$ + icon_image_alt icon alt string + notes just a notes string + retry_interval 1 + service_description test_ok_no_contacts + servicegroups servicegroup_01,ok + use generic-service + event_handler eventhandler + notes_url /alignak/wiki/doku.php/$HOSTNAME$/$SERVICEDESC$ + action_url /alignak/pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$ + _custname custvalue +} diff --git a/test/test_notifications.py b/test/test_notifications.py index 65d3e8868..8224ccd19 100644 --- a/test/test_notifications.py +++ b/test/test_notifications.py @@ -138,6 +138,57 @@ def test_1_nonotif_enablewithcmd(self): self.assert_actions_match(0, 'serviceoutput CRITICAL', 'command') self.assert_actions_match(1, 'serviceoutput OK', 'command') + def test_1_notifications_service_with_no_contacts(self): + """ Test notifications are sent to host contacts for a service with no defined contacts + + :return: None + """ + self.print_header() + self.setup_with_file('cfg/cfg_nonotif.cfg') + + host = self.schedulers['scheduler-master'].sched.hosts.find_by_name("test_host_0") + host.checks_in_progress = [] + host.act_depend_of = [] # ignore the router + + svc = self.schedulers['scheduler-master'].sched.services.find_srv_by_name_and_hostname( + "test_host_0", "test_ok_no_contacts") + # To make tests quicker we make notifications sent very quickly + svc.notification_interval = 0.1 + svc.checks_in_progress = [] + svc.act_depend_of = [] # no hostchecks on critical checkresults + svc.event_handler_enabled = False + assert svc.notifications_enabled + + self.scheduler_loop(1, [[host, 0, 'UP'], [svc, 0, 'OK']]) + time.sleep(0.1) + assert 0 == svc.current_notification_number, 'All OK no notifications' + self.assert_actions_count(0) + + self.scheduler_loop(1, [[svc, 2, 'CRITICAL']]) + time.sleep(0.1) + assert "SOFT" == svc.state_type + assert 0 == svc.current_notification_number, 'Critical SOFT, no notifications' + self.assert_actions_count(0) + + self.scheduler_loop(2, [[svc, 2, 'CRITICAL']]) + assert "HARD" == svc.state_type + assert "CRITICAL" == svc.state + assert 1 == svc.current_notification_number, 'Critical HARD, must have 1 notification' + self.assert_actions_count(2) + self.assert_actions_match(0, 'VOID', 'command') + self.assert_actions_match(1, 'serviceoutput CRITICAL', 'command') + + self.scheduler_loop(1, [[svc, 0, 'OK']]) + time.sleep(0.1) + assert 0 == svc.current_notification_number, 'Ok HARD, no notifications' + self.assert_actions_count(2) + self.assert_actions_match(0, 'serviceoutput CRITICAL', 'command') + self.assert_actions_match(1, 'serviceoutput OK', 'command') + + self.assert_actions_count(2) + self.assert_actions_match(0, 'serviceoutput CRITICAL', 'command') + self.assert_actions_match(1, 'serviceoutput OK', 'command') + def test_2_notifications(self): """ Test notifications sent in normal mode