[icinga-checkins] icinga.org: icinga2/mfriedrich/config: ConfigConvert: fix 4 little big thinkos on import/convert/dump

git at icinga.org git at icinga.org
Mon Apr 8 11:20:15 CEST 2013


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

Author: Michael Friedrich <michael.friedrich at netways.de>
Date:   Wed Apr  3 21:45:06 2013 +0200

ConfigConvert: fix 4 little big thinkos on import/convert/dump

1) do not skip lines containing ;.* - there may actually be a comment on
attr->val line. rather clean the line safely, and let the magic happen.

2) the type counter was implemented in order to have unique object ids
by type, since 1x config does not provide us with such. problem - the
function had a local var, saving the counter for each file, overwriting
the hash attributes later on, starting again with 0. nasty bug, hard to
figure out. fixed by feeding the type_cnt into the cfg_obj hive for now.
revamped the state machine logic as well, like so

I: define TYPE { 	--> __TYPE = TYPE
II: attr val		--> {type}->{type_cnt->cnt}->{attr} = val
III: }			--> type_cnt->cnt++
IV: <empty/comment>

3) also skip service templates (__IS_TEMPLATE) sooner, and do not try to
calculate some host_name/service_description magic out of them - they
just can't go down one level to the host, but only up with 'use'.

4) host and service templates on 2x config dump must check if
__USES_TEMPLATE is actually set to 1, checking for defined is not
enough.

whatthecommit.com:

A long time ago, in a galaxy far far away...

refs #2743

---

 contrib/configconvert/Icinga2/Convert.pm          |   30 ++++++++---------
 contrib/configconvert/Icinga2/DumpIcinga2Cfg.pm   |    4 +-
 contrib/configconvert/Icinga2/ImportIcinga1Cfg.pm |   38 ++++++++++++--------
 3 files changed, 39 insertions(+), 33 deletions(-)

diff --git a/contrib/configconvert/Icinga2/Convert.pm b/contrib/configconvert/Icinga2/Convert.pm
index 04568ce..d74a23a 100644
--- a/contrib/configconvert/Icinga2/Convert.pm
+++ b/contrib/configconvert/Icinga2/Convert.pm
@@ -30,7 +30,7 @@ use File::Find;
 use Storable qw(dclone);
 
 use feature 'say';
