[icinga-checkins] icinga.org: icinga2-migration/master: Lookup host_name and hostgroup_name in service templates generating relationships

git at icinga.org git at icinga.org
Tue Jun 10 23:10:31 CEST 2014


Module: icinga2-migration
Branch: master
Commit: b3ca90822a40122766317fadbcb5d801c003b1fb
URL:    https://git.icinga.org/?p=icinga2-migration.git;a=commit;h=b3ca90822a40122766317fadbcb5d801c003b1fb

Author: Michael Friedrich <michael.friedrich at gmail.com>
Date:   Tue Jun 10 20:57:50 2014 +0200

Lookup host_name and hostgroup_name in service templates generating relationships

This is mandatory for assigning services to hosts and hostgroups in the
first place. Icinga 2 cannot later detect whether this attribute is
hidden in a template or not, but requires the direct service->host and
service->hostgroup relation.

Therefore the attributes need to be looked up in the parent objects
which could cost some additional cpu time due to recursive lookup but
still paves the way for the notifications and contact* attribute search
too.

---

 .../library/Conftool/Icinga/IcingaConfig.php       |   62 +++++++++++++++++---
 .../Conftool/Icinga2/Icinga2ObjectDefinition.php   |    3 +-
 2 files changed, 55 insertions(+), 10 deletions(-)

diff --git a/modules/conftool/library/Conftool/Icinga/IcingaConfig.php b/modules/conftool/library/Conftool/Icinga/IcingaConfig.php
index 92fe296..dba261a 100644
--- a/modules/conftool/library/Conftool/Icinga/IcingaConfig.php
+++ b/modules/conftool/library/Conftool/Icinga/IcingaConfig.php
@@ -297,9 +297,9 @@ class IcingaConfig
             foreach ($uses as $use) {
                 if (! array_key_exists($use, $this->templates)) {
                     //there may still be an object used instead
-                    print("Template does not exist. Trying a real object.\n");
+                    print("//Template does not exist. Trying a real object.\n");
                     if (! array_key_exists($use, $this->allDefinitions)) {
-                        print("ERROR: Template '".$use."' does not exist. Fix your configuration.\n");
+                        print("//ERROR: Template '".$use."' does not exist. Fix your configuration.\n");
                         continue;
                         //throw new IcingaDefinitionException(
                         //   sprintf('Object inherits from unknown template "%s"', $use) . print_r($definition)
@@ -308,8 +308,8 @@ class IcingaConfig
                 }
                 $definition->addParent($this->templates[$use]);
             }
-            print_r("object templates:\n");
-            var_dump($definition->getParents());
+            //print_r("object templates:\n");
+            //var_dump($definition->getParents());
         }
     }
 
@@ -375,6 +375,7 @@ class IcingaConfig
         }
     }
 
+
     //TODO this only works if the object has 'host_name' or 'hostgroup_name'
     //but not if that attribute is hidden in the template tree
     protected function resolveService(IcingaService $service)
@@ -391,13 +392,36 @@ class IcingaConfig
                ? $this->splitComma($service->host_name)
                : array();
 
-        if (empty($hosts) && empty($hostgroups)) {
-            $service_tmpl = $service->getParents();
+        //check if there are attributes hidden in the template tree
+        //we need to directly assign host_name and hostgroup_name here
+        //v2 cannot easily resolve these attributes
+        if (empty($hosts)) {
+            //print("parents: ".$service."\n");
+            //var_dump($service_tmpl); //FIXME lookup the attributes in the template tree?
+            $hosts = $this->getObjectAttributeRecursive($service, 'host_name');
+            if (! $hosts) {
+                $hosts = array();
+            } else {
+                print_r("Found host_name attribute in template tree: ".$hosts);
+                $service->_hosts = $hosts; //store them for later
+            }
+            //var_dump($hosts);
+        }
+        if (empty($hostgroups)) {
+            $hostgroups = $this->getObjectAttributeRecursive($service, 'hostgroup_name');
+            if (! $hostgroups) {
+                $hostgroups = array();
+            } else {
+                $service->_hostgroups = $hostgroups; //store them for later
+            }
+            //var_dump($hostgroups);
+        }
 
-            print("parents: ".$service."\n");
-            var_dump($service_tmpl); //FIXME lookup the attributes in the template tree?
-            return;
+        if (empty($hosts) && empty($hostgroups) && !$service->isTemplate()) {
+            print("Could not find any host or hostgroup_name attribute. Skipping invalid object.");
+            return;          
         }
+
         if (empty($hosts) && empty($hostgroups) && $service->isTemplate()) {
             return;
         }
@@ -407,6 +431,8 @@ class IcingaConfig
             if (isset($this->definitions['host'][$host])) {
                 $assigned = true;
                 if (! $this->definitions['host'][$host]->hasService($service)) {
+                    //force relation (could be overridden from template tree)
+                    $service->host_name = (string) $host;
                     $this->definitions['host'][$host]->addService($service);
                 }
             } elseif (substr($host, 0, 1) === '!' && isset($this->definitions['host'][substr($host, 1)])) {
@@ -447,6 +473,24 @@ class IcingaConfig
         }
     }
 
+    //required for host_name, hostgroup_name, contact{,_group}s lookups
+    protected function getObjectAttributeRecursive($object, $attr)
+    {
+        if ($object->$attr) {
+            return $object->$attr;
+        }
+        
+        $templates = $object->getParents();
+
+        foreach ($templates as $template) {
+            if (!$template->$attr) {
+                $this->getObjectAttributeRecursive($template, $attr);
+            } else {
+                return $template->$attr;
+            }
+        }
+    }
+
     protected function addTemplate(IcingaTemplate $template)
     {
         $this->addDefinition($template);
diff --git a/modules/conftool/library/Conftool/Icinga2/Icinga2ObjectDefinition.php b/modules/conftool/library/Conftool/Icinga2/Icinga2ObjectDefinition.php
index 85336fd..c1b47b0 100644
--- a/modules/conftool/library/Conftool/Icinga2/Icinga2ObjectDefinition.php
+++ b/modules/conftool/library/Conftool/Icinga2/Icinga2ObjectDefinition.php
@@ -72,12 +72,13 @@ class Icinga2ObjectDefinition
             }
 
             //ugly 1.x hacks
+	    //these values must be resolved earlier already
             if($this->is_template && ($key == "service_description" || $key == "host_name")) {
                 continue; //skip invalid template attributes
             }
             if (!$this->is_template && $key == "name") {
                 continue; //skip invalid object attributes
-            }
+	    }
 
             // template imports
             if ($key == "use") {



More information about the icinga-checkins mailing list