Skip to content

Commit

Permalink
Raise a configuration warning log only for hosts that do not have any…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
mohierf committed Mar 9, 2017
1 parent 98a74b9 commit 032ceac
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 10 deletions.
6 changes: 6 additions & 0 deletions alignak/objects/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
7 changes: 0 additions & 7 deletions alignak/objects/schedulingitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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" % (
Expand Down
4 changes: 2 additions & 2 deletions test/alignak_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 >>>----------------------------------"
Expand Down
25 changes: 24 additions & 1 deletion test/cfg/nonotif/services.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,6 +24,8 @@ define service{
}

define service{
contact_groups test_contact

active_checks_enabled 1
check_command check_service!ok
check_interval 1
Expand All @@ -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
}
51 changes: 51 additions & 0 deletions test/test_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 032ceac

Please sign in to comment.