[Pkg-nagios-changes] [SCM] UNNAMED PROJECT branch, debian/master, updated. 810edbdd3feedbfe37f4a65bee50b57b2f60fa2a

Naparuba naparuba at gmail.com
Tue Feb 28 22:11:54 UTC 2012


The following commit has been merged in the debian/master branch:
commit 9c2f449f1d0e90fc2d12ee11e10a0b6eaf0e0dc6
Author: Naparuba <naparuba at gmail.com>
Date:   Wed Jan 4 15:04:56 2012 +0100

    Add : add the complex group management but for srv templates on host template. Should rename the complex expression functions name now it manage hostgroups AND templates :p

diff --git a/shinken/objects/hostgroup.py b/shinken/objects/hostgroup.py
index d8368b9..2dec1b7 100644
--- a/shinken/objects/hostgroup.py
+++ b/shinken/objects/hostgroup.py
@@ -63,22 +63,23 @@ class Hostgroup(Itemgroup):
             return []
 
 
-    #We fillfull properties with template ones if need
-    #Because hostgroup we call may not have it's members
-    #we call get_hosts_by_explosion on it
+    # We fillfull properties with template ones if need
+    # Because hostgroup we call may not have it's members
+    # we call get_hosts_by_explosion on it
     def get_hosts_by_explosion(self, hostgroups):
-        #First we tag the hg so it will not be explode
-        #if a son of it already call it
+        # First we tag the hg so it will not be explode
+        # if a son of it already call it
         self.already_explode = True
 
-        #Now the recursiv part
-        #rec_tag is set to False avery HG we explode
-        #so if True here, it must be a loop in HG
-        #calls... not GOOD!
+        # Now the recursiv part
+        # rec_tag is set to False avery HG we explode
+        # so if True here, it must be a loop in HG
+        # calls... not GOOD!
         if self.rec_tag:
             print "Error : we've got a loop in hostgroup definition", self.get_name()
             return self.get_hosts()
-        #Ok, not a loop, we tag it and continue
+
+        # Ok, not a loop, we tag it and continue
         self.rec_tag = True
 
         hg_mbrs = self.get_hostgroup_members()
diff --git a/shinken/objects/item.py b/shinken/objects/item.py
index ab549cb..793fbe4 100644
--- a/shinken/objects/item.py
+++ b/shinken/objects/item.py
@@ -982,7 +982,7 @@ class Items(object):
                 setattr(i, prop, com_list)
 
 
-    def evaluate_hostgroup_expression(self, expr, hosts, hostgroups):
+    def evaluate_hostgroup_expression(self, expr, hosts, hostgroups, look_in='hostgroups'):
         begin = 0
         end = len(expr)
         ctxres = hg_name_parse_EXPR(expr, begin, end) 
@@ -993,11 +993,10 @@ class Items(object):
             return []
         
         str_setexpr = hg_name_rebuild_str(ctxres.full_res)
-        
         # We must protect the eval() against some names that will be match as
         # Python things like - or print and not real names. So we "change" them with __OTHERNAME__
         # values in the HostGroup_Name_Parse_Ctx class.
-        groupsname2hostsnames = hg_name_get_groupnames(ctxres.full_res, hosts, hostgroups)
+        groupsname2hostsnames = hg_name_get_groupnames(ctxres.full_res, hosts, hostgroups, look_in=look_in)
         newgroupname2hostnames = {}
         for gn, val in groupsname2hostsnames.items():
             gn = gn.replace('-', HostGroup_Name_Parse_Ctx.minus_sign_in_name)
@@ -1331,17 +1330,19 @@ def get_all_host_names_set(hosts):
     )
 
 
-def hg_name_get_groupnames(all_res, hosts, hostgroups, res=None):
+# Get the groups (or templates) that match this. We can look for hostgroups
+# or templates.
+def hg_name_get_groupnames(all_res, hosts, hostgroups, res=None, look_in='hostgroups'):
     if res is None:
         res = {}
 
     for tok in all_res:
         if isinstance(tok, tuple):
-            hg_name_get_groupnames(tok[0], hosts, hostgroups, res)
-            hg_name_get_groupnames(tok[1], hosts, hostgroups, res)
+            hg_name_get_groupnames(tok[0], hosts, hostgroups, res, look_in)
+            hg_name_get_groupnames(tok[1], hosts, hostgroups, res, look_in)
             continue
         if isinstance(tok, list):
