[icinga-checkins] icinga.org: icinga2/mfriedrich/config: ConfigConvert: refactor host_name/ service_description link resolving

git at icinga.org git at icinga.org
Wed Apr 3 19:10:50 CEST 2013


Module: icinga2
Branch: mfriedrich/config
Commit: d744666a98b4876efe2db2845c53fe24471a94d0
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=d744666a98b4876efe2db2845c53fe24471a94d0

Author: Michael Friedrich <michael.friedrich at netways.de>
Date:   Wed Apr  3 17:01:11 2013 +0200

ConfigConvert: refactor host_name/service_description link resolving

previous algorithm was buggy, as well get_host_name always looked up
host template objects, even if the template object was a service. this
is now fixed by re-using the __TYPE attribute (set on Icinga 1x config
read).

the algorithm is now like

                       |---------------------------------|
                      \/                                 |
                   svc_desc?                             |
                0 /        \ 1                           |
              use tmpl?    END -------> [svc_desc]       |
             0 /   \ 1                                   |
[undef] <- BROKEN  getobjbyattr(['use'], __TYPE, svcdesc |
                    |                                    |
                 getsvcdesc(obj)                         |
                    |                                    |
                    --------------------------------------

refs #2743

---

 contrib/configconvert/Icinga2/Convert.pm |  113 +++++++++++++-----------------
 1 files changed, 49 insertions(+), 64 deletions(-)

diff --git a/contrib/configconvert/Icinga2/Convert.pm b/contrib/configconvert/Icinga2/Convert.pm
index 2e0d4db..eea0839 100644
--- a/contrib/configconvert/Icinga2/Convert.pm
+++ b/contrib/configconvert/Icinga2/Convert.pm
@@ -265,42 +265,35 @@ sub obj_1x_get_host_name {
     return $obj_1x->{'__HOSTNAME'} if defined($obj_1x->{'__HOSTNAME'});
     delete $obj_1x->{'__HOSTNAME'};
 
-    # obj does not have a hostname, check if templates used
-    if (!defined($obj_1x->{'host_name'})) {
-        if(defined($obj_1x->{'name'})) {
-            # we get a name here. decide if this is a full object or just a template
-            if(obj_1x_is_template($obj_1x) == 0) {
-                #debug("Found host with name (deprecated): $obj_1x->{'name'}");
-                $obj_1x->{'__HOSTNAME'} = $host_name = $obj_1x->{'name'};
-                return $obj_1x->{'__HOSTNAME'};
-            }
-        }
-        if(defined($obj_1x->{'use'})) {
-            #debug("trying use $obj_1x->{'use'} template");
-            # XXX FIXME allow pattern array to be passed to function to reduce looping
-            my $obj_1x_tmpl = obj_1x_get_obj_by_attr_val($objs_1x, 'host', 'host_name',  $obj_1x->{'use'});
-            # fallback for the name only
-            if (!defined($obj_1x_tmpl)) {
-                $obj_1x_tmpl = obj_1x_get_obj_by_attr_val($objs_1x, 'host', 'name',  $obj_1x->{'use'});
-            }
-            #debug("Template: ".Dumper($obj_1x_tmpl));
-            $host_name = obj_1x_get_host_name($objs_1x,$obj_1x_tmpl);
-            return undef if(!defined($host_name));
-            $obj_1x->{'__HOSTNAME'} = $host_name;
-            #debug("found host_name $host_name");
-        }
-        else {
-            # no templates and no host_name looks like a broken object XXX
-            #debug("no host_name and no use found in ".Dumper($obj_1x));
-        }
+    # if this object got what we want, return (it can be recursion and a template!)
+    if(defined($obj_1x->{'host_name'})) {
+        $obj_1x->{'__HOSTNAME'} = $obj_1x->{'host_name'};
+        return $obj_1x->{'__HOSTNAME'};
     }
-    # we actually have it
+
+    
+    # we don't have a host name, should we look into a template?
+    # make sure _not_ to use 
+    if (defined($obj_1x->{'__USES_TEMPLATE'}) && $obj_1x->{'__USES_TEMPLATE'} == 1) {
+        # get the object referenced as template
+        my $obj_1x_tmpl = obj_1x_get_obj_by_attr_val($objs_1x, $obj_1x->{'__TYPE'}, 'host_name',  $obj_1x->{'__TEMPLATE_NAME'});
+
+        # now recurse into ourselves and look for a possible service_description
+        $host_name = obj_1x_get_host_name($objs_1x,$obj_1x_tmpl);
+        # bail here if search did not unveil anything
+        return undef if(!defined($host_name));
+
+        # get the host_name and return
+        $obj_1x->{'__HOSTNAME'} = $host_name;
+        return $obj_1x->{'__HOSTNAME'};
+    }
+    # no template used, and no host name - broken object, ignore it
     else {
-        #debug("Found host with host_name: $obj_1x->{'host_name'}");
-        $obj_1x->{'__HOSTNAME'} = $host_name = $obj_1x->{'host_name'};
+        return undef;
     }
 
-    return $obj_1x->{'__HOSTNAME'};
+    # we should never hit here
+    return undef;
 }
 
 sub obj_1x_get_service_description {
@@ -315,42 +308,34 @@ sub obj_1x_get_service_description {
     return $obj_1x->{'__SERVICEDESCRIPTION'} if defined($obj_1x->{'__SERVICEDESCRIPTION'});
     delete $obj_1x->{'__SERVICEDESCRIPTION'};
 
-    # obj does not have a service_description, check if templates used
-    if (!defined($obj_1x->{'service_description'})) {
-        if(defined($obj_1x->{'name'})) {
-            # we get a name here. decide if this is a full object or just a template
-            if(obj_1x_is_template($obj_1x) == 0) {
-                #debug("Found service with name (deprecated): $obj_1x->{'name'}");
-                $obj_1x->{'__SERVICEDESCRIPTION'} = $service_description = $obj_1x->{'name'};
-                return $obj_1x->{'__SERVICEDESCRIPTION'};
-            }
-        }
-        if(defined($obj_1x->{'use'})) {
-            #debug("trying use $obj_1x->{'use'} template");
-            # XXX FIXME allow pattern array to be passed to function to reduce looping
-            my $obj_1x_tmpl = obj_1x_get_obj_by_attr_val($objs_1x, 'service', 'service_description',  $obj_1x->{'use'});
-            # fallback for the name only
-            if (!defined($obj_1x_tmpl)) {
-                $obj_1x_tmpl = obj_1x_get_obj_by_attr_val($objs_1x, 'service', 'name',  $obj_1x->{'use'});
-            }
-            #debug("Template: ".Dumper($obj_1x_tmpl));
-            $service_description = obj_1x_get_service_description($objs_1x,$obj_1x_tmpl);
-            return undef if(!defined($service_description));
-            $obj_1x->{'__SERVICEDESCRIPTION'} = $service_description;
-            #debug("found service_description $service_description");
-        }
-        else {
-            # no templates and no service_description looks like a broken object XXX
-            #debug("no service_description and no use found in ".Dumper($obj_1x));
-        }
+    # if this object got what we want, return (it can be recursion and a template!)
+    if(defined($obj_1x->{'service_description'})) {
+        $obj_1x->{'__SERVICEDESCRIPTION'} = $obj_1x->{'service_description'};
+        return $obj_1x->{'__SERVICEDESCRIPTION'};
     }
-    # we actually have it
+
+    # we don't have a service description, should we look into a template?
+    # make sure _not_ to use 
+    if (defined($obj_1x->{'__USES_TEMPLATE'}) && $obj_1x->{'__USES_TEMPLATE'} == 1) {
+        # get the object referenced as template
+        my $obj_1x_tmpl = obj_1x_get_obj_by_attr_val($objs_1x, 'service', 'service_description',  $obj_1x->{'__TEMPLATE_NAME'});
+
+        # now recurse into ourselves and look for a possible service_description
+        $service_description = obj_1x_get_service_description($objs_1x,$obj_1x_tmpl);
+        # bail here if search did not unveil anything
+        return undef if(!defined($service_description));
+
+        # get the service description and return
+        $obj_1x->{'__SERVICEDESCRIPTION'} = $service_description;
+        return $obj_1x->{'__SERVICEDESCRIPTION'};
+    }
+    # no template used, and not service description - broken object, ignore it
     else {
-        #debug("Found service with service_description: $obj_1x->{'service_description'}");
-        $obj_1x->{'__SERVICEDESCRIPTION'} = $service_description = $obj_1x->{'service_description'};
+        return undef;
     }
 
-    return $obj_1x->{'__SERVICEDESCRIPTION'};
+    # we should never hit here
+    return undef;
 }
 
 





More information about the icinga-checkins mailing list