configconvert: notification_options -> notification_{state,type}_filter

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
This commit is contained in:
Michael Friedrich 2013-06-26 13:22:15 +02:00
parent 277fe0d6ab
commit e7ab01aa6d
3 changed files with 109 additions and 14 deletions

View 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
####################################################

View 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
####################################################

View 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;