]> granicus.if.org Git - icinga2/commitdiff
configconvert: custom attr (CVs, *_url, notes, *image*, 2d_coords)
authorMichael Friedrich <michael.friedrich@netways.de>
Wed, 26 Jun 2013 14:01:22 +0000 (16:01 +0200)
committerMichael Friedrich <michael.friedrich@netways.de>
Wed, 26 Jun 2013 14:01:22 +0000 (16:01 +0200)
collect attributes
* notes
* icon_image
* icon_image_alt
* action_url
* notes_url
* statusmap_image
* 2d_coords
* _CVs
* !__I2CONVERT

and stash it onto the custom dictionary.

note: attribute names starting with a number need to be quoted.

fixes #3096

tools/configconvert/Icinga2/Convert.pm
tools/configconvert/Icinga2/ExportIcinga2Cfg.pm

index 0b81d602c931400f4a5fa9d64303eb56a29a1b85..a767144f84c6088105235f07a41e33995920a2d7 100644 (file)
@@ -715,6 +715,23 @@ sub obj_2x_get_service_servicegroups {
 # Conversion
 #################################################################################
 
+# convert CVs, *_url, etc into custom hash
+sub obj_convert_custom_attr_to_custom_hash {
+    my $obj = shift;
+    my $custom;
+
+    foreach my $key (keys %{$obj}) {
+        if ($key =~ /^_|_url$|notes|image|coords/ && $key !~ /__I2CONVERT/) {
+            my $new_key = ($key =~ /^\d/) ? "\"$key\"" : $key;
+            $custom->{$new_key} = Icinga2::Utils::escape_str($obj->{$key});
+        }
+    }
+
+    $obj->{'__I2CONVERT_CUSTOM_ATTR'} = $custom;
+
+    return $custom;
+}
+
 # convert notification_options to state|type_filter
 sub convert_notification_options_to_filter {
     my $notification_options = shift;
@@ -978,6 +995,11 @@ sub convert_2x {
             }
         }
 
+        ####################################################
+        # migrate custom attributes
+        ####################################################
+        my $custom = obj_convert_custom_attr_to_custom_hash($obj_1x_service);
+
         ####################################################
         # get related host_name/service_description
         # used later in host->service resolval
@@ -1025,41 +1047,28 @@ sub convert_2x {
             # same:
             # - display_name
             # - max_check_attempts
+            # - check_period
+            # - notification_period
+            # custom:
+            # - _CV
             # - action_url
             # - notes_url
             # - notes
             # - icon_image
-            # - notes
             # change:
             # - servicegroups (commaseperated strings to array)
             # - check_command
             # - check_interval (X min -> Xm) + normal_check_interval
             # - retry_interval (X min -> Xm) + retry_check_interval
             # - notification_interval (X min -> Xm)
-            # - check_period - XXX TODO
-            # - notification_period - XXX TODO
             # - contacts => users XXX DO NOT DELETE contacts and contactgroups, they will be assembled later for notifications!
-            # -
             ####################################################
 
-            ##########################################
-            # escape strings in attributes
-            ##########################################
-            if(defined($cfg_obj_2x->{'service'}->{$service_cnt}->{'action_url'})) {
-                $cfg_obj_2x->{'service'}->{$service_cnt}->{'action_url'} = Icinga2::Utils::escape_str($cfg_obj_2x->{'service'}->{$service_cnt}->{'action_url'});
-            }
-            if(defined($cfg_obj_2x->{'service'}->{$service_cnt}->{'notes_url'})) {
-                $cfg_obj_2x->{'service'}->{$service_cnt}->{'notes_url'} = Icinga2::Utils::escape_str($cfg_obj_2x->{'service'}->{$service_cnt}->{'notes_url'});
-            }
-            if(defined($cfg_obj_2x->{'service'}->{$service_cnt}->{'notes'})) {
-                $cfg_obj_2x->{'service'}->{$service_cnt}->{'notes'} = Icinga2::Utils::escape_str($cfg_obj_2x->{'service'}->{$service_cnt}->{'notes'});
-            }
-            if(defined($cfg_obj_2x->{'service'}->{$service_cnt}->{'icon_image'})) {
-                $cfg_obj_2x->{'service'}->{$service_cnt}->{'icon_image'} = Icinga2::Utils::escape_str($cfg_obj_2x->{'service'}->{$service_cnt}->{'icon_image'});
-            }
-            if(defined($cfg_obj_2x->{'service'}->{$service_cnt}->{'icon_image_alt'})) {
-                $cfg_obj_2x->{'service'}->{$service_cnt}->{'icon_image_alt'} = Icinga2::Utils::escape_str($cfg_obj_2x->{'service'}->{$service_cnt}->{'icon_image_alt'});
-            }
+            ####################################################
+            # migrate custom attributes
+            ####################################################
+            $cfg_obj_2x->{'service'}->{$service_cnt}->{'__I2CONVERT_CUSTOM_ATTR'} = obj_convert_custom_attr_to_custom_hash($cfg_obj_2x->{'service'}->{$service_cnt});
+            #say Dumper($cfg_obj_2x->{'service'}->{$service_cnt}->{'__I2CONVERT_CUSTOM_ATTR'});
 
             ##########################################
             # servicegroups
@@ -1276,24 +1285,23 @@ sub convert_2x {
         # map existing host attributes
         # same:
         # - max_check_attempts
+        # - check_period
+        # - notification_period
+        # custom:
+        # - _CVs
         # - action_url
         # - notes_url
         # - notes
         # - icon_image
         # - statusmap_image
-        # - notes
         # change:
         # - display_name (if alias is set, overwrites it)
         # - hostgroups (commaseperated strings to array)
         # - check_interval (X min -> Xm) + normal_check_interval
         # - retry_interval (X min -> Xm) + retry_check_interval
         # - notification_interval (X min -> Xm)
-        # - check_period - XXX TODO
-        # - notification_period - XXX TODO
         # - contacts => users XXX DO NOT DELETE contacts and contactgroups - they will be assembled later for notifications!
         # -
-        # remove:
-        # - check_command
         ####################################################
 
         ##########################################
@@ -1306,24 +1314,10 @@ sub convert_2x {
             $cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'__I2CONVERT_MACROS'}->{'address6'} = $cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'address6'};
         }
 
-        ##########################################
-        # escape strings in attributes
-        ##########################################
-        if(defined($cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'action_url'})) {
-            $cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'action_url'} = Icinga2::Utils::escape_str($cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'action_url'});
-        }
-        if(defined($cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'notes_url'})) {
-            $cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'notes_url'} = Icinga2::Utils::escape_str($cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'notes_url'});
-        }
-        if(defined($cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'notes'})) {
-            $cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'notes'} = Icinga2::Utils::escape_str($cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'notes'});
-        }
-        if(defined($cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'icon_image'})) {
-            $cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'icon_image'} = Icinga2::Utils::escape_str($cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'icon_image'});
-        }
-        if(defined($cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'icon_image_alt'})) {
-            $cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'icon_image_alt'} = Icinga2::Utils::escape_str($cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'icon_image_alt'});
-        }
+        ####################################################
+        # migrate custom attributes
+        ####################################################
+        $cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'__I2CONVERT_CUSTOM_ATTR'} = obj_convert_custom_attr_to_custom_hash($cfg_obj_2x->{'host'}->{$host_obj_1x_key});
 
         ####################################################
         # display_name -> alias mapping
