Skip to content

Commit

Permalink
Merge pull request #1710 from geektophe/fix_service_descr_in_template
Browse files Browse the repository at this point in the history
Fix: fixed svc template level defined description
  • Loading branch information
naparuba committed Oct 1, 2015
2 parents 0832db8 + 8bb0f31 commit 986b42b
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 57 deletions.
4 changes: 1 addition & 3 deletions shinken/objects/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ def add_items(self, items, index_items):
self.add_item(i, index_items)


def manage_conflict(self, item, name, partial=False):
def manage_conflict(self, item, name):
"""
Cheks if an object holding the same name already exists in the index.
Expand All @@ -774,8 +774,6 @@ def manage_conflict(self, item, name, partial=False):
"""
if item.is_tpl():
existing = self.name_to_template[name]
elif partial:
existing = self.name_to_partial[name]
else:
existing = self.name_to_item[name]
existing_prio = getattr(
Expand Down
64 changes: 11 additions & 53 deletions shinken/objects/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -1294,11 +1294,6 @@ class Services(Items):
name_property = 'unique_key' # only used by (un)indexitem (via 'name_property')
inner_class = Service # use for know what is in items

def __init__(self, items, index_items=True):
self.partial_services = {}
self.name_to_partial = {}
super(Services, self).__init__(items, index_items)

def add_template(self, tpl):
"""
Adds and index a template into the `templates` container.
Expand All @@ -1319,7 +1314,7 @@ def add_template(self, tpl):
tpl = self.index_template(tpl)
self.templates[tpl.id] = tpl

def add_item(self, item, index=True, was_partial=False):
def add_item(self, item, index=True):
"""
Adds and index an item into the `items` container.
Expand All @@ -1338,51 +1333,23 @@ def add_item(self, item, index=True, was_partial=False):
in_file = " in %s" % source
else:
in_file = ""
if not hname and not hgname and not sdesc:
if not hname and not hgname:
mesg = "a %s has been defined without host_name nor " \
"hostgroups nor service_description%s" % (objcls, in_file)
"hostgroups%s" % (objcls, in_file)
item.configuration_errors.append(mesg)
elif not sdesc or sdesc and not hgname and not hname and not was_partial:
self.add_partial_service(item, index, (objcls, hname, hgname, sdesc, in_file))
return

if index is True:
item = self.index_item(item)
if hname and sdesc:
item = self.index_item(item)
else:
mesg = "a %s has been defined without host_name nor " \
"service_description%s" % (objcls, in_file)
item.configuration_errors.append(mesg)
return
self.items[item.id] = item

def add_partial_service(self, item, index=True, var_tuple=None):
if var_tuple is None:
return

objcls, hname, hgname, sdesc, in_file = var_tuple
use = getattr(item, 'use', [])


if use == []:
mesg = "a %s has been defined without host_name nor " \
"hostgroups nor service_description and " \
"there is no use to create a unique key%s" % (objcls, in_file)
item.configuration_errors.append(mesg)
return

use = ','.join(use)
if sdesc:
name = "::".join((sdesc, use))
elif hname:
name = "::".join((hname, use))
else:
name = "::".join((hgname, use))

if name in self.name_to_partial:
item = self.manage_conflict(item, name, partial=True)
self.name_to_partial[name] = item

self.partial_services[item.id] = item

# Inheritance for just a property
def apply_partial_inheritance(self, prop):
for i in itertools.chain(self.items.itervalues(),
self.partial_services.itervalues(),
self.templates.itervalues()):
i.get_property_by_inheritance(prop, 0)
# If a "null" attribute was inherited, delete it
Expand All @@ -1402,21 +1369,13 @@ def apply_inheritance(self):
for prop in cls.properties:
self.apply_partial_inheritance(prop)
for i in itertools.chain(self.items.itervalues(),
self.partial_services.itervalues(),
self.templates.itervalues()):
i.get_customs_properties_by_inheritance(0)

for i in self.partial_services.itervalues():
self.add_item(i, True, True)

del self.partial_services
del self.name_to_partial


def linkify_templates(self):
# First we create a list of all templates
for i in itertools.chain(self.items.itervalues(),
self.partial_services.itervalues(),
self.templates.itervalues()):
self.linkify_item_templates(i)
for i in self:
Expand Down Expand Up @@ -1831,8 +1790,7 @@ def explode(self, hosts, hostgroups, contactgroups,

# Then for every host create a copy of the service with just the host
# because we are adding services, we can't just loop in it
for id in self.items.keys():
s = self.items[id]
for s in self.items.values():
# items::explode_host_groups_into_hosts
# take all hosts from our hostgroup_name into our host_name property
self.explode_host_groups_into_hosts(s, hosts, hostgroups)
Expand Down
21 changes: 21 additions & 0 deletions test/etc/service_generators/hosts.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,27 @@ define host{
ISDN1$(4)$$(80%!95%)$
}

define hostgroup {
hostgroup_name cisco-routers
alias Cisco Routers
}

define host{
use generic-host
host_name r1
address 1.1.1.1
check_command check-host-alive-parent!up!$HOSTSTATE:r1$
hostgroups cisco-routers
}

define host{
use generic-host
host_name r2
address 1.1.1.2
check_command check-host-alive-parent!up!$HOSTSTATE:r2$
hostgroups cisco-routers
}

define host{
address 127.0.0.1
check_command check-host-alive-parent!up!$HOSTSTATE:test_router_0$
Expand Down
12 changes: 12 additions & 0 deletions test/etc/service_generators/services.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ define service{
default_value 38%!24%
}

define service{
name ssh-critical-service
use generic-service
service_description SSH
check_command check_service!$KEY$!$VALUE$
register 0
}

define service {
use ssh-critical-service
hostgroup_name cisco-routers
}

define service{
check_command check_service!$KEY$!$VALUE$
Expand Down
6 changes: 6 additions & 0 deletions test/test_service_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ def test_service_generators_array(self):
self.assertIsNot(svc, None)
self.assertEqual('check_service!4!80%!95%', svc.check_command.call)

def test_service_inherited_description(self):
s1 = self.sched.services.find_srv_by_name_and_hostname("r1", 'SSH')
s2 = self.sched.services.find_srv_by_name_and_hostname("r2", 'SSH')
self.assertIsNotNone(s1)
self.assertIsNotNone(s2)

def test_key_as_descr(self):
svc_h = self.sched.services.find_srv_by_name_and_hostname("test_host_1", "Generated Service H")
svc_i = self.sched.services.find_srv_by_name_and_hostname("test_host_1", "Generated Service I")
Expand Down
2 changes: 1 addition & 1 deletion test/test_service_nohost.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_service_with_no_host(self):
len( [ log
for log in logs
if re.search(
'a service item has been defined without unique_key ',
'a service has been defined without host_name nor hostgroups ',
log)
])
)
Expand Down

0 comments on commit 986b42b

Please sign in to comment.