[icinga-checkins] icinga.org: icinga2/feature/configconvert-2743: configconvert: add service- >hostgroup<-hostmembers conversion to template support

git at icinga.org git at icinga.org
Thu May 2 22:17:32 CEST 2013


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

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

configconvert: add service->hostgroup<-hostmembers conversion to template support

WIP: there's multiple service->hostgroup relations - duplicated hosthg
templates. must be fixed.

refs #2743
refs #4016

---

 contrib/configconvert/Icinga2/Convert.pm |  149 ++++++++++++++++++++++++++++++
 contrib/configconvert/Icinga2/Utils.pm   |   17 ++++
 2 files changed, 166 insertions(+), 0 deletions(-)

diff --git a/contrib/configconvert/Icinga2/Convert.pm b/contrib/configconvert/Icinga2/Convert.pm
index 198440c..9d13321 100644
--- a/contrib/configconvert/Icinga2/Convert.pm
+++ b/contrib/configconvert/Icinga2/Convert.pm
@@ -28,6 +28,7 @@ use strict;
 use Data::Dumper;
 use File::Find;
 use Storable qw(dclone);
+use List::Util 'max';
 
 use feature 'say';
 our $dbg_lvl = 1;
@@ -252,10 +253,61 @@ sub obj_get_tmpl_obj_by_tmpl_name {
 }
 
 
+# get hostgroup object by attr 'hostgroup_name'
+sub obj_get_hostgroup_obj_by_hostgroup_name {
+    my $objs = shift;
+    my $obj_type = 'hostgroup';
+    my $obj_attr = 'hostgroup_name';
+    my $obj_val = shift;
+
+    #debug("My objects hive: ".Dumper($objs));
+
+    #debug("Checking for type=$obj_type attr=$obj_attr val=$obj_val ");
+    foreach my $obj_key (keys %{@$objs{$obj_type}}) {
+        my $obj = @$objs{$obj_type}->{$obj_key};
+        next if !defined($obj->{$obj_attr});
+        #debug("Getting attr $obj_attr and val $obj_val");
+        if ($obj->{$obj_attr} eq $obj_val) {
+            #debug("Found object: ".Dumper($obj));
+            return $obj;
+        }
+    }
+
+    return undef;
+}
+
 #################################################################################
 # Get Object Attribute Helpers 
 #################################################################################
 
+
+# get host_names by attr 'hostgroup_name'
+sub obj_get_hostnames_obj_by_hostgroup_name {
+    my $objs = shift;
+    my $obj_type = 'host';
+    my $obj_attr = 'hostgroups';
+    my $obj_val = shift;
+    my @host_names = ();
+
+    #debug("My objects hive: ".Dumper($objs));
+
+    #debug("Checking for type=$obj_type attr=$obj_attr val=$obj_val ");
+    foreach my $obj_key (keys %{@$objs{$obj_type}}) {
+        my $obj = @$objs{$obj_type}->{$obj_key};
+        next if !defined($obj->{$obj_attr});
+        #debug("Getting attr $obj_attr and val $obj_val");
+
+        foreach my $hg (@{$obj->{$obj_attr}}) {
+            if ($hg eq $obj_val) {
+                #debug("Found object: ".Dumper($obj));
+                push @host_names, $obj->{'host_name'};
+            }
+        }
+    }
+
+    return @host_names;
+}
+
 # get host_name from object
 sub obj_1x_get_host_host_name {
     my $objs_1x = shift;
@@ -1186,6 +1238,103 @@ sub convert_2x {
         }
     }
 
+    ######################################
+    # SERVICE->HG<-HOSTMEMBERS MAGIC
+    # we've skipped services without
+    # host_name before, now deal with them
+    # hostgroups have been prepared with
+    # all their members too (!!)
+    # we're working on 2.x objects now
+    ######################################
+
+    # get the max key for hosts (required for adding more)
+    my $obj_2x_hosts_cnt = (reverse sort {$a <=> $b} (keys %{@$cfg_obj_2x{'host'}}))[0];
+    #print "FOO: $obj_2x_hosts_cnt\n";
+
+    foreach my $service_obj_2x_key (keys %{@$cfg_obj_2x{'service'}}) {
+
+        my $obj_2x_service = @$cfg_obj_2x{'service'}->{$service_obj_2x_key};
+
+        #print "DEBUG: now checking $obj_2x_service->{'service_description'}...\n";
+        # skip all services which already got a host_name? which one wins here? XXX
+
+        # 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";
+
+        my $obj_2x_hostgroup = obj_get_hostgroup_obj_by_hostgroup_name($cfg_obj_2x, $obj_2x_service->{'hostgroup_name'});
+
+        if(!defined($obj_2x_hostgroup)) {
+            # no hostgroup defined?
+        }
+
+        # 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'});
+
+        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";
+            next;
+        }
+        
+        # we've got:
+        # * a service, 
+        # * a hostgroup 
+        # * an array of hosts as hostgroup members
+        # we'll create: 
+        # * a service template, 
+        # * a hg-host template referencing the service template, 
+        # * 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;
+
+        # 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) {
+            # get the host obj
+            my $obj_2x_host = obj_get_host_obj_by_host_name($cfg_obj_2x, $hostgroup_member_host_name); # this is a reference in memory, not a copy!
+            
+            # push the template used
+            # (override __I2CONVERT_USES_TEMPLATE too)
+            $obj_2x_host->{__I2CONVERT_USES_TEMPLATE} = 1;
+            push @{$obj_2x_host->{'__I2CONVERT_TEMPLATE_NAMES'}}, $obj_2x_host_template->{'__I2CONVERT_TEMPLATE_NAME'};
+
+        }
+
+        # push back the newly created host template (incl the service inherit below SERVICE) to the objects 2.x hive
+        #say Dumper($obj_2x_host_template);
+
+        $obj_2x_hosts_cnt++;
+        print "adding new host at key " . $obj_2x_hosts_cnt . "\n";
+        $cfg_obj_2x->{'host'}->{$obj_2x_hosts_cnt} = $obj_2x_host_template;
+
+    }
+
+    #exit(0);
+
     ############################################################################
     ############################################################################
 
diff --git a/contrib/configconvert/Icinga2/Utils.pm b/contrib/configconvert/Icinga2/Utils.pm
index 7d9b437..b8839ae 100644
--- a/contrib/configconvert/Icinga2/Utils.pm
+++ b/contrib/configconvert/Icinga2/Utils.pm
@@ -86,6 +86,23 @@ sub slurp {
     }
 }
 
+# inspired by http://stackoverflow.com/questions/2886872/what-is-the-easiest-way-to-get-a-key-with-the-highest-value-from-a-hash-in-perl
+sub hash_largest_key {
+    my $hash = shift;
+
+    my ($key, @keys) = keys %$hash;
+    my ($big, @vals) = values %$hash;
+
+    for (0 .. $#keys) {
+        if ($vals[$_] > $big) {
+            $big = $vals[$_];
+            $key = $keys[$_];
+        }
+    }
+
+    return $key;
+}
+
 
 1;
 





More information about the icinga-checkins mailing list