@@ -1551,6 +1545,11 @@ sub convert_2x {
             }
         }
 
+        ####################################################
+        # migrate custom attributes
+        ####################################################
+        $cfg_obj_2x->{'hostgroup'}->{$hostgroup_obj_1x_key}->{'__I2CONVERT_CUSTOM_ATTR'} = obj_convert_custom_attr_to_custom_hash($cfg_obj_2x->{'hostgroup'}->{$hostgroup_obj_1x_key});
+
         ####################################################
         # check if there are members defined, we must re-link them in their host object again
         ####################################################
@@ -1599,6 +1598,11 @@ sub convert_2x {
             }
         }
 
+        ####################################################
+        # migrate custom attributes
+        ####################################################
+        $cfg_obj_2x->{'servicegroup'}->{$servicegroup_obj_1x_key}->{'__I2CONVERT_CUSTOM_ATTR'} = obj_convert_custom_attr_to_custom_hash($cfg_obj_2x->{'servicegroup'}->{$servicegroup_obj_1x_key});
+
         ####################################################
         # check if there are members defined, we must re-link them in their service object again
         ####################################################
@@ -1656,6 +1660,11 @@ sub convert_2x {
             }
         }
 
+        ####################################################
+        # migrate custom attributes
+        ####################################################
+        $cfg_obj_2x->{'usergroup'}->{$contactgroup_obj_1x_key}->{'__I2CONVERT_CUSTOM_ATTR'} = obj_convert_custom_attr_to_custom_hash($cfg_obj_2x->{'usergroup'}->{$contactgroup_obj_1x_key});
+
         ####################################################
         # check if there are members defined, we must re-link them in their host object again
         ####################################################
