]> granicus.if.org Git - icinga2/commitdiff
configconvert: notification_options -> notification_{state,type}_filter
authorMichael Friedrich <michael.friedrich@netways.de>
Wed, 26 Jun 2013 11:22:15 +0000 (13:22 +0200)
committerMichael Friedrich <michael.friedrich@netways.de>
Wed, 26 Jun 2013 11:22:15 +0000 (13:22 +0200)
parsing the existing comma separated list and using some perl hash magic
to convert it to the new notification_type_filter and
notification_state_filter logic.

fixed missing file permission check on writing config files too.

fixes #4060

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

index a1001327ac60faeb2537cf160171760b345fad95..a717d142d8a72ae0e3558ebdf3bcb53b3618de1d 100644 (file)
@@ -105,7 +105,7 @@ sub obj_2x_command_exists {
 }
 
 
-################################################################################
+#################################################################################
 # Migration
 #################################################################################
 
@@ -715,6 +715,57 @@ sub obj_2x_get_service_servicegroups {
 # Conversion
 #################################################################################
 
+# convert notification_options to state|type_filter
+sub convert_notification_options_to_filter {
+    my $notification_options = shift;
+    my $filter = ();
+    @{$filter->{'state'}} = ();
+    @{$filter->{'type'}} = ();
+
+    # define all types
+    my $filter_names = {
+        'o' => 'StateFilterOK',
+        'w' => 'StateFilterWarning',
+        'c' => 'StateFilterCritical',
+        'u' => 'StateFilterUnknown',
+        'd' => 'StateFilterCritical', # down, treated as critical
+        's' => 'NotificationFilterDowntimeStart | NotificationFilterDowntimeEnd | NotificationFilterDowntimeRemoved',
+        'r' => 'NotificationFilterRecovery',
+        'f' => 'NotificationFilterFlappingStart | NotificationFilterFlappingEnd'
+    };
+    my $filter_by = {
+        'o' => 'state',
+        'w' => 'state',
+        'c' => 'state',
+        'u' => 'state',
+        'd' => 'state',
+        's' => 'type',
+        'r' => 'type',
+        'f' => 'type'
+    };
+
+    # split the string
+    my @options = Icinga2::Utils::str2arr_by_delim_without_excludes($notification_options, ',', 1);
+
+    # verify if there's 'n' (none) or 'a' (all) and ignore the rest then
+    if (grep /n/, @options) {
+        return $filter;
+    }
+    if (grep /a/, @options) {
+        foreach my $by (keys %{$filter_by}) {
+            push @{$filter->{$by}}, $filter_names->{$by};
+        }
+        return $filter;
+    }
+
+    # the selective way
+    foreach my $option (@options) {
+        push @{$filter->{$filter_by->{$option}}}, $filter_names->{$option};
+    }
+
+    return $filter;
+}
+
 # host|service_notification_commands are a comma seperated list w/o arguments
 sub convert_notificationcommand {
     my $objs_1x = shift;
@@ -1059,7 +1110,7 @@ sub convert_2x {
             }
 
             ##########################################
-            # notification_interval
+            # notification_*
             ##########################################
             my $service_notification_interval = undef;
             if(defined($obj_1x_service->{'notification_interval'})) {
@@ -1070,6 +1121,11 @@ sub convert_2x {
                 $cfg_obj_2x->{'service'}->{$service_cnt}->{'notification_interval'} = $service_notification_interval."m";
             }
 
+            if(defined($obj_1x_service->{'notification_options'})) {
+                $cfg_obj_2x->{'service'}->{$service_cnt}->{'__I2CONVERT_NOTIFICATION_FILTERS'} = convert_notification_options_to_filter($obj_1x_service->{'notification_options'});
+                #say Dumper($cfg_obj_2x->{'service'}->{$service_cnt});
+            }
+
             ##########################################
             # eventhandler
             ##########################################
@@ -1334,6 +1390,11 @@ sub convert_2x {
             $cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'notification_interval'} = $host_notification_interval."m";
         }
 
+        if(defined($obj_1x_host->{'notification_options'})) {
+            $cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'__I2CONVERT_NOTIFICATION_FILTERS'} = convert_notification_options_to_filter($obj_1x_host->{'notification_options'});
+            #say Dumper($cfg_obj_2x->{'host'}->{$host_obj_1x_key});
+        }
+
         if(defined($obj_1x_host->{'parents'})) {
             my @host_parents = Icinga2::Utils::str2arr_by_delim_without_excludes($obj_1x_host->{'parents'}, ',', 1);
             push @{$cfg_obj_2x->{'host'}->{$host_obj_1x_key}->{'__I2CONVERT_PARENT_HOSTNAMES'}}, @host_parents;
@@ -1423,6 +1484,13 @@ sub convert_2x {
             }
         }
 
+        ##########################################
+        # notification_options (service only XXX)
+        ##########################################
+        if(defined($obj_1x_contact->{'service_notification_options'})) {
+            $cfg_obj_2x->{'user'}->{$contact_obj_1x_key}->{'__I2CONVERT_NOTIFICATION_FILTERS'} = convert_notification_options_to_filter($obj_1x_contact->{'service_notification_options'});
+        }
+
         ####################################################
         # migrate renamed attributes
         ####################################################
index a9fa5893f954b3c63571adf0c2bb6f76c811fe7f..85fd2c078e9b4631aabcf6ed26f751c4e2ee40d1 100644 (file)
@@ -47,9 +47,13 @@ sub open_cfg_file {
     my $file = shift;
     my $FH;
 
-    say "opening file '$file'...\n";
+    say "writing file '$file'...\n";
     open($FH, ">".$file);
 
+    if (!-w $FH) {
+        print "ERROR: cannot write file '$file'. Check permissions!\n";
+    }
+
     return $FH;
 }
 
@@ -383,8 +387,13 @@ sub dump_service_2x {
     if(defined($service_2x->{'notifications_enabled'})) {
         dump_config_line($icinga2_cfg, "\tnotifications_enabled = $service_2x->{'notifications_enabled'},");
     }
-    if(defined($service_2x->{'notification_options'})) {
-        dump_config_line($icinga2_cfg, "\tnotification_options = \"$service_2x->{'notification_options'}\",");
+
+    if(defined($service_2x->{'__I2CONVERT_NOTIFICATION_FILTERS'})) {
+        say Dumper($service_2x);
+        foreach my $by (keys %{$service_2x->{'__I2CONVERT_NOTIFICATION_FILTERS'}}) {
+            my $notification_filter = "notification_".$by."_filter = (". (join ' | ', @{$service_2x->{'__I2CONVERT_NOTIFICATION_FILTERS'}->{$by}}) .")";
+            dump_config_line($icinga2_cfg, "\t$notification_filter,");
+        }
     }
 
     ####################################################
@@ -503,6 +512,13 @@ sub dump_host_2x {
     ####################################################
     # notifications
     ####################################################
+    if(defined($host_2x->{'__I2CONVERT_NOTIFICATION_FILTERS'})) {
+        foreach my $by (keys %{$host_2x->{'__I2CONVERT_NOTIFICATION_FILTERS'}}) {
+            my $notification_filter = "notification_".$by."_filter = (". (join ' | ', @{$host_2x->{'__I2CONVERT_NOTIFICATION_FILTERS'}->{$by}}) .")";
+            dump_config_line($icinga2_cfg, "\t$notification_filter,");
+        }
+    }
+
     if(defined($host_2x->{'__I2CONVERT_NOTIFICATIONS'})) {
         #say Dumper ($host_2x->{'__I2CONVERT_NOTIFICATIONS'});
         # this is an array of notification objects
@@ -545,9 +561,6 @@ sub dump_host_2x {
     if(defined($host_2x->{'notifications_enabled'})) {
         dump_config_line($icinga2_cfg, "\tnotifications_enabled = $host_2x->{'notifications_enabled'},");
     }
-    if(defined($host_2x->{'notification_options'})) {
-        dump_config_line($icinga2_cfg, "\tnotification_options = \"$host_2x->{'notification_options'}\",");
-    }
 
     ####################################################
     # other host attributes, if set
@@ -652,6 +665,13 @@ sub dump_host_2x {
         ####################################################
         # notifications
         ####################################################
+        if(defined($service_2x->{'__I2CONVERT_NOTIFICATION_FILTERS'})) {
+            foreach my $by (keys %{$service_2x->{'__I2CONVERT_NOTIFICATION_FILTERS'}}) {
+                my $notification_filter = "notification_".$by."_filter = (". (join ' | ', @{$service_2x->{'__I2CONVERT_NOTIFICATION_FILTERS'}->{$by}}) .")";
+                dump_config_line($icinga2_cfg, "\t$notification_filter,");
+            }
+        }
+
         if(defined($service_2x->{'__I2CONVERT_NOTIFICATIONS'})) {
             #say Dumper ($service_2x->{'__I2CONVERT_NOTIFICATIONS'});
             # this is an array of notification objects
@@ -701,9 +721,6 @@ sub dump_host_2x {
         if(defined($service_2x->{'notifications_enabled'})) {
             dump_config_line($icinga2_cfg, "\tnotifications_enabled = $service_2x->{'notifications_enabled'},");
         }
-        if(defined($service_2x->{'notification_options'})) {
-            dump_config_line($icinga2_cfg, "\tnotification_options = \"$service_2x->{'notification_options'}\",");
-        }
 
         ####################################################
         # other service attributes, if set
@@ -790,13 +807,17 @@ sub dump_user_2x {
     # notifications
     ####################################################
 
-    if(defined($user_2x->{'notification_options'})) {
-        dump_config_line($icinga2_cfg, "\tnotification_options = \"$user_2x->{'notification_options'}\",");
-    }
     if(defined($user_2x->{'notification_period'})) {
         dump_config_line($icinga2_cfg, "\tnotification_period = \"$user_2x->{'notification_period'}\",");
     }
 
+    if(defined($user_2x->{'__I2CONVERT_NOTIFICATION_FILTERS'})) {
+        foreach my $by (keys %{$user_2x->{'__I2CONVERT_NOTIFICATION_FILTERS'}}) {
+            my $notification_filter = "notification_".$by."_filter = (". (join ' | ', @{$user_2x->{'__I2CONVERT_NOTIFICATION_FILTERS'}->{$by}}) .")";
+            dump_config_line($icinga2_cfg, "\t$notification_filter,");
+        }
+    }
+
     ####################################################
     # usergroups
     ####################################################
index 6b65ab98307108fe1af02ca4c99e848dd97af30f..904e60d7e32e702e9f8c2bf4f4f6eed5e9112267 100644 (file)
@@ -62,6 +62,12 @@ sub escape_str {
 
     return $str;
 }
+sub escape_shell_meta {
+    my $str = shift;
+
+    $str =~ s/([;<>`'":&!#\$\[\]\{\}\(\)\*\|])/\\$1/g;
+    return $str;
+}
 
 sub debug {
     my $dbg_str = shift;