[icinga-checkins] icinga.org: icinga2/feature/configconvert-2743: configconvert: refactor service-hg-hosts for one hosthg template and multiple services

git at icinga.org git at icinga.org
Tue Apr 30 18:48:46 CEST 2013


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

Author: Michael Friedrich <michael.friedrich at netways.de>
Date:   Tue Apr 30 18:31:28 2013 +0200

configconvert: refactor service-hg-hosts for one hosthg template and multiple services

we need to first loop over all services, getting the key ids, collecting
them for each hostgroup_name (this is the unique identifier, because
this is our hosthg template later on).

then we loop over the collected unique hostgroups and build the 2.x
template logic. the difference here is now, that the code is refactored,
and loops over the service keys (getting their objects) only once,
pushing their templates plus a relation to a single (1) hosthg-template.

there's still some space for enhancements - the service template names
as well as hosthg template names as well as service object names are
generated with some hardcoded strings/numbers.

refs #2743
refs #4016

---

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

diff --git a/contrib/configconvert/Icinga2/Convert.pm b/contrib/configconvert/Icinga2/Convert.pm
index 9d13321..7c2795a 100644
--- a/contrib/configconvert/Icinga2/Convert.pm
+++ b/contrib/configconvert/Icinga2/Convert.pm
@@ -308,6 +308,8 @@ sub obj_get_hostnames_obj_by_hostgroup_name {
     return @host_names;
 }
 
+
+
 # get host_name from object
 sub obj_1x_get_host_host_name {
     my $objs_1x = shift;
@@ -1251,6 +1253,8 @@ sub convert_2x {
     my $obj_2x_hosts_cnt = (reverse sort {$a <=> $b} (keys %{@$cfg_obj_2x{'host'}}))[0];
     #print "FOO: $obj_2x_hosts_cnt\n";
 
+    my $obj_2x_services_hg = {};
+
     foreach my $service_obj_2x_key (keys %{@$cfg_obj_2x{'service'}}) {
 
         my $obj_2x_service = @$cfg_obj_2x{'service'}->{$service_obj_2x_key};
@@ -1261,13 +1265,25 @@ sub convert_2x {
         # skip all services without hostgroup_name
         next if(!defined($obj_2x_service->{'hostgroup_name'}));
 
-        #if ($obj_2x_service->{'service_description'} =~/2743/) {
-        #    say Dumper($obj_2x_service);
-        #}
-        
-        #print "DEBUG: now checking $obj_2x_service->{'service_description'}...\n";
+        # we need to save all services first, but our new key is the hostgroupname
+        # so that we can create multiple services for a single hosthg template later on
+#        $obj_2x_services_hg->{'hostgroup_name'}
+        push @{%$obj_2x_services_hg->{$obj_2x_service->{'hostgroup_name'}}}, $service_obj_2x_key;
+
+    }
+
+    # now loop over all hostgroups with service relations
+    foreach my $service_hg_obj_2x_key (keys %{$obj_2x_services_hg}) {
+
+        say Dumper($obj_2x_services_hg);
 
-        my $obj_2x_hostgroup = obj_get_hostgroup_obj_by_hostgroup_name($cfg_obj_2x, $obj_2x_service->{'hostgroup_name'});
+        # get the stored unique key to our services
+        my $hg_name = $service_hg_obj_2x_key;
+        my @service_keys = @{$obj_2x_services_hg->{$hg_name}};
+
+        print "DEBUG: Looking for $hg_name ...\n";
+
+        my $obj_2x_hostgroup = obj_get_hostgroup_obj_by_hostgroup_name($cfg_obj_2x, $hg_name);
 
         if(!defined($obj_2x_hostgroup)) {
             # no hostgroup defined?
@@ -1275,42 +1291,52 @@ sub convert_2x {
 
         # we now need all host names for this hostgroup name, as an array
 
-        my @service_hostgroup_hostnames = obj_get_hostnames_obj_by_hostgroup_name($cfg_obj_2x, $obj_2x_service->{'hostgroup_name'});
+        my @service_hostgroup_hostnames = obj_get_hostnames_obj_by_hostgroup_name($cfg_obj_2x, $hg_name);
 
         if(@service_hostgroup_hostnames == 0) {
             # no members, so service cannot be linked. log a warning XXX
-            print " DEBUG: no members found, skipping $obj_2x_service->{'hostgroup_name'}\n";
+            print " DEBUG: no members found, skipping $hg_name\n";
             next;
         }
         
         # we've got:
-        # * a service, 
+        # * n services linked to hostgroups, 
         # * a hostgroup 
         # * an array of hosts as hostgroup members
         # we'll create: 
-        # * a service template, 
-        # * a hg-host template referencing the service template, 
+        # * n service templates, 
+        # * a hg-host template referencing the service templates, 
         # * host objects inheriting from it
         #
         my $svc_count = 0;
 
-        # set the service as template.
-        $obj_2x_service->{'__I2CONVERT_IS_TEMPLATE'} = 1;
-        $obj_2x_service->{'__I2CONVERT_TEMPLATE_NAME'} = $obj_2x_service->{'service_description'}."-group"; # XXX hardcode it for now
-        
-        # create a dummy service inheriting the service template
-        my $obj_2x_service_inherit;
-        $obj_2x_service_inherit->{__I2CONVERT_USES_TEMPLATE} = 1;
-        push @{$obj_2x_service_inherit->{'__I2CONVERT_TEMPLATE_NAMES'}}, $obj_2x_service->{'__I2CONVERT_TEMPLATE_NAME'};
-        $obj_2x_service_inherit->{'service_description'} = $obj_2x_service->{'service_description'};
-
         # create a host template with hgname-group
         my $obj_2x_host_template;
         $obj_2x_host_template->{'__I2CONVERT_IS_TEMPLATE'} = 1;
         $obj_2x_host_template->{'__I2CONVERT_TEMPLATE_NAME'} = $obj_2x_hostgroup->{'hostgroup_name'}."-group"; # XXX hardcode it for now
 
-        # link the service inherit to the host template
-        $obj_2x_host_template->{'SERVICE'}->{$svc_count} = $obj_2x_service_inherit;
+        # loop through all services and attach them to the host template
+        foreach my $service_obj_2x_key_val (@service_keys) {
+
+            print "DEBUG: Working on $service_obj_2x_key_val ...\n";
+            # get the service object by key
+            my $obj_2x_service = @$cfg_obj_2x{'service'}->{$service_obj_2x_key_val};
+
+            # set the service as template.
+            $obj_2x_service->{'__I2CONVERT_IS_TEMPLATE'} = 1;
+            $obj_2x_service->{'__I2CONVERT_TEMPLATE_NAME'} = $obj_2x_service->{'service_description'}."-group-".$svc_count; # XXX hardcode it for now
+        
+            # create a dummy service inheriting the service template
+            my $obj_2x_service_inherit;
+            $obj_2x_service_inherit->{__I2CONVERT_USES_TEMPLATE} = 1;
+            push @{$obj_2x_service_inherit->{'__I2CONVERT_TEMPLATE_NAMES'}}, $obj_2x_service->{'__I2CONVERT_TEMPLATE_NAME'};
+            $obj_2x_service_inherit->{'service_description'} = $obj_2x_service->{'service_description'};
+
+            # link the service inherit to the host template
+            $obj_2x_host_template->{'SERVICE'}->{$svc_count} = $obj_2x_service_inherit;
+
+            $svc_count++;
+        }
 
         # all host objects on the hostgroup members will get the host hg template name pushed into their array
         foreach my $hostgroup_member_host_name (@service_hostgroup_hostnames) {





More information about the icinga-checkins mailing list