index c5d7fb0b2a2b3e1f1af03bd32fac7f7f2bc36e36..54cb94eed1694650b792cd1145c8b283e6e538e3 100644 (file)
@@ -419,26 +419,22 @@ sub dump_service_2x {
         dump_config_line($icinga2_cfg, "\tcheck_period = \"$service_2x->{'check_period'}\",");
     }
 
-    if(defined($service_2x->{'action_url'})) {
-        dump_config_line($icinga2_cfg, "\taction_url = \"$service_2x->{'action_url'}\",");
-    }
-
-    if(defined($service_2x->{'notes_url'})) {
-        dump_config_line($icinga2_cfg, "\tnotes_url = \"$service_2x->{'notes_url'}\",");
-    }
-
-    if(defined($service_2x->{'notes'})) {
-        dump_config_line($icinga2_cfg, "\tnotes = \"$service_2x->{'notes'}\",");
-    }
-
-    if(defined($service_2x->{'icon_image'})) {
-        dump_config_line($icinga2_cfg, "\ticon_image = \"$service_2x->{'icon_image'}\",");
-    }
-
     if(defined($service_2x->{'volatile'})) {
         dump_config_line($icinga2_cfg, "\tvolatile = $service_2x->{'volatile'},");
     }
 
+    ####################################################
+    # custom attr
+    ####################################################
+    if(defined($service_2x->{'__I2CONVERT_CUSTOM_ATTR'}) && $service_2x->{'__I2CONVERT_CUSTOM_ATTR'} != 0) {
+        dump_config_line($icinga2_cfg, "\tcustom = {");
+        foreach my $custom_key (keys %{$service_2x->{'__I2CONVERT_CUSTOM_ATTR'}}) {
+            dump_config_line($icinga2_cfg, "\t\t$custom_key = \"$service_2x->{'__I2CONVERT_CUSTOM_ATTR'}->{$custom_key}\",");
+        }
+        dump_config_line($icinga2_cfg, "\t},");
+    }
+
+    dump_config_line($icinga2_cfg, "");
 
     dump_config_line($icinga2_cfg, "}");
     dump_config_line($icinga2_cfg, "\n");
