From 99874a9eeeb8958f033e50a962b370bd3fefcf93 Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Thu, 10 Oct 2013 20:47:53 +0200 Subject: [PATCH] Refactor config migration script, install as /usr/sbin/icinga2-migrate-config fixes #4696 --- configure.ac | 1 + doc/7-migrating-from-icinga-1x.md | 3 +- icinga2.spec.in | 1 + tools/Makefile.am | 3 +- tools/configconvert/Icinga2.pm | 35 - .../configconvert/Icinga2/ExportIcinga2Cfg.pm | 1099 ---------- .../configconvert/Icinga2/ImportIcinga1Cfg.pm | 309 --- tools/configconvert/Icinga2/Utils.pm | 139 -- tools/configconvert/icinga2_convert_v1_v2.pl | 243 --- tools/configconvert/run_icinga2 | 6 - tools/migration/Makefile.am | 5 + tools/{configconvert => migration}/README | 8 +- .../conf/.gitignore | 0 .../icinga2-migrate-config} | 1800 ++++++++++++++++- .../icinga2-migration.conf} | 0 15 files changed, 1738 insertions(+), 1914 deletions(-) delete mode 100644 tools/configconvert/Icinga2.pm delete mode 100644 tools/configconvert/Icinga2/ExportIcinga2Cfg.pm delete mode 100644 tools/configconvert/Icinga2/ImportIcinga1Cfg.pm delete mode 100644 tools/configconvert/Icinga2/Utils.pm delete mode 100755 tools/configconvert/icinga2_convert_v1_v2.pl delete mode 100755 tools/configconvert/run_icinga2 create mode 100644 tools/migration/Makefile.am rename tools/{configconvert => migration}/README (94%) rename tools/{configconvert => migration}/conf/.gitignore (100%) rename tools/{configconvert/Icinga2/Convert.pm => migration/icinga2-migrate-config} (68%) mode change 100644 => 100755 rename tools/{configconvert/icinga2-conv.conf => migration/icinga2-migration.conf} (100%) diff --git a/configure.ac b/configure.ac index becf3e478..eb4acbcac 100644 --- a/configure.ac +++ b/configure.ac @@ -192,6 +192,7 @@ third-party/cJSON/Makefile third-party/execvpe/Makefile third-party/mmatch/Makefile tools/Makefile +tools/migration/Makefile tools/i2enfeature tools/mkembedconfig/Makefile ]) diff --git a/doc/7-migrating-from-icinga-1x.md b/doc/7-migrating-from-icinga-1x.md index 2a43f34ea..91243a973 100644 --- a/doc/7-migrating-from-icinga-1x.md +++ b/doc/7-migrating-from-icinga-1x.md @@ -26,8 +26,7 @@ possible. > Please check the provided README file for additional notes and possible > scaveats. - # cd tools/configconvert - # ./icinga2_convert_v1_v2.pl -c /etc/icinga/icinga.cfg -o conf/ + # /usr/sbin/icinga2-migrate-config -c /etc/icinga/icinga.cfg -o conf/ ### Manual Config Conversion diff --git a/icinga2.spec.in b/icinga2.spec.in index c3f87aa5c..44ba56428 100644 --- a/icinga2.spec.in +++ b/icinga2.spec.in @@ -215,6 +215,7 @@ fi %{_bindir}/%{name} %{_sbindir}/i2enfeature %{_sbindir}/i2disfeature +%{_sbindir}/icinga2-migrate-config %exclude %{_libdir}/%{name}/libdb_ido_mysql* %{_libdir}/%{name} %{_datadir}/doc/%{name} diff --git a/tools/Makefile.am b/tools/Makefile.am index a3240827c..e64a59db3 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -1,5 +1,6 @@ SUBDIRS = \ - mkembedconfig + migration \ + mkembedconfig sbin_SCRIPTS = i2enfeature CLEANFILES = $(bin_SCRIPTS) diff --git a/tools/configconvert/Icinga2.pm b/tools/configconvert/Icinga2.pm deleted file mode 100644 index 183c4f0d1..000000000 --- a/tools/configconvert/Icinga2.pm +++ /dev/null @@ -1,35 +0,0 @@ - -=pod -/****************************************************************************** - * Icinga 2 * - * Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/) * - * * - * This program is free software; you can redistribute it and/or * - * modify it under the terms of the GNU General Public License * - * as published by the Free Software Foundation; either version 2 * - * of the License, or (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the Free Software Foundation * - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * - ******************************************************************************/ -=cut - -package Icinga2; - -use strict; - -# GLOBAL definitions -# -# - - -1; - -__END__ -# vi: sw=4 ts=4 expandtab : diff --git a/tools/configconvert/Icinga2/ExportIcinga2Cfg.pm b/tools/configconvert/Icinga2/ExportIcinga2Cfg.pm deleted file mode 100644 index 2c241f54f..000000000 --- a/tools/configconvert/Icinga2/ExportIcinga2Cfg.pm +++ /dev/null @@ -1,1099 +0,0 @@ - -=pod -/****************************************************************************** - * Icinga 2 * - * Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/) * - * * - * This program is free software; you can redistribute it and/or * - * modify it under the terms of the GNU General Public License * - * as published by the Free Software Foundation; either version 2 * - * of the License, or (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the Free Software Foundation * - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * - ******************************************************************************/ -=cut - - -package Icinga2::ExportIcinga2Cfg; - -use strict; - -use Data::Dumper; -use File::Find; -use Storable qw(dclone); -use POSIX qw/strftime/; - -use feature 'say'; - -#use Icinga2; -use Icinga2::Utils; - -#our $dbg_lvl = 1; - -# XXX figure a better way for \t count printing, making dumps more modular -# XXX sort macros and linked hostservices by name - -################################################################################ -# Helpers -################################################################################ - -sub open_cfg_file { - my $file = shift; - my $FH; - - say "writing file '$file'...\n"; - open($FH, ">".$file); - - if (!-w $FH) { - print "ERROR: cannot write file '$file'. Check permissions!\n"; - } - - return $FH; -} - -sub close_cfg_file { - my $FH = shift; - - close($FH); -} - -sub add_header { - my $icinga2_cfg = shift; - - dump_config_line($icinga2_cfg, "/******************************************************************************"); - dump_config_line($icinga2_cfg, " * GENERATED BY ICINGA2 CONVERSION SCRIPT"); - dump_config_line($icinga2_cfg, " * " . strftime('%Y-%m-%d %H:%M:%S',localtime)); - dump_config_line($icinga2_cfg, " ******************************************************************************/"); -} - -sub dump_config_line { - my $icinga2_cfg = shift; - my $line = shift; - my $dbg_lvl = $icinga2_cfg->{'__I2EXPORT_DEBUG'}; - - if ($dbg_lvl) { - print $line. "\n"; - } - - print { $icinga2_cfg->{'__I2EXPORT_FH'} } "$line\n"; -} - -sub start_object_type_config_dump { - my $icinga2_cfg = shift; - my $cfg_type = shift; - - $icinga2_cfg->{'__I2EXPORT_FH'} = open_cfg_file($icinga2_cfg->{$cfg_type}); - add_header($icinga2_cfg); -} - -sub end_object_type_config_dump { - my $icinga2_cfg = shift; - - close_cfg_file($icinga2_cfg->{'__I2EXPORT_FH'}); -} - -################################################################################ -# DUMP ALL OBJECTS 2.x -################################################################################ - -sub dump_cfg_resource_2x { - my $icinga2_cfg = shift; - my $icinga2_global_macros = shift; - - start_object_type_config_dump($icinga2_cfg, 'resource'); - - dump_config_line($icinga2_cfg, "set IcingaMacros = {"); - foreach my $global_macro_2x_key (sort keys %{$icinga2_global_macros}) { - dump_config_line($icinga2_cfg, "\t\"$global_macro_2x_key\" = \"$icinga2_global_macros->{$global_macro_2x_key}\","); - } - - dump_config_line($icinga2_cfg, "}"); - - end_object_type_config_dump($icinga2_cfg); -} - -sub dump_cfg_obj_2x { - my $icinga2_cfg = shift; - my $cfg_obj_2x = shift; - - dump_hosts_2x($icinga2_cfg, $cfg_obj_2x); - dump_services_2x($icinga2_cfg, $cfg_obj_2x); - dump_users_2x($icinga2_cfg, $cfg_obj_2x); - dump_notifications_2x($icinga2_cfg, $cfg_obj_2x); - dump_timeperiods_2x($icinga2_cfg, $cfg_obj_2x); - dump_groups_2x($icinga2_cfg, $cfg_obj_2x); - dump_commands_2x($icinga2_cfg, $cfg_obj_2x); - -} -sub dump_hosts_2x { - my $icinga2_cfg = shift; - my $cfg_obj_2x = shift; - - start_object_type_config_dump($icinga2_cfg, 'hosts'); - #say Dumper($icinga2_cfg); - - foreach my $host_2x_key (keys %{@$cfg_obj_2x{'host'}}) { - my $host_2x = @$cfg_obj_2x{'host'}->{$host_2x_key}; - #say Dumper($host_2x); - #say "==============\n"; - # function decides itsself if object or template - Icinga2::ExportIcinga2Cfg::dump_host_2x($icinga2_cfg, $host_2x); - } - - end_object_type_config_dump($icinga2_cfg); -} - -sub dump_services_2x { - my $icinga2_cfg = shift; - my $cfg_obj_2x = shift; - - start_object_type_config_dump($icinga2_cfg, 'services'); - - foreach my $service_2x_key (keys %{@$cfg_obj_2x{'service'}}) { - my $service_2x = @$cfg_obj_2x{'service'}->{$service_2x_key}; - - Icinga2::ExportIcinga2Cfg::dump_service_2x($icinga2_cfg, $service_2x); - } - - end_object_type_config_dump($icinga2_cfg); -} - -sub dump_users_2x { - my $icinga2_cfg = shift; - my $cfg_obj_2x = shift; - - start_object_type_config_dump($icinga2_cfg, 'users'); - - foreach my $user_2x_key (keys %{@$cfg_obj_2x{'user'}}) { - my $user_2x = @$cfg_obj_2x{'user'}->{$user_2x_key}; - - Icinga2::ExportIcinga2Cfg::dump_user_2x($icinga2_cfg, $user_2x); - } - - end_object_type_config_dump($icinga2_cfg); -} - -sub dump_notifications_2x { - my $icinga2_cfg = shift; - my $cfg_obj_2x = shift; - - start_object_type_config_dump($icinga2_cfg, 'notifications'); - - foreach my $notification_2x_key (keys %{@$cfg_obj_2x{'notification'}}) { - my $notification_2x = @$cfg_obj_2x{'notification'}->{$notification_2x_key}; - - Icinga2::ExportIcinga2Cfg::dump_notification_2x($icinga2_cfg, $notification_2x); - } - - end_object_type_config_dump($icinga2_cfg); -} - - -sub dump_timeperiods_2x { - my $icinga2_cfg = shift; - my $cfg_obj_2x = shift; - - start_object_type_config_dump($icinga2_cfg, 'timeperiods'); - - foreach my $timeperiod_2x_key (keys %{@$cfg_obj_2x{'timeperiod'}}) { - my $timeperiod_2x = @$cfg_obj_2x{'timeperiod'}->{$timeperiod_2x_key}; - - Icinga2::ExportIcinga2Cfg::dump_timeperiod_2x($icinga2_cfg, $timeperiod_2x); - } - - end_object_type_config_dump($icinga2_cfg); -} - -# XXX maybe split later -sub dump_groups_2x { - my $icinga2_cfg = shift; - my $cfg_obj_2x = shift; - - start_object_type_config_dump($icinga2_cfg, 'groups'); - - if (!@$cfg_obj_2x{'hostgroup'}) { - goto SKIP_HOSTGROUPS; - } - - foreach my $hostgroup_2x_key (keys %{@$cfg_obj_2x{'hostgroup'}}) { - my $hostgroup_2x = @$cfg_obj_2x{'hostgroup'}->{$hostgroup_2x_key}; - - Icinga2::ExportIcinga2Cfg::dump_group_2x($icinga2_cfg, $hostgroup_2x); - } - -SKIP_HOSTGROUPS: - - if (!@$cfg_obj_2x{'servicegroup'}) { - goto SKIP_SERVICEGROUPS; - } - - foreach my $servicegroup_2x_key (keys %{@$cfg_obj_2x{'servicegroup'}}) { - my $servicegroup_2x = @$cfg_obj_2x{'servicegroup'}->{$servicegroup_2x_key}; - - Icinga2::ExportIcinga2Cfg::dump_group_2x($icinga2_cfg, $servicegroup_2x); - } - -SKIP_SERVICEGROUPS: - - if (!@$cfg_obj_2x{'usergroup'}) { - goto SKIP_USERGROUPS; - } - - foreach my $usergroup_2x_key (keys %{@$cfg_obj_2x{'usergroup'}}) { - my $usergroup_2x = @$cfg_obj_2x{'usergroup'}->{$usergroup_2x_key}; - - Icinga2::ExportIcinga2Cfg::dump_group_2x($icinga2_cfg, $usergroup_2x); - } - -SKIP_USERGROUPS: - end_object_type_config_dump($icinga2_cfg); -} - -sub dump_commands_2x { - my $icinga2_cfg = shift; - my $cfg_obj_2x = shift; - - start_object_type_config_dump($icinga2_cfg, 'commands'); - - foreach my $command_2x_key (keys %{@$cfg_obj_2x{'command'}}) { - my $command_2x = @$cfg_obj_2x{'command'}->{$command_2x_key}; - - Icinga2::ExportIcinga2Cfg::dump_command_2x($icinga2_cfg, $command_2x); - } - - end_object_type_config_dump($icinga2_cfg); -} - -################################################################################ -# DUMP OBJECT 2.x -################################################################################ - - -sub dump_service_2x { - my $icinga2_cfg = shift; - my $service_2x = shift; - my $object_type = "object"; # object or template - my $service_description = $service_2x->{__I2CONVERT_SERVICEDESCRIPTION}; - - # only dump templates, the objects will be directly created in host objects - if ($service_2x->{__I2CONVERT_IS_TEMPLATE} == 0) { - return; - } - #say Dumper($host_2x); - if ($service_2x->{__I2CONVERT_IS_TEMPLATE} == 1) { - $object_type = "template"; - $service_description = $service_2x->{'__I2CONVERT_TEMPLATE_NAME'}; - } - - #################################################### - # start, inherit from template? - #################################################### - if (defined($service_2x->{'__I2CONVERT_USES_TEMPLATE'}) && $service_2x->{'__I2CONVERT_USES_TEMPLATE'} == 1) { - my $service_2x_templates = join '", "', @{$service_2x->{'__I2CONVERT_TEMPLATE_NAMES'}}; - dump_config_line($icinga2_cfg, "$object_type Service \"$service_description\" inherits \"$service_2x_templates\" {"); - } else { - dump_config_line($icinga2_cfg, "$object_type Service \"$service_description\" {"); - } - - #################################################### - # display_name - #################################################### - if(defined($service_2x->{'display_name'})) { - dump_config_line($icinga2_cfg, "\tdisplay_name = \"$service_2x->{'display_name'}\","); - } - - #################################################### - # macros - #################################################### - - if(defined($service_2x->{'__I2CONVERT_MACROS'}) && $service_2x->{'__I2CONVERT_MACROS'} != 0) { - dump_config_line($icinga2_cfg, "\tmacros = {"); - foreach my $macro_key (keys %{$service_2x->{'__I2CONVERT_MACROS'}}) { - next if $macro_key =~ /USER/; - dump_config_line($icinga2_cfg, "\t\t$macro_key = \"$service_2x->{'__I2CONVERT_MACROS'}->{$macro_key}\","); - } - dump_config_line($icinga2_cfg, "\t},"); - } - - dump_config_line($icinga2_cfg, ""); - - #################################################### - # check command - #################################################### - if(defined($service_2x->{'__I2_CONVERT_CHECKCOMMAND_NAME'})) { - dump_config_line($icinga2_cfg, "\tcheck_command = \"$service_2x->{'__I2_CONVERT_CHECKCOMMAND_NAME'}\","); - } - - #################################################### - # event command - #################################################### - if(defined($service_2x->{'__I2_CONVERT_EVENTCOMMAND_NAME'})) { - dump_config_line($icinga2_cfg, "\tevent_command = \"$service_2x->{'__I2_CONVERT_EVENTCOMMAND_NAME'}\","); - } - - #################################################### - # servicegroups - #################################################### - if(defined($service_2x->{'servicegroups'}) && scalar(@{$service_2x->{'servicegroups'}}) > 0) { - #say Dumper($service_2x->{'servicegroups'}); - my $servicegroups = join '", "', @{$service_2x->{'servicegroups'}}; - if ($service_2x->{'__I2_CONVERT_SG_ADD'} == 1) { - dump_config_line($icinga2_cfg, "\tgroups += [ \"$servicegroups\" ],"); - } else { - dump_config_line($icinga2_cfg, "\tgroups = [ \"$servicegroups\" ],"); - } - #say Dumper($service_description); - #say Dumper($service_2x->{'servicegroups'}); - } - - #################################################### - # servicedependencies (1.x deps) - #################################################### - if(defined($service_2x->{'__I2CONVERT_PARENT_SERVICES'})) { - dump_config_line($icinga2_cfg, "\tservice_dependencies = ["); - - #say Dumper($service_2x); - # this is a hash with keys - foreach my $servicedep_key (keys %{$service_2x->{'__I2CONVERT_PARENT_SERVICES'}}) { - my $servicedep = $service_2x->{'__I2CONVERT_PARENT_SERVICES'}->{$servicedep_key}; - dump_config_line($icinga2_cfg, "\t\t{ host = \"$servicedep->{'host'}\", service = \"$servicedep->{'service'}\" },"); - } - dump_config_line($icinga2_cfg, "\t],"); - } - - #################################################### - # notifications - #################################################### - if(defined($service_2x->{'__I2CONVERT_NOTIFICATIONS'})) { - #say Dumper ($service_2x->{'__I2CONVERT_NOTIFICATIONS'}); - # this is an array of notification objects - foreach my $service_notification_hash (@{$service_2x->{'__I2CONVERT_NOTIFICATIONS'}}) { - - #say Dumper($service_notification_hash); - - # this is a hash by unique key of the notification template, but all further attributes are seperatedly available too - foreach my $service_notification_key (keys %{$service_notification_hash}) { - my $service_notification = $service_notification_hash->{$service_notification_key}; - #say Dumper($service_notification); - - # skip everything not related to service notifications - next if ($service_notification->{'type'} ne 'service'); - - dump_config_line($icinga2_cfg, "\tnotifications[\"$service_notification->{'name'}\"] = {"); - - if (defined ($service_notification->{'templates'}) && @{$service_notification->{'templates'}} > 0) { - my $service_notification_templates = join '", "', @{$service_notification->{'templates'}}; - dump_config_line($icinga2_cfg, "\t\ttemplates = [ \"$service_notification_templates\" ],"); - } - - if(defined($service_notification->{'users'}) && @{$service_notification->{'users'}} > 0) { - my $service_users = join '", "', @{$service_notification->{'users'}}; - dump_config_line($icinga2_cfg, "\t\tusers = [ \"$service_users\" ],"); - } - - if(defined($service_notification->{'__I2CONVERT_NOTIFICATION_FILTERS'})) { - #say Dumper($service_notification); - foreach my $by (keys %{$service_notification->{'__I2CONVERT_NOTIFICATION_FILTERS'}}) { - next if !@{$service_notification->{'__I2CONVERT_NOTIFICATION_FILTERS'}->{$by}}; - my $notification_filter; - if (grep /0/, @{$service_notification->{'__I2CONVERT_NOTIFICATION_FILTERS'}->{$by}}) { - $notification_filter = 0; - } else { - $notification_filter = "(". (join ' | ', @{$service_notification->{'__I2CONVERT_NOTIFICATION_FILTERS'}->{$by}}) .")"; - } - dump_config_line($icinga2_cfg, "\t\tnotification_".$by."_filter = $notification_filter,"); - } - } - - if(defined($service_notification->{'__I2CONVERT_NOTIFICATION_INTERVAL'})) { - dump_config_line($icinga2_cfg, "\t\tnotification_interval = $service_notification->{'__I2CONVERT_NOTIFICATION_INTERVAL'},"); - } - - if(defined($service_notification->{'__I2CONVERT_NOTIFICATION_PERIOD'})) { - dump_config_line($icinga2_cfg, "\t\tnotification_period = \"$service_notification->{'__I2CONVERT_NOTIFICATION_PERIOD'}\","); - } - - # this is set for escalations - if(defined($service_notification->{'__I2CONVERT_NOTIFICATION_TIMES'}) && $service_notification->{'__I2CONVERT_NOTIFICATION_TIMES'} != 0) { - dump_config_line($icinga2_cfg, "\t\ttimes = {"); - dump_config_line($icinga2_cfg, "\t\t\tbegin = $service_notification->{'__I2CONVERT_NOTIFICATION_TIMES'}->{'begin'},"); - dump_config_line($icinga2_cfg, "\t\t\tend = $service_notification->{'__I2CONVERT_NOTIFICATION_TIMES'}->{'end'}"); - dump_config_line($icinga2_cfg, "\t\t},"); - } - - dump_config_line($icinga2_cfg, "\t},"); - } - } - } - - #################################################### - # other service attributes, if set - #################################################### - if(defined($service_2x->{'check_interval'})) { - dump_config_line($icinga2_cfg, "\tcheck_interval = $service_2x->{'check_interval'},"); - } - if(defined($service_2x->{'retry_interval'})) { - dump_config_line($icinga2_cfg, "\tretry_interval = $service_2x->{'retry_interval'},"); - } - if(defined($service_2x->{'max_check_attempts'})) { - dump_config_line($icinga2_cfg, "\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->{'volatile'})) { - dump_config_line($icinga2_cfg, "\tvolatile = $service_2x->{'volatile'},"); - } - - if(defined($service_2x->{'flap_detection_enabled'})) { - dump_config_line($icinga2_cfg, "\tenable_flapping = $service_2x->{'flap_detection_enabled'},"); - } - - #################################################### - # 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, "\tmacros += {"); - foreach my $custom_key (keys %{$service_2x->{'__I2CONVERT_CUSTOM_ATTR'}}) { - if ($custom_key !~ /^_/) { - next; - } - - my $key = substr($custom_key, 1); - dump_config_line($icinga2_cfg, "\t\t_SERVICE$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"); -} - -sub dump_host_2x { - my $icinga2_cfg = shift; - my $host_2x = shift; - my $object_type = "object"; # object or template - my $host_name = $host_2x->{'host_name'}; # default, may be changed for templates - - #say Dumper($host_2x); - if ($host_2x->{__I2CONVERT_IS_TEMPLATE} == 1) { - $object_type = "template"; - $host_name = $host_2x->{'__I2CONVERT_TEMPLATE_NAME'}; - } - - #################################################### - # start, inherit from template? - #################################################### - if (defined($host_2x->{'__I2CONVERT_USES_TEMPLATE'}) && $host_2x->{'__I2CONVERT_USES_TEMPLATE'} == 1) { - my $host_2x_templates = join '", "', @{$host_2x->{'__I2CONVERT_TEMPLATE_NAMES'}}; - dump_config_line($icinga2_cfg, "$object_type Host \"$host_name\" inherits \"$host_2x_templates\" {"); - } else { - dump_config_line($icinga2_cfg, "$object_type Host \"$host_name\" {"); - } - - #################################################### - # display_name - #################################################### - if(defined($host_2x->{'display_name'})) { - dump_config_line($icinga2_cfg, "\tdisplay_name = \"$host_2x->{'display_name'}\","); - } - - #################################################### - # macros - #################################################### - dump_config_line($icinga2_cfg, ""); - - if(defined($host_2x->{'__I2CONVERT_MACROS'}) && $host_2x->{'__I2CONVERT_MACROS'} != 0) { - dump_config_line($icinga2_cfg, "\tmacros = {"); - foreach my $macro_key (keys %{$host_2x->{'__I2CONVERT_MACROS'}}) { - next if $macro_key =~ /USER/; - dump_config_line($icinga2_cfg, "\t\t$macro_key = \"$host_2x->{'__I2CONVERT_MACROS'}->{$macro_key}\","); - } - dump_config_line($icinga2_cfg, "\t},"); - } - dump_config_line($icinga2_cfg, ""); - - #################################################### - # hostgroups - #################################################### - if(defined($host_2x->{'hostgroups'})) { - my $hostgroups = join '", "', @{$host_2x->{'hostgroups'}}; - if ($host_2x->{'__I2_CONVERT_HG_ADD'} == 1) { - dump_config_line($icinga2_cfg, "\tgroups += [ \"$hostgroups\" ],"); - } else { - dump_config_line($icinga2_cfg, "\tgroups = [ \"$hostgroups\" ],"); - } - } - - #################################################### - # hostdependencies (1.x deps and parents combined) - #################################################### - if(defined($host_2x->{'__I2CONVERT_PARENT_HOSTNAMES'})) { - my $hostdependency_hosts = join '", "', @{$host_2x->{'__I2CONVERT_PARENT_HOSTNAMES'}}; - dump_config_line($icinga2_cfg, "\thost_dependencies = [ \"$hostdependency_hosts\" ],"); - } - - #################################################### - # custom attr - #################################################### - - 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},"); - - dump_config_line($icinga2_cfg, "\tmacros += {"); - foreach my $custom_key (keys %{$host_2x->{'__I2CONVERT_CUSTOM_ATTR'}}) { - if ($custom_key !~ /^_/) { - next; - } - - my $key = substr($custom_key, 1); - dump_config_line($icinga2_cfg, "\t\t_HOST$key = \"$host_2x->{'__I2CONVERT_CUSTOM_ATTR'}->{$custom_key}\","); - } - dump_config_line($icinga2_cfg, "\t},"); - } - - #################################################### - # hostcheck - #################################################### - # this is magic, and must be set during conversion - if(defined($host_2x->{'__I2CONVERT_HOSTCHECK'})) { - dump_config_line($icinga2_cfg, "\tcheck = \"$host_2x->{'__I2CONVERT_HOSTCHECK'}\","); - } - - #################################################### - # host with no services - valid configuration - #################################################### - if (!defined($host_2x->{'SERVICE'})) { - dump_config_line($icinga2_cfg, "}"); - dump_config_line($icinga2_cfg, "\n"); - return; - } - - #say Dumper($host_2x->{'SERVICE'}); - - #################################################### - # now all services with templates - #################################################### - foreach my $service_2x_key (keys %{$host_2x->{'SERVICE'}}) { - my $service_2x = $host_2x->{'SERVICE'}->{$service_2x_key}; - - dump_config_line($icinga2_cfg, "\tservices[\"$service_2x->{__I2CONVERT_SERVICEDESCRIPTION}\"] = {"); - - #################################################### - # templates - #################################################### - if (defined($service_2x->{'__I2CONVERT_USES_TEMPLATE'}) && $service_2x->{'__I2CONVERT_USES_TEMPLATE'} == 1) { - my $service_2x_templates = join '", "', @{$service_2x->{'__I2CONVERT_TEMPLATE_NAMES'}}; - dump_config_line($icinga2_cfg, "\t\ttemplates = [ \"$service_2x_templates\" ],") - } - #################################################### - # display_name - #################################################### - if(defined($service_2x->{'display_name'})) { - dump_config_line($icinga2_cfg, "\t\tdisplay_name = \"$service_2x->{'display_name'}\","); - } - - dump_config_line($icinga2_cfg, ""); - - #################################################### - # macros - #################################################### - - if(defined($service_2x->{'__I2CONVERT_MACROS'}) && $service_2x->{'__I2CONVERT_MACROS'} != 0) { - dump_config_line($icinga2_cfg, "\t\tmacros = {"); - foreach my $macro_key (keys %{$service_2x->{'__I2CONVERT_MACROS'}}) { - next if $macro_key =~ /USER/; - dump_config_line($icinga2_cfg, "\t\t\t$macro_key = \"$service_2x->{'__I2CONVERT_MACROS'}->{$macro_key}\","); - } - dump_config_line($icinga2_cfg, "\t\t},"); - } - - - #################################################### - # check_command - #################################################### - if(defined($service_2x->{'__I2_CONVERT_CHECKCOMMAND_NAME'})) { - dump_config_line($icinga2_cfg, "\t\tcheck_command = \"$service_2x->{'__I2_CONVERT_CHECKCOMMAND_NAME'}\","); - } - - #################################################### - # servicegroups - #################################################### - if(defined($service_2x->{'servicegroups'}) && scalar(@{$service_2x->{'servicegroups'}}) > 0) { - #say Dumper($service_2x->{'servicegroups'}); - my $servicegroups = join '", "', @{$service_2x->{'servicegroups'}}; - if ($service_2x->{'__I2_CONVERT_SG_ADD'} == 1) { - dump_config_line($icinga2_cfg, "\t\tgroups += [ \"$servicegroups\" ],"); - } else { - dump_config_line($icinga2_cfg, "\t\tgroups = [ \"$servicegroups\" ],"); - } - } - - #################################################### - # notifications - #################################################### - if(defined($service_2x->{'__I2CONVERT_NOTIFICATIONS'})) { - #say Dumper ($service_2x->{'__I2CONVERT_NOTIFICATIONS'}); - # this is an array of notification objects - foreach my $service_notification_hash (@{$service_2x->{'__I2CONVERT_NOTIFICATIONS'}}) { - - #say Dumper($service_notification_hash); - - # this is a hash by unique key of the notification template, but all further attributes are seperatedly available too - foreach my $service_notification_key (keys %{$service_notification_hash}) { - my $service_notification = $service_notification_hash->{$service_notification_key}; - #say Dumper($service_notification); - - # skip everything not related to service notifications - next if ($service_notification->{'type'} ne 'service'); - - dump_config_line($icinga2_cfg, "\t\tnotifications[\"$service_notification->{'name'}\"] = {"); - - if (defined ($service_notification->{'templates'}) && @{$service_notification->{'templates'}} > 0) { - my $service_notification_templates = join '", "', @{$service_notification->{'templates'}}; - dump_config_line($icinga2_cfg, "\t\t\ttemplates = [ \"$service_notification_templates\" ],"); - } - - if(defined($service_notification->{'users'}) && @{$service_notification->{'users'}} > 0) { - my $service_users = join '", "', @{$service_notification->{'users'}}; - dump_config_line($icinga2_cfg, "\t\t\tusers = [ \"$service_users\" ],"); - } - - # this is set for escalations - if(defined($service_notification->{'__I2CONVERT_NOTIFICATION_TIMES'}) && $service_notification->{'__I2CONVERT_NOTIFICATION_TIMES'} != 0) { - dump_config_line($icinga2_cfg, "\t\t\ttimes = {"); - dump_config_line($icinga2_cfg, "\t\t\t\tbegin = $service_notification->{'__I2CONVERT_NOTIFICATION_TIMES'}->{'begin'},"); - dump_config_line($icinga2_cfg, "\t\t\t\tend = $service_notification->{'__I2CONVERT_NOTIFICATION_TIMES'}->{'end'}"); - dump_config_line($icinga2_cfg, "\t\t\t},"); - } - - dump_config_line($icinga2_cfg, "\t\t},"); - } - } - } - - if(defined($host_2x->{'__I2CONVERT_NOTIFICATIONS'}) && $host_2x->{'__I2CONVERT_HOSTCHECK'} eq $service_2x->{__I2CONVERT_SERVICEDESCRIPTION}) { - #say Dumper ($host_2x->{'__I2CONVERT_NOTIFICATIONS'}); - # this is an array of notification objects - foreach my $host_notification_hash (@{$host_2x->{'__I2CONVERT_NOTIFICATIONS'}}) { - - #say Dumper($host_notification_hash); - - # this is a hash by unique key of the notification template, but all further attributes are seperatedly available too - foreach my $host_notification_key (keys %{$host_notification_hash}) { - my $host_notification = $host_notification_hash->{$host_notification_key}; - #say Dumper($host_notification); - - # skip everything not related to host notifications - next if ($host_notification->{'type'} ne 'host'); - - dump_config_line($icinga2_cfg, "\t\tnotifications[\"$host_notification->{'name'}\"] = {"); - - if (defined ($host_notification->{'templates'}) && @{$host_notification->{'templates'}} > 0) { - my $host_notification_templates = join '", "', @{$host_notification->{'templates'}}; - dump_config_line($icinga2_cfg, "\t\t\ttemplates = [ \"$host_notification_templates\" ],"); - } - - if(defined($host_notification->{'users'}) && @{$host_notification->{'users'}} > 0) { - my $host_users = join '", "', @{$host_notification->{'users'}}; - dump_config_line($icinga2_cfg, "\t\t\tusers = [ \"$host_users\" ],"); - } - - dump_config_line($icinga2_cfg, "\t\t},"); - } - } - } - - #################################################### - # other service attributes, if set - #################################################### - if(defined($service_2x->{'check_interval'})) { - dump_config_line($icinga2_cfg, "\t\tcheck_interval = $service_2x->{'check_interval'},"); - } - if(defined($service_2x->{'retry_interval'})) { - dump_config_line($icinga2_cfg, "\t\tretry_interval = $service_2x->{'retry_interval'},"); - } - 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->{'flap_detection_enabled'})) { - dump_config_line($icinga2_cfg, "\t\tenable_flapping = $service_2x->{'flap_detection_enabled'},"); - } - - #################################################### - # custom attr - #################################################### - - 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\tmacros += {"); - foreach my $custom_key (keys %{$service_2x->{'__I2CONVERT_CUSTOM_ATTR'}}) { - if ($custom_key !~ /^_/) { - next; - } - - my $key = substr($custom_key, 1); - dump_config_line($icinga2_cfg, "\t\t\t_SERVICE$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, ""); - } - dump_config_line($icinga2_cfg, "}"); - dump_config_line($icinga2_cfg, "\n"); -} - -sub dump_user_2x { - my $icinga2_cfg = shift; - my $user_2x = shift; - my $object_type = "object"; # object or template - my $user_name = $user_2x->{'contact_name'}; # default, may be changed for templates - - #say Dumper($user_2x); - if ($user_2x->{__I2CONVERT_IS_TEMPLATE} == 1) { - $object_type = "template"; - $user_name = $user_2x->{'__I2CONVERT_TEMPLATE_NAME'}; - } - - #################################################### - # start, inherit from template? - #################################################### - if (defined($user_2x->{'__I2CONVERT_USES_TEMPLATE'}) && $user_2x->{'__I2CONVERT_USES_TEMPLATE'} == 1) { - my $user_2x_templates = join '", "', @{$user_2x->{'__I2CONVERT_TEMPLATE_NAMES'}}; - dump_config_line($icinga2_cfg, "$object_type User \"$user_name\" inherits \"$user_2x_templates\" {"); - } else { - dump_config_line($icinga2_cfg, "$object_type User \"$user_name\" {"); - } - - if(defined($user_2x->{'display_name'})) { - dump_config_line($icinga2_cfg, "\tdisplay_name = \"$user_2x->{'display_name'}\","); - } - - #################################################### - # macros - #################################################### - - if(defined($user_2x->{'__I2CONVERT_MACROS'}) && $user_2x->{'__I2CONVERT_MACROS'} != 0) { - dump_config_line($icinga2_cfg, "\tmacros = {"); - foreach my $macro_key (keys %{$user_2x->{'__I2CONVERT_MACROS'}}) { - next if $macro_key =~ /USER/; - dump_config_line($icinga2_cfg, "\t\t$macro_key = \"$user_2x->{'__I2CONVERT_MACROS'}->{$macro_key}\","); - } - dump_config_line($icinga2_cfg, "\t},"); - } - - dump_config_line($icinga2_cfg, ""); - - #################################################### - # notifications - #################################################### - - 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'}}) { - next if !@{$user_2x->{'__I2CONVERT_NOTIFICATION_FILTERS'}->{$by}}; - my $notification_filter; - if (grep /0/, @{$user_2x->{'__I2CONVERT_NOTIFICATION_FILTERS'}->{$by}}) { - $notification_filter = 0; - } else { - $notification_filter = "(". (join ' | ', @{$user_2x->{'__I2CONVERT_NOTIFICATION_FILTERS'}->{$by}}) .")"; - } - dump_config_line($icinga2_cfg, "\tnotification_".$by."_filter = $notification_filter,"); - } - } - - if(defined($user_2x->{'notifications_enabled'})) { - dump_config_line($icinga2_cfg, "\tenable_notifications = \"$user_2x->{'notifications_enabled'}\","); - } - #################################################### - # usergroups - #################################################### - if(defined($user_2x->{'usergroups'})) { - #say Dumper($user_2x->{'usergroups'}); - my $usergroups = join '", "', @{$user_2x->{'usergroups'}}; - if ($user_2x->{'__I2_CONVERT_UG_ADD'} == 1) { - dump_config_line($icinga2_cfg, "\tgroups += [ \"$usergroups\" ],"); - } else { - dump_config_line($icinga2_cfg, "\tgroups = [ \"$usergroups\" ],"); - } - } - - #################################################### - # 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, "\tmacros += {"); - foreach my $custom_key (keys %{$user_2x->{'__I2CONVERT_CUSTOM_ATTR'}}) { - if ($custom_key !~ /^_/) { - next; - } - - my $key = substr($custom_key, 1); - dump_config_line($icinga2_cfg, "\t\t_CONTACT$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"); -} - -sub dump_notification_2x { - my $icinga2_cfg = shift; - my $notification_2x = shift; - my $object_type = "object"; # object or template - my $notification_name = $notification_2x->{'__I2CONVERT_NOTIFICATION_NAME'}; # default, may be changed for templates - - #say Dumper($notification_2x); - if ($notification_2x->{__I2CONVERT_IS_TEMPLATE} == 1) { - $object_type = "template"; - $notification_name = $notification_2x->{'__I2CONVERT_NOTIFICATION_TEMPLATE_NAME'}; - } - - return if (!defined($notification_name)); - - #################################################### - # start, inherit from template? - #################################################### - if (defined($notification_2x->{'__I2CONVERT_USES_TEMPLATE'}) && $notification_2x->{'__I2CONVERT_USES_TEMPLATE'} == 1) { - my $notification_2x_templates = join '", "', @{$notification_2x->{'__I2CONVERT_TEMPLATE_NAMES'}}; - dump_config_line($icinga2_cfg, "$object_type Notification \"$notification_name\" inherits \"$notification_2x_templates\" {"); - } else { - dump_config_line($icinga2_cfg, "$object_type Notification \"$notification_name\" {"); - } - - if(defined($notification_2x->{'display_name'})) { - dump_config_line($icinga2_cfg, "\tdisplay_name = \"$notification_2x->{'display_name'}\","); - } - - if(defined($notification_2x->{'__I2CONVERT_NOTIFICATION_COMMAND'})) { - #say Dumper($notifications_2x->{'notification_command'}); - dump_config_line($icinga2_cfg, "\tnotification_command = \"$notification_2x->{'__I2CONVERT_NOTIFICATION_COMMAND'}\","); - } - - if(defined($notification_2x->{'export_macros'})) { - #say Dumper($notification_2x->{'export_macros'}); - my $export_macros = join '",\n"', @{$notification_2x->{'export_macros'}}; - dump_config_line($icinga2_cfg, "\texport_macros = [ \"$export_macros\" ],"); - } - - if(defined($notification_2x->{'users'}) && @{$notification_2x->{'users'}} > 0) { - my $service_users = join '", "', @{$notification_2x->{'users'}}; - dump_config_line($icinga2_cfg, "\tusers = [ \"$service_users\" ],"); - } - - if(defined($notification_2x->{'usergroups'}) && @{$notification_2x->{'usergroups'}} > 0) { - my $service_usergroups = join '", "', @{$notification_2x->{'usergroups'}}; - dump_config_line($icinga2_cfg, "\tusergroups = [ \"$service_usergroups\" ],"); - } - - if(defined($notification_2x->{'__I2CONVERT_NOTIFICATION_FILTERS'})) { - foreach my $by (keys %{$notification_2x->{'__I2CONVERT_NOTIFICATION_FILTERS'}}) { - next if !@{$notification_2x->{'__I2CONVERT_NOTIFICATION_FILTERS'}->{$by}}; - my $notification_filter; - if (grep /0/, @{$notification_2x->{'__I2CONVERT_NOTIFICATION_FILTERS'}->{$by}}) { - $notification_filter = 0; - } else { - $notification_filter = "(". (join ' | ', @{$notification_2x->{'__I2CONVERT_NOTIFICATION_FILTERS'}->{$by}}) .")"; - } - dump_config_line($icinga2_cfg, "\tnotification_".$by."_filter = $notification_filter,"); - } - } - - if(defined($notification_2x->{'__I2CONVERT_NOTIFICATION_INTERVAL'})) { - dump_config_line($icinga2_cfg, "\tnotification_interval = $notification_2x->{'__I2CONVERT_NOTIFICATION_INTERVAL'},"); - } - - if(defined($notification_2x->{'__I2CONVERT_NOTIFICATION_PERIOD'})) { - dump_config_line($icinga2_cfg, "\tnotification_period = \"$notification_2x->{'__I2CONVERT_NOTIFICATION_PERIOD'}\","); - } - - # this is set for escalations - if(defined($notification_2x->{'__I2CONVERT_NOTIFICATION_TIMES'}) && $notification_2x->{'__I2CONVERT_NOTIFICATION_TIMES'} != 0) { - dump_config_line($icinga2_cfg, "\ttimes = {"); - dump_config_line($icinga2_cfg, "\t\tbegin = $notification_2x->{'__I2CONVERT_NOTIFICATION_TIMES'}->{'begin'},"); - dump_config_line($icinga2_cfg, "\t\tend = $notification_2x->{'__I2CONVERT_NOTIFICATION_TIMES'}->{'end'}"); - dump_config_line($icinga2_cfg, "\t},"); - } - - dump_config_line($icinga2_cfg, ""); - dump_config_line($icinga2_cfg, "}"); - dump_config_line($icinga2_cfg, "\n"); -} - - -sub dump_timeperiod_2x { - my $icinga2_cfg = shift; - my $object_type = "object"; # object or template - my $timeperiod_2x = shift; - my $timeperiod_name = $timeperiod_2x->{'timeperiod_name'}; - - #say Dumper($timeperiod_2x); - if ($timeperiod_2x->{__I2CONVERT_IS_TEMPLATE} == 1) { - $object_type = "template"; - $timeperiod_name = $timeperiod_2x->{'__I2CONVERT_TEMPLATE_NAME'}; - } - - #################################################### - # start, inherit from template? - #################################################### - if (defined($timeperiod_2x->{'__I2CONVERT_USES_TEMPLATE'}) && $timeperiod_2x->{'__I2CONVERT_USES_TEMPLATE'} == 1) { - my $timeperiod_2x_templates = join '", "', @{$timeperiod_2x->{'__I2CONVERT_TEMPLATE_NAMES'}}; - dump_config_line($icinga2_cfg, "$object_type TimePeriod \"$timeperiod_name\" inherits \"$timeperiod_2x_templates\" {"); - } else { - dump_config_line($icinga2_cfg, "$object_type TimePeriod \"$timeperiod_name\" {"); - } - - # display_name is seperated at first position - if(defined($timeperiod_2x->{'display_name'})) { - dump_config_line($icinga2_cfg, "\tdisplay_name = \"$timeperiod_2x->{'display_name'}\","); - } - - dump_config_line($icinga2_cfg, "\tranges = {"); - - # dump all possible keys (there's no fixed string attr here) - foreach my $key (sort (keys %{$timeperiod_2x})) { - if ($key !~ /__I2CONVERT/ && - $key ne 'alias' && - $key ne 'name' && - $key ne 'timeperiod_name' && - $key ne 'display_name' && - $key ne 'use' - ) { - dump_config_line($icinga2_cfg, "\t\t\"$key\" \t= \"$timeperiod_2x->{$key}\","); - } - } - - dump_config_line($icinga2_cfg, "\t},"); - dump_config_line($icinga2_cfg, ""); - dump_config_line($icinga2_cfg, "}"); - dump_config_line($icinga2_cfg, "\n"); -} - -sub dump_group_2x { - my $icinga2_cfg = shift; - my $group_2x = shift; - my $group_name_attr = $group_2x->{__I2CONVERT_TYPE} . "_name"; - my $group_name = $group_2x->{$group_name_attr}; - my $group_type = ucfirst("$group_2x->{__I2CONVERT_TYPE}"); - $group_type =~ s/group/Group/; - - #say Dumper($group_2x); - - dump_config_line($icinga2_cfg, "object $group_type \"$group_name\" {"); - 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"); -} - -sub dump_command_2x { - my $icinga2_cfg = shift; - my $command_2x = shift; - my $command_name = $command_2x->{'__I2CONVERT_COMMAND_NAME'}; - my $command_line = $command_2x->{'__I2CONVERT_COMMAND_LINE'}; - my $command_type = ucfirst("$command_2x->{__I2CONVERT_COMMAND_TYPE}Command"); - my $object_type = "object"; - - #say Dumper($command_2x); - # skip used commands - if ($command_2x->{'__I2_CONVERT_NOTIFICATION_COMMAND_USED'} == 1) { - say "Skipping already processed notification command" . Dumper($command_2x); - return; - } - - if ($command_2x->{__I2CONVERT_IS_TEMPLATE} == 1) { - $object_type = "template"; - $command_name = $command_2x->{'__I2CONVERT_TEMPLATE_NAME'}; - } - - #################################################### - # start, inherit from template? - #################################################### - if (defined($command_2x->{'__I2CONVERT_USES_TEMPLATE'}) && $command_2x->{'__I2CONVERT_USES_TEMPLATE'} == 1) { - my $command_2x_templates = join '", "', @{$command_2x->{'__I2CONVERT_TEMPLATE_NAMES'}}; - dump_config_line($icinga2_cfg, "$object_type $command_type \"$command_name\" inherits \"$command_2x_templates\" {"); - } else { - dump_config_line($icinga2_cfg, "$object_type $command_type \"$command_name\" {"); - } - - #################################################### - # attributes - #################################################### - if(defined($command_2x->{'display_name'})) { - dump_config_line($icinga2_cfg, "\tdisplay_name = \"$command_2x->{'display_name'}\","); - } - - if(defined($command_line)) { - dump_config_line($icinga2_cfg, "\tcommand = \"$command_line\","); - } - - dump_config_line($icinga2_cfg, ""); - dump_config_line($icinga2_cfg, "}"); - dump_config_line($icinga2_cfg, "\n"); -} - - - -1; - -__END__ -# vi: sw=4 ts=4 expandtab : diff --git a/tools/configconvert/Icinga2/ImportIcinga1Cfg.pm b/tools/configconvert/Icinga2/ImportIcinga1Cfg.pm deleted file mode 100644 index 14fb91fb8..000000000 --- a/tools/configconvert/Icinga2/ImportIcinga1Cfg.pm +++ /dev/null @@ -1,309 +0,0 @@ - -=pod -/****************************************************************************** - * Icinga 2 * - * Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/) * - * * - * This program is free software; you can redistribute it and/or * - * modify it under the terms of the GNU General Public License * - * as published by the Free Software Foundation; either version 2 * - * of the License, or (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the Free Software Foundation * - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * - ******************************************************************************/ -=cut - - -package Icinga2::ImportIcinga1Cfg; - -push (@INC, 'pwd'); - -use strict; -use Data::Dumper; -use File::Find; -use Storable qw(dclone); - -use feature 'say'; - -#use Icinga2; -use Icinga2::Utils; - - -################################################################################ -# PARSE 1.x -################################################################################ - -sub get_key_from_icinga1_main_cfg { - my ($file, $key) = @_; - - my @key_arr = (); - - if ( !-f $file) { - print "cfg file $file does not exist!"; - return; - } - - if ( open ( my $fh, '<', $file ) ) { - while ( my $line = <$fh> ) { - chomp($line); - $line =~ s/#.*//; - if ($line =~ /^\s*$key=([^\s]+)/) { - push @key_arr, $1; # we may have multiple occurences - } - } - } - - return @key_arr; -} - -sub parse_icinga1_resource_cfg { - my $file = shift; - - my @cfg = Icinga2::Utils::slurp($file); - - my $global_macros = {}; - - foreach my $line (@cfg) { - $line = Icinga2::Utils::strip($line); - - # skip comments and empty lines - next if ($line eq "" || !defined($line) || $line =~ /^\s+$/); - next if ($line =~ /^[#;]/ || $line =~ /;.*/); - - #debug($line); - my ($macro_name, $macro_value) = split /=/, $line, 2; - $macro_name =~ /\$(.*)\$/; - $macro_name = $1; - - $global_macros->{$macro_name} = Icinga2::Utils::escape_str($macro_value); - } - - return $global_macros; - -} - -sub parse_icinga1_global_macros { - my $icinga1_cfg = shift; - - my ($icinga1_resource_file) = get_key_from_icinga1_main_cfg($icinga1_cfg, "resource_file"); - - # resource.cfg - my $global_macros = parse_icinga1_resource_cfg($icinga1_resource_file); - - # special attributes in icinga.cfg (admin_*) - my ($admin_pager) = get_key_from_icinga1_main_cfg($icinga1_cfg, "admin_pager"); - my ($admin_email) = get_key_from_icinga1_main_cfg($icinga1_cfg, "admin_email"); - - $global_macros->{'ADMINPAGER'} = $admin_pager; - $global_macros->{'ADMINEMAIL'} = $admin_email; - - return $global_macros; -} - -sub parse_icinga1_object_cfg { - my $cfg_obj = shift; - my $file = shift; - - my $obj = {}; #hashref - my $in_define = 0; - my $in_timeperiod = 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 $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 =~ /;.*/); - $line =~ s/[\r\n\s]+$//; - $line =~ s/^\s+//; - - # end of def - if ($line =~ /}(\s*)$/) { - $in_define = undef; - # store type for later - $cfg_obj->{'type_cnt'}->{$type} = $cfg_obj->{'type_cnt'}->{$type} + 1; - $type = ""; - next; - } - # start of def - elsif ($line =~ /define\s+(\w+)\s*{?(.*)$/) { - $type = $1; - $append = $2; - if ($type eq "timeperiod") { - $in_timeperiod = 1; - } else { - $in_timeperiod = 0; - } - - # save the type - $cfg_obj->{$type}->{$cfg_obj->{'type_cnt'}->{$type}}->{'__I2CONVERT_TYPE'} = $type; - - # we're ready to process entries - $in_define = 1; - # save the current type counter, being our unique key here - next; - } - # in def - elsif ($in_define == 1) { - - # first, remove the annoying inline comments after ';' - $line =~ s/\s*[;\#](.*)$//; - $inline_comment = $1; - - # then split it and save it by type->cnt->attr->val - #($attr, $val) = split (/\s+/, $line, 2); # important - only split into 2 elements - - # timeperiods require special parser - if ($in_timeperiod == 1) { - if ($line =~ /timeperiod_name/ || $line =~ /alias/ || $line =~ /exclude/) { - $line =~ m/([\w]+)\s*(.*)/; - $attr = Icinga2::Utils::strip($1); $val = Icinga2::Utils::strip($2); - } else { - $line =~ m/(.*)\s+([\d\W]+)/; - $attr = Icinga2::Utils::strip($1); $val = Icinga2::Utils::strip($2); - } - } else { - $line =~ m/([\w]+)\s*(.*)/; - $attr = Icinga2::Utils::strip($1); $val = Icinga2::Utils::strip($2); - } - # ignore empty values - next if (!defined($val)); - next if ($val eq ""); - #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"); - - # strip illegal object name characters, replace with _ - if ( ($attr =~ /name/ && $attr !~ /display_name/) || - $attr =~ /description/ || - $attr =~ /contact/ || - $attr =~ /groups/ || - $attr =~ /members/ || - $attr =~ /use/ || - $attr =~ /parents/ - ) { - $val = Icinga2::Utils::strip_object_name($val); - } - # treat 'null' (disable) as '0' - if ($val eq "null") { - $val = 0; - } - - $cfg_obj->{$type}->{$cfg_obj->{'type_cnt'}->{$type}}->{$attr} = $val; - - # ignore duplicated attributes, last one wins - } - else { - $in_define = 0; - } - - } - - #Icinga2::Utils::debug("========================================================"); - - return $cfg_obj; - -} - -# the idea is to reduce work load - get all the existing object relations (host->service) -# and have core 1.x already mapped that. we focus on getting the details when -# needed, but do not print the object without templates - only if there's no other way. -sub parse_icinga1_objects_cache { - my $icinga1_cfg = shift; - - # XXX not needed right now - return undef; - - # functions return array in case of multiple occurences, we'll take only the first one - my ($object_cache_file) = get_key_from_icinga1_main_cfg($icinga1_cfg, "object_cache_file"); - - if(!defined($object_cache_file)) { - print "ERROR: No objects cache file found in $icinga1_cfg! We'll need for final object conversion.\n"; - return -1; - } - - if(! -r $object_cache_file) { - print "ERROR: objects cache file '$object_cache_file' from $icinga1_cfg not found! We'll need it for final object conversion.\n"; - return -1; - } - - my $cfg_obj_cache = {}; - - $cfg_obj_cache = parse_icinga1_object_cfg($cfg_obj_cache, $object_cache_file); - - #say Dumper($cfg_obj_cache); - - return $cfg_obj_cache; - -} - -# parse all existing config object included in icinga.cfg, with all their templates -# and grouping tricks -sub parse_icinga1_objects { - my $icinga1_cfg = shift; - - my @cfg_files = get_key_from_icinga1_main_cfg($icinga1_cfg, "cfg_file"); - my @cfg_dirs = get_key_from_icinga1_main_cfg($icinga1_cfg, "cfg_dir"); - - sub find_icinga1_cfg_files { - my $file = $File::Find::name; - return if -d $file; - if ($file =~ /\.cfg$/) { - push @cfg_files, $file; - } - } - - foreach my $cfg_dir (@cfg_dirs) { - find(\&find_icinga1_cfg_files, $cfg_dir); - } - - # check if there was nothing to include - if (!@cfg_files) { - print "ERROR: $icinga1_cfg did not contain any object includes.\n"; - return -1; - } - #print "@cfg_files"; - - # now fetch all the config information into our global hash ref - my $cfg_objs = {}; - - foreach my $cfg_file (@cfg_files) { - print "Processing file '$cfg_file'...\n"; - $cfg_objs = parse_icinga1_object_cfg($cfg_objs, $cfg_file); - } - - #say Dumper($cfg_obj); - #say Dumper($cfg_obj->{'service'}); - - return $cfg_objs; -} - - -1; - -__END__ -# vi: sw=4 ts=4 expandtab : diff --git a/tools/configconvert/Icinga2/Utils.pm b/tools/configconvert/Icinga2/Utils.pm deleted file mode 100644 index ce84f433b..000000000 --- a/tools/configconvert/Icinga2/Utils.pm +++ /dev/null @@ -1,139 +0,0 @@ - -=pod -/****************************************************************************** - * Icinga 2 * - * Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/) * - * * - * This program is free software; you can redistribute it and/or * - * modify it under the terms of the GNU General Public License * - * as published by the Free Software Foundation; either version 2 * - * of the License, or (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the Free Software Foundation * - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * - ******************************************************************************/ -=cut - - -package Icinga2::Utils; - -use strict; -#use Icinga2; - -our $dbg_lvl; - -################################################################################ -# HELPER FUNCTIONS -################################################################################ - -sub strip { - my $str = shift; - - #strip trailing and leading whitespaces - $str =~ s/^\s+//; - $str =~ s/\s+$//; - - return $str; -} - -sub errlog { - my $err_lvl = shift; - my $log_str = shift; - - if ($err_lvl > 0) { - print STDERR color("red"), "$log_str\n";; - } else { - print "$log_str\n"; - } - -} - -sub escape_str { - my $str = shift; - - $str =~ s/"/\\"/g; - $str =~ s/\\\\"/\\"/g; - - return $str; -} -sub escape_shell_meta { - my $str = shift; - - $str =~ s/([;<>`'":&!#\$\[\]\{\}\(\)\*\|])/\\$1/g; - return $str; -} - -sub debug { - my $dbg_str = shift; - our $dbg_lvl; - - if ($dbg_lvl > 0) { - print "$dbg_str\n"; - } -} - -sub slurp { - my $file = shift; - - if ( -f $file ) { - open ( my $fh, "<", $file ) or die "Could not open $file: $!"; - return do { - <$fh>; - } - } elsif (! -r $file) { - print " ERROR: $file not readable. check permissions/user!\n" - } else { - print " ERROR: $file does not exist\n"; - } -} - -# stolen from http://stackoverflow.com/questions/7651/how-do-i-remove-duplicate-items-from-an-array-in-perl -sub uniq { - return keys %{{ map { $_ => 1 } @_ }}; -} - -sub str2arr_by_delim_without_excludes { - my $str = shift; - my $delim = shift; - my $sort = shift; - my $exclude = shift; - my @arr = (); - - @arr = map { s/^\s+//; s/\s+$//; $_ } - grep { !/^!/ } - split (/$delim/, $str); - - if ($sort == 1) { - @arr = sort (@arr); - } - - return @arr; -} - -sub strip_object_name { - my $obj_str = shift; - - #$obj_str =~ s/[`~!\\\$%\^&\*|'"<>\?,\(\)=:]/_/g; - $obj_str =~ s/[:]/_/g; - - return $obj_str; -} - -sub print_sorted_hash { - my $hash = shift; - - foreach my $key (sort keys %{$hash}) { - print "$key = $hash->{$key}\n"; - } -} - -1; - -__END__ -# vi: sw=4 ts=4 expandtab : diff --git a/tools/configconvert/icinga2_convert_v1_v2.pl b/tools/configconvert/icinga2_convert_v1_v2.pl deleted file mode 100755 index 5b9e6d7d7..000000000 --- a/tools/configconvert/icinga2_convert_v1_v2.pl +++ /dev/null @@ -1,243 +0,0 @@ -#!/usr/bin/perl - -=pod -/****************************************************************************** - * Icinga 2 * - * Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/) * - * * - * This program is free software; you can redistribute it and/or * - * modify it under the terms of the GNU General Public License * - * as published by the Free Software Foundation; either version 2 * - * of the License, or (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the Free Software Foundation * - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * - ******************************************************************************/ -=cut - -=head1 NAME - -icinga2_convert_v1_v2.pl - convert icinga 1.x config to icinga 2.x format - -=head1 SYNOPSIS - -icinga2_convert_v1_v2.pl -c - -o - [-v] - [-h] - [-V] - -Convert Icinga 1.x configuration to new Icinga 2.x configuration format. - -=head1 OPTIONS - -=over - -=item -c|--icingacfgfile - -Path to your Icinga 1.x main configuration file "icinga.cfg". - -=item -o|--outputcfgdir - -Directory to Icinga 2.x configuration output. - -=item --hosttmpl=