[icinga-checkins] icinga.org: icinga2/feature/configconvert-2743: configconvert: refactor how services without host_name are handled

git at icinga.org git at icinga.org
Tue Apr 30 19:49:47 CEST 2013


Module: icinga2
Branch: feature/configconvert-2743
Commit: 7446d2833a67429c27bc0344a1a514d3dcf92d8f
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=7446d2833a67429c27bc0344a1a514d3dcf92d8f

Author: Michael Friedrich <michael.friedrich at netways.de>
Date:   Tue Apr 30 19:48:17 2013 +0200

configconvert: refactor how services without host_name are handled

this is mainly the cause when using hostgroup relations, or other object
tricks. we still need the single loop run to convert all 2.x object
attributes in one run, and deal with the missing attributes later on.

---

 contrib/configconvert/Icinga2/Convert.pm |   37 +++++++++++++++--------------
 1 files changed, 19 insertions(+), 18 deletions(-)

diff --git a/contrib/configconvert/Icinga2/Convert.pm b/contrib/configconvert/Icinga2/Convert.pm
index ccaf251..58cd869 100644
--- a/contrib/configconvert/Icinga2/Convert.pm
+++ b/contrib/configconvert/Icinga2/Convert.pm
@@ -336,8 +336,8 @@ sub obj_1x_get_service_host_name_arr {
     return undef if !defined($obj_1x);
 
     # first, check if we already got a host_name here in our struct (recursion safety)
-    return $obj_1x->{'__I2CONVERT_HOSTNAME'} if defined($obj_1x->{'__I2CONVERT_HOSTNAME'});
-    delete $obj_1x->{'__I2CONVERT_HOSTNAME'};
+    return $obj_1x->{'__I2CONVERT_HOSTNAMES'} if defined($obj_1x->{'__I2CONVERT_HOSTNAMES'});
+    delete $obj_1x->{'__I2CONVERT_HOSTNAMES'};
 
     # if this object got what we want, return (it can be recursion and a template!)
     if(defined($obj_1x->{'host_name'})) {
@@ -345,11 +345,11 @@ sub obj_1x_get_service_host_name_arr {
         #print "DEBUG: found $obj_1x->{'host_name'}\n";
 
         # convert to array
-        delete($obj_1x->{'__I2CONVERT_HOSTNAME'});
+        delete($obj_1x->{'__I2CONVERT_HOSTNAMES'});
         @host_name = sort (map { s/^\s+//; s/\s+$//; $_ } split (/,/, $obj_1x->{'host_name'}) );
-        push @{$obj_1x->{'__I2CONVERT_HOSTNAME'}}, @host_name;
+        push @{$obj_1x->{'__I2CONVERT_HOSTNAMES'}}, @host_name;
 
-        #print "DEBUG: @{$obj_1x->{'__I2CONVERT_HOSTNAME'}}"; 
+        #print "DEBUG: @{$obj_1x->{'__I2CONVERT_HOSTNAMES'}}"; 
         return @host_name;
     }
 
@@ -374,8 +374,8 @@ sub obj_1x_get_service_host_name_arr {
 
             # get the host_name and return - first template wins
             # convert to array
-            delete($obj_1x->{'__I2CONVERT_HOSTNAME'});
-            push @{$obj_1x->{'__I2CONVERT_HOSTNAME'}}, @host_name;
+            delete($obj_1x->{'__I2CONVERT_HOSTNAMES'});
+            push @{$obj_1x->{'__I2CONVERT_HOSTNAMES'}}, @host_name;
             return @host_name;
         }
     }
@@ -599,23 +599,22 @@ sub convert_2x {
        
         #print "DEBUG: service @host_names\n";
 
-        delete($obj_1x_service->{'__I2CONVERT_HOSTNAME'});
-        push @{$obj_1x_service->{'__I2CONVERT_HOSTNAME'}}, @host_names;
-
-
-        # skip non-template objects without valid hostnames - we'll deal later with hostgroup magic
-        if (!defined($obj_1x_service->{'__I2CONVERT_HOSTNAME'}) && $obj_1x_service->{'__I2CONVERT_IS_TEMPLATE'} == 0) {
-            #Icinga2::Utils::debug("Skipping invalid service object without host_name ".Dumper($obj_1x_service));
-            next;
+        delete($obj_1x_service->{'__I2CONVERT_HOSTNAMES'});
+        if(@host_names == 0) {
+            # set a dummy value for postprocessing - we need the prepared 2.x service for later object tricks
+            push @host_names, "__I2CONVERT_DUMMY";
         }
+        push @{$obj_1x_service->{'__I2CONVERT_HOSTNAMES'}}, @host_names;
+
 
         # if there is more than one host_name involved on the service object, clone it in a loop
-        foreach my $service_host_name (@{$obj_1x_service->{'__I2CONVERT_HOSTNAME'}}) {
+        foreach my $service_host_name (@{$obj_1x_service->{'__I2CONVERT_HOSTNAMES'}}) {
 
             # we can only look up services with their uniqueness to the host_name
             $obj_1x_service->{'__I2CONVERT_SERVICEDESCRIPTION'} = obj_1x_get_service_service_description($cfg_obj_1x, $obj_1x_service, $service_host_name);
 
             # skip non-template objects without a valid service description (we cannot tolerate 'name' here!)
+            # XXX find a better way - we actually need all services in the list, even if __I2CONVERT_SERVICEDESCRIPTION is undef
             if (!defined($obj_1x_service->{'__I2CONVERT_SERVICEDESCRIPTION'}) && $obj_1x_service->{'__I2CONVERT_IS_TEMPLATE'} == 0) {
                 #Icinga2::Utils::debug("Skipping invalid service object without service_description ".Dumper($obj_1x_service));
                 next;
@@ -626,8 +625,10 @@ sub convert_2x {
             ####################################################
             $cfg_obj_2x->{'service'}->{$service_cnt} = dclone(@$cfg_obj_1x{'service'}->{$service_obj_1x_key});
 
-            # immediately overwrite the correct host_name
-            $cfg_obj_2x->{'service'}->{$service_cnt}->{__I2CONVERT_SERVICE_HOSTNAME} = $service_host_name;
+            # immediately overwrite the correct host_name - if there's no dummy value set for further processing
+            if ($service_host_name !~ /__I2CONVERT_DUMMY/) {
+                $cfg_obj_2x->{'service'}->{$service_cnt}->{__I2CONVERT_SERVICE_HOSTNAME} = $service_host_name;
+            }
 
             #say Dumper($cfg_obj_2x->{'service'}->{$service_cnt});
             ####################################################





More information about the icinga-checkins mailing list