@@ -586,32 +582,25 @@ sub dump_host_2x {
     if(defined($host_2x->{'max_check_attempts'})) {
         dump_config_line($icinga2_cfg, "\tmax_check_attempts = $host_2x->{'max_check_attempts'},");
     }
-
     if(defined($host_2x->{'check_period'})) {
         dump_config_line($icinga2_cfg, "\tcheck_period = \"$host_2x->{'check_period'}\",");
     }
 
-    if(defined($host_2x->{'action_url'})) {
-        dump_config_line($icinga2_cfg, "\taction_url = \"$host_2x->{'action_url'}\",");
-    }
-
-    if(defined($host_2x->{'notes_url'})) {
-        dump_config_line($icinga2_cfg, "\tnotes_url = \"$host_2x->{'notes_url'}\",");
-    }
-
-    if(defined($host_2x->{'notes'})) {
-        dump_config_line($icinga2_cfg, "\tnotes = \"$host_2x->{'notes'}\",");
-    }
-
-    if(defined($host_2x->{'icon_image'})) {
-        dump_config_line($icinga2_cfg, "\ticon_image = \"$host_2x->{'icon_image'}\",");
-    }
+    ####################################################
+    # custom attr
+    ####################################################
 
-    if(defined($host_2x->{'statusmap_image'})) {
-        dump_config_line($icinga2_cfg, "\tstatusmap_image = $host_2x->{'statusmap_image'},");
+    if(defined($host_2x->{'__I2CONVERT_CUSTOM_ATTR'}) && $host_2x->{'__I2CONVERT_CUSTOM_ATTR'} != 0) {
+        dump_config_line($icinga2_cfg, "\tcustom = {");
+        foreach my $custom_key (keys %{$host_2x->{'__I2CONVERT_CUSTOM_ATTR'}}) {
+            dump_config_line($icinga2_cfg, "\t\t$custom_key = \"$host_2x->{'__I2CONVERT_CUSTOM_ATTR'}->{$custom_key}\",");
+        }
+        dump_config_line($icinga2_cfg, "\t},");
     }
 
+    ####################################################
     # host with no services - valid configuration
+    ####################################################
     if (!defined($host_2x->{'SERVICE'})) {
         dump_config_line($icinga2_cfg, "}");
         dump_config_line($icinga2_cfg, "\n");
@@ -752,28 +741,22 @@ sub dump_host_2x {
         if(defined($service_2x->{'max_check_attempts'})) {
             dump_config_line($icinga2_cfg, "\t\tmax_check_attempts = $service_2x->{'max_check_attempts'},");
         }
-
         if(defined($service_2x->{'check_period'})) {
             dump_config_line($icinga2_cfg, "\tcheck_period = \"$service_2x->{'check_period'}\",");
         }
 
-        if(defined($service_2x->{'action_url'})) {
-            dump_config_line($icinga2_cfg, "\t\taction_url = \"$service_2x->{'action_url'}\",");
-        }
-
-        if(defined($service_2x->{'notes_url'})) {
-            dump_config_line($icinga2_cfg, "\t\tnotes_url = \"$service_2x->{'notes_url'}\",");
-        }
-
-        if(defined($service_2x->{'notes'})) {
-            dump_config_line($icinga2_cfg, "\t\tnotes = \"$service_2x->{'notes'}\",");
-        }
+        ####################################################
+        # custom attr
+        ####################################################
 
-        if(defined($service_2x->{'icon_image'})) {
-            dump_config_line($icinga2_cfg, "\t\ticon_image = \"$service_2x->{'icon_image'}\",");
+        if(defined($service_2x->{'__I2CONVERT_CUSTOM_ATTR'}) && $service_2x->{'__I2CONVERT_CUSTOM_ATTR'} != 0) {
+            dump_config_line($icinga2_cfg, "\t\tcustom = {");
+            foreach my $custom_key (keys %{$service_2x->{'__I2CONVERT_CUSTOM_ATTR'}}) {
+                dump_config_line($icinga2_cfg, "\t\t\t$custom_key = \"$service_2x->{'__I2CONVERT_CUSTOM_ATTR'}->{$custom_key}\",");
+            }
+            dump_config_line($icinga2_cfg, "\t\t},");
         }
 
-
         dump_config_line($icinga2_cfg, "\t},");
         dump_config_line($icinga2_cfg, "");
     }
@@ -855,6 +838,18 @@ sub dump_user_2x {
         }
     }
 
+    ####################################################
+    # custom attr
+    ####################################################
+
+    if(defined($user_2x->{'__I2CONVERT_CUSTOM_ATTR'}) && $user_2x->{'__I2CONVERT_CUSTOM_ATTR'} != 0) {
+        dump_config_line($icinga2_cfg, "\tcustom = {");
+        foreach my $custom_key (keys %{$user_2x->{'__I2CONVERT_CUSTOM_ATTR'}}) {
+            dump_config_line($icinga2_cfg, "\t\t$custom_key = \"$user_2x->{'__I2CONVERT_CUSTOM_ATTR'}->{$custom_key}\",");
+        }
+        dump_config_line($icinga2_cfg, "\t},");
+    }
+
     dump_config_line($icinga2_cfg, "");
     dump_config_line($icinga2_cfg, "}");
     dump_config_line($icinga2_cfg, "\n");
@@ -980,6 +975,19 @@ sub dump_group_2x {
     if(defined($group_2x->{'display_name'})) {
         dump_config_line($icinga2_cfg, "\tdisplay_name = \"$group_2x->{'display_name'}\",");
     }
+
+    ####################################################
+    # custom attr
+    ####################################################
+
+    if(defined($group_2x->{'__I2CONVERT_CUSTOM_ATTR'}) && $group_2x->{'__I2CONVERT_CUSTOM_ATTR'} != 0) {
+        dump_config_line($icinga2_cfg, "\tcustom = {");
+        foreach my $custom_key (keys %{$group_2x->{'__I2CONVERT_CUSTOM_ATTR'}}) {
+            dump_config_line($icinga2_cfg, "\t\t$custom_key = \"$group_2x->{'__I2CONVERT_CUSTOM_ATTR'}->{$custom_key}\",");
+        }
+        dump_config_line($icinga2_cfg, "\t},");
+    }
+
     dump_config_line($icinga2_cfg, "");
     dump_config_line($icinga2_cfg, "}");
     dump_config_line($icinga2_cfg, "\n");