-our $dbd_lvl = 1;
+our $dbg_lvl = 1;
 
 sub validate_general_object_2x {
     my $obj_1x = shift;
@@ -369,17 +369,15 @@ sub convert_2x {
 
         # skip non-template objects without a valid hostname
         if (!defined($obj_1x_service->{'__HOSTNAME'}) && $obj_1x_service->{'__IS_TEMPLATE'} == 0) {
-            Icinga2::Utils::debug("Skipping invalid service object without host_name ".Dumper($obj_1x_service));
+            #Icinga2::Utils::debug("Skipping invalid service object without host_name ".Dumper($obj_1x_service));
             # XXX we may look into objects.cache if there?
-            say "Skipping invalid service object without host_name" . Dumper($obj_1x_service);
             next;
         }
 
         # skip non-template objects without a valid hostname
         if (!defined($obj_1x_service->{'__SERVICEDESCRIPTION'}) && $obj_1x_service->{'__IS_TEMPLATE'} == 0) {
-            Icinga2::Utils::debug("Skipping invalid service object without service_description ".Dumper($obj_1x_service));
+            #Icinga2::Utils::debug("Skipping invalid service object without service_description ".Dumper($obj_1x_service));
             # XXX we may look into objects.cache if there?
-            say "Skipping invalid service object without service_description" . Dumper($obj_1x_service);
             next;
         }
 
@@ -423,7 +421,7 @@ sub convert_2x {
 
         # skip non-template objects without a valid hostname
         if (!defined($obj_1x_host->{'__HOSTNAME'}) && $obj_1x_host->{'__IS_TEMPLATE'} == 0) {
-            Icinga2::Utils::debug("Skipping invalid host object without host_name ".Dumper($obj_1x_host));
+            #Icinga2::Utils::debug("Skipping invalid host object without host_name ".Dumper($obj_1x_host));
             next;
         }
         #say Dumper($obj_1x_host);
@@ -478,13 +476,21 @@ sub convert_2x {
             #$obj_1x_service->{'__IS_TEMPLATE'} = obj_1x_is_template($obj_1x_service);
             #$obj_1x_service->{'__USES_TEMPLATE'} = obj_1x_uses_template($obj_1x_service);
 
+            ######################################
+            # skip service templates 
+            ######################################
+            if ($obj_1x_service->{'__IS_TEMPLATE'} == 1) {
+                #Icinga2::Utils::debug("WARNING: Skipping service template '$obj_1x_service->{'__TEMPLATE_NAME'}' for linking to host '$obj_1x_host->{'__HOSTNAME'}'.");
+                next;
+            }
+
             # save it for later
             # XXX if host_name can't be located in the service template tree, check if hostgroup is set somewhere
             # we then need to check if the service -> hostgroup <- hostmember applies (ugly) FIXME
 
-            # XXX if service_description can't be determined, log an error 
+            # XXX if host_name can't be determined, log an error XXX templates MUST be skipped before (they cannot look down, only up in use tree) 
             if (!defined($obj_1x_service_host_name)) {
-                print "ERROR: No host_name for service given " . Dumper($obj_1x_service);
+                #print "ERROR: No host_name for service given " . Dumper($obj_1x_service);
                 next;
             }
 
@@ -498,14 +504,6 @@ sub convert_2x {
                 @$cfg_obj_2x{'host'}->{$host_obj_1x_key}->{'SERVICE'}->{$obj_1x_host_service_cnt} = dclone(@$cfg_obj_1x{'service'}->{$service_obj_1x_key});
 
                 ######################################
-                # skip service templates 
-                ######################################
-                if ($obj_1x_service->{'__IS_TEMPLATE'} == 1) {
-                    Icinga2::Utils::debug("WARNING: Skipping service template '$obj_1x_service->{'__TEMPLATE_NAME'}' for linking to host '$obj_1x_host->{'__HOSTNAME'}'.");
-                    next;
-                }
-
-                ######################################
                 # COMMAND MAGIC
                 ######################################
                 my $service_check_command_2x = Icinga2::Convert::convert_checkcommand(@$cfg_obj_1x{'command'}, $obj_1x_service, $user_macros_1x);
diff --git a/contrib/configconvert/Icinga2/DumpIcinga2Cfg.pm b/contrib/configconvert/Icinga2/DumpIcinga2Cfg.pm
index 0947bd3..c732be3 100644
--- a/contrib/configconvert/Icinga2/DumpIcinga2Cfg.pm
+++ b/contrib/configconvert/Icinga2/DumpIcinga2Cfg.pm
@@ -68,7 +68,7 @@ sub dump_service_2x {
         $service_description = $service_2x->{'__TEMPLATE_NAME'};
     }
 
-    if (defined($service_2x->{'__USES_TEMPLATE'})) {
+    if (defined($service_2x->{'__USES_TEMPLATE'}) && $service_2x->{'__USES_TEMPLATE'} == 1) {
         dump_config_line("$object_type Service \"$service_description\" inherits \"$service_2x->{'use'}\" {");
     } else {
         dump_config_line("$object_type Service \"$service_description\" {");
@@ -116,7 +116,7 @@ sub dump_host_2x {
     }
 
     # if this is a _template_ we need to use 'name' instead of 'host_name'
-    if (defined($host_2x->{'__USES_TEMPLATE'})) { 
+    if (defined($host_2x->{'__USES_TEMPLATE'}) && $host_2x->{'__USES_TEMPLATE'} == 1) { 
         dump_config_line("$object_type Host \"$host_name\" inherits \"$host_2x->{'use'}\" {");
     } else {
         dump_config_line("$object_type Host \"$host_name\" {");
diff --git a/contrib/configconvert/Icinga2/ImportIcinga1Cfg.pm b/contrib/configconvert/Icinga2/ImportIcinga1Cfg.pm
index ce7e355..37e9c89 100644
--- a/contrib/configconvert/Icinga2/ImportIcinga1Cfg.pm
+++ b/contrib/configconvert/Icinga2/ImportIcinga1Cfg.pm
@@ -103,34 +103,37 @@ sub parse_icinga1_object_cfg {
     my $cfg_obj = shift;
     my $file = shift;
 
-    my $type_cnt;
-    
     my $obj = {}; #hashref
     my $in_define = 0;
     my $type;
     my $append; # this is a special case where multiple lines are appended with \ - not sure if we support THAT.
     my $inline_comment;
 
-    my $cnt = 0;
     my $attr;
     my $val;
 
     my @cfg = Icinga2::Utils::slurp($file);
 
+    #Icinga2::Utils::debug("========================================================");
+    #Icinga2::Utils::debug("File: $file");
     foreach my $line (@cfg) {
         $line = Icinga2::Utils::strip($line);
 
+        #Icinga2::Utils::debug("Processing line: '$line'");
+
         # skip comments and empty lines
         next if ($line eq "" || !defined($line) || $line =~ /^\s+$/);
-        next if ($line =~ /^[#;]/ || $line =~ /;.*/);
-        
-        #debug($line);
+        next if ($line =~ /^[#;]/);
+       
+        # || $line =~ /;.*/);
+        $line =~ s/[\r\n\s]+$//;
+        $line =~ s/^\s+//;
 
         # end of def
         if ($line =~ /}(\s*)$/) {
-            $in_define = 0;
+            $in_define = undef;
             # store type for later
-            $cfg_obj->{$type}->{$cnt}->{'__TYPE'} = $type;
+            $cfg_obj->{'type_cnt'}->{$type} = $cfg_obj->{'type_cnt'}->{$type} + 1;
             $type = "";
             next;
         }
@@ -139,10 +142,12 @@ sub parse_icinga1_object_cfg {
             $type = $1;
             $append = $2;
 
+            # save the type
+            $cfg_obj->{$type}->{$cfg_obj->{'type_cnt'}->{$type}}->{'__TYPE'} = $type;
+
             # we're ready to process entries
             $in_define = 1;
             # save the current type counter, being our unique key here
-            $cnt = $type_cnt->{$type} += 1;
             next;
         }
         # in def
@@ -157,13 +162,14 @@ sub parse_icinga1_object_cfg {
             # ignore empty values
             next if (!defined($val));
             next if ($val eq "");
-            #debug("line: '$line'");
-            #debug("type: $type");
-            #debug("attr: $attr");
-            #debug("val: $val");
-            #debug("\n");
+            #Icinga2::Utils::debug("cnt: $cfg_obj->{'type_cnt'}->{$type}");
+            #Icinga2::Utils::debug("line: '$line'");
+            #Icinga2::Utils::debug("type: $type");
+            #Icinga2::Utils::debug("attr: $attr");
+            #Icinga2::Utils::debug("val: $val");
+            #Icinga2::Utils::debug("\n");
 
-            $cfg_obj->{$type}->{$cnt}->{$attr} = $val;
+            $cfg_obj->{$type}->{$cfg_obj->{'type_cnt'}->{$type}}->{$attr} = $val;
 
             # ignore duplicated attributes, last one wins
         }
@@ -173,6 +179,8 @@ sub parse_icinga1_object_cfg {
 
     }
 
+    #Icinga2::Utils::debug("========================================================");
+
     return $cfg_obj;
 
 }





More information about the icinga-checkins mailing list