-            hg_name_get_groupnames(tok, hosts, hostgroups, res)
+            hg_name_get_groupnames(tok, hosts, hostgroups, res, look_in)
             continue
         
         save_tok = tok
@@ -1356,8 +1357,17 @@ def hg_name_get_groupnames(all_res, hosts, hostgroups, res=None):
         if save_tok == '*':
             elts = get_all_host_names_set(hosts)
         else:
-            # we got a group name :
-            members = hostgroups.get_members_by_name(tok)
+            members = []
+            # We got 2 possibilities : hostgroups or templates
+            if look_in == 'hostgroups':
+                # we got a group name :
+                members = hostgroups.get_members_by_name(tok)
+            else:  # == templates
+                # It's a dict of template.
+                # So first find the template, and then get all it's
+                # hosts
+                members = hosts.find_hosts_that_use_template(tok)
+                print "GOT COMPLEX HOST MEMBERS", members
             # TODO: check why:
             # sometimes we get a list, sometimes we get a string of hosts name which are ',' separated..
             if isinstance(members, list):
diff --git a/shinken/objects/service.py b/shinken/objects/service.py
index 017a9d1..1bb70b7 100644
--- a/shinken/objects/service.py
+++ b/shinken/objects/service.py
@@ -1104,7 +1104,9 @@ class Services(Items):
             # But for template it's more tricky : it's a template name
             # we've got, not a real host_name/ So we must get a list of host_name
             # that use this template
-            hosts_from_tpl = hosts.find_hosts_that_use_template(hname.strip())
+            # Use the complex expression manager for it, it will call find_hosts_that_use_template
+            # for the templates it think it's useful
+            hosts_from_tpl = self.evaluate_hostgroup_expression(hname.strip(), hosts, hosts.templates, look_in='templates')
 
             # And now copy our real services
             for n in hosts_from_tpl:
diff --git a/test/etc/service_tpl_on_host_tpl/hosts.cfg b/test/etc/service_tpl_on_host_tpl/hosts.cfg
index 93aded2..11c4c85 100644
--- a/test/etc/service_tpl_on_host_tpl/hosts.cfg
+++ b/test/etc/service_tpl_on_host_tpl/hosts.cfg
@@ -111,3 +111,46 @@ define host{
   use                            layer1
 }
 
+
+
+##### For complex expressions
+define host{
+     use      generic-host
+     name     linux
+     register 0
+}
+
+define host{
+     use      generic-host
+     name     windows
+     register 0
+}
+
+
+define host{
+     use      generic-host
+     name     http
+     register 0
+}
+
+
+define host{
+  address                        127.0.0.1
+  alias                          up_0
+  check_command                  check-host-alive-parent!up!$HOSTSTATE:test_router_0$
+  event_handler                  eventhandler
+  check_period                   24x7
+  host_name                      host_linux_http
+  use                            linux,http
+}
+
+define host{
+  address                        127.0.0.1
+  alias                          up_0
+  check_command                  check-host-alive-parent!up!$HOSTSTATE:test_router_0$
+  event_handler                  eventhandler
+  check_period                   24x7
+  host_name                      host_windows_http
+  use                            windows,http
+}
+
diff --git a/test/etc/service_tpl_on_host_tpl/services.cfg b/test/etc/service_tpl_on_host_tpl/services.cfg
index b60bd4e..922fc21 100644
--- a/test/etc/service_tpl_on_host_tpl/services.cfg
+++ b/test/etc/service_tpl_on_host_tpl/services.cfg
@@ -75,3 +75,62 @@ define service{
   register                       0
 }
 
+
+
+###Complex expression now
+define service{
+  #Here host_name is just a template name, and the high level layer
+  host_name                      http&linux
+  service_description            http_AND_linux
+  use                            generic-service
+  check_command                  check_service!ok
+
+  #And of course make it a template
+  register                       0
+}
+
+define service{
+  #Here host_name is just a template name, and the high level layer
+  host_name                      http|linux
+  service_description            http_OR_linux
+  use                            generic-service
+  check_command                  check_service!ok
+
+  #And of course make it a template
+  register                       0
+}
+
+
+define service{
+  #Here host_name is just a template name, and the high level layer
+  host_name                      http!linux
+  service_description            http_BUT_NOT_linux
+  use                            generic-service
+  check_command                  check_service!ok
+
+  #And of course make it a template
+  register                       0
+}
+
+
+define service{
+  #Here host_name is just a template name, and the high level layer
+  host_name                      *!linux
+  service_description            http_ALL_BUT_NOT_linux
+  use                            generic-service
+  check_command                  check_service!ok
+
+  #And of course make it a template
+  register                       0
+}
+
+define service{
+  #Here host_name is just a template name, and the high level layer
+  host_name                      (*!linux)|linux
+  service_description            http_ALL_BUT_NOT_linux_AND_EVEN_LINUX
+  use                            generic-service
+  check_command                  check_service!ok
+
+  #And of course make it a template
+  register                       0
+}
diff --git a/test/test_service_tpl_on_host_tpl.py b/test/test_service_tpl_on_host_tpl.py
index 0a5d0bd..5c99d6c 100755
--- a/test/test_service_tpl_on_host_tpl.py
+++ b/test/test_service_tpl_on_host_tpl.py
@@ -58,6 +58,55 @@ class TestSrvTplOnHostTpl(ShinkenTest):
         self.assert_(svc is not None)
 
 
+    # And look for multy layer template too. Like a service is apply on
+    # layer1, that use layer2. And srv is apply on layer2
+    def test_complex_expr(self):
+        h_linux = self.sched.hosts.find_by_name("host_linux_http")
+        print "All the host_linux_http services"
+        for s in h_linux.services:
+            print s.get_dbg_name()
+
+        # The linux and http service should exist on the linux host
+        svc = self.sched.services.find_srv_by_name_and_hostname("host_linux_http", "http_AND_linux")
+        self.assert_(svc is not None)
+
+        # But not on the windows one
+        h_windows = self.sched.hosts.find_by_name("host_windows_http")
+        print "All the host_windows_http services"
+        for s in h_windows.services:
+            print s.get_dbg_name()
+
+        # The linux and http service should exist on the linux host
+        svc = self.sched.services.find_srv_by_name_and_hostname("host_windows_http", "http_AND_linux")
+        self.assert_(svc is None)
+
+        # The http_OR_linux should be every where
+        svc = self.sched.services.find_srv_by_name_and_hostname("host_linux_http", "http_OR_linux")
+        self.assert_(svc is not None)
+        svc = self.sched.services.find_srv_by_name_and_hostname("host_windows_http", "http_OR_linux")
+        self.assert_(svc is not None)
+
+        # The http_BUT_NOT_linux should be in the windows host only
+        svc = self.sched.services.find_srv_by_name_and_hostname("host_linux_http", "http_BUT_NOT_linux")
+        self.assert_(svc is None)
+        svc = self.sched.services.find_srv_by_name_and_hostname("host_windows_http", "http_BUT_NOT_linux")
+        self.assert_(svc is not None)
+
+        # The http_ALL_BUT_NOT_linux should be in the windows host only
+        svc = self.sched.services.find_srv_by_name_and_hostname("host_linux_http", "http_ALL_BUT_NOT_linux")
+        self.assert_(svc is None)
+        svc = self.sched.services.find_srv_by_name_and_hostname("host_windows_http", "http_ALL_BUT_NOT_linux")
+        self.assert_(svc is not None)
+
+        # The http_ALL_BUT_NOT_linux_AND_EVEN_LINUX should be every where :)
+        # yes, it's a stupid example, but at least it help to test :)
+        svc = self.sched.services.find_srv_by_name_and_hostname("host_linux_http", "http_ALL_BUT_NOT_linux_AND_EVEN_LINUX")
+        self.assert_(svc is not None)
+        svc = self.sched.services.find_srv_by_name_and_hostname("host_windows_http", "http_ALL_BUT_NOT_linux_AND_EVEN_LINUX")
+        self.assert_(svc is not None)
+
+
+
 
 
 if __name__ == '__main__':

-- 
UNNAMED PROJECT



More information about the Pkg-nagios-changes mailing list