From 6a3c1ec7aa3ea1123cb0aa54a2e59e45a313c9a3 Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Wed, 18 Sep 2013 11:39:38 +0200 Subject: [PATCH] Config Conversion: Allow to define external global templates referenced by all objects. tests: $ ./icinga2_convert_v1_v2.pl -c /etc/icinga/icinga.cfg -o conf/ --hosttmpl="hostbumstitmpl" --servicetmpl="servicebumstitmpl" --usertmpl="userbumstitmpl" --timeperiodtmpl="timeperiodbumstitmpl" --notificationtmpl="notificationbumstitmpl" --checkcommandtmpl="checkcommandbumstitmpl" --eventcommandtmpl="eventcommandbumstitmpl" --notificationcommandtmpl="notificationcommandbumstitmpl" refs #4697 --- tools/configconvert/Icinga2/Convert.pm | 27 ++++++++- tools/configconvert/Icinga2/Utils.pm | 2 +- tools/configconvert/icinga2_convert_v1_v2.pl | 59 +++++++++++++++++--- 3 files changed, 79 insertions(+), 9 deletions(-) diff --git a/tools/configconvert/Icinga2/Convert.pm b/tools/configconvert/Icinga2/Convert.pm index baa8afa0e..05a63dd4e 100644 --- a/tools/configconvert/Icinga2/Convert.pm +++ b/tools/configconvert/Icinga2/Convert.pm @@ -30,7 +30,7 @@ use File::Find; use Storable qw(dclone); use feature 'say'; -our $dbg_lvl = 1; +our $dbg_lvl; ################################################################################ ## Validation @@ -2846,7 +2846,32 @@ sub convert_2x { } ############################################################################ + # add custom templates to every existing object ############################################################################ + my @objects = ('host', 'service', 'user', 'notification', 'timeperiod', 'command'); + + foreach my $object (@objects) { + Icinga2::Utils::debug("Custom template for $object"); + #say "Custom template for $object"; + + foreach my $obj_2x_key (keys %{@$cfg_obj_2x{$object}}) { + my $obj_2x = @$cfg_obj_2x{$object}->{$obj_2x_key}; + + my $obj_template = "$object-template"; + if (defined($obj_2x->{'__I2CONVERT_COMMAND_TYPE'})) { + $obj_template = lc $obj_2x->{'__I2CONVERT_COMMAND_TYPE'} . "$object-template"; + } + + if(defined($icinga2_cfg->{'customtmpl'}->{$obj_template}) && $icinga2_cfg->{'customtmpl'}->{$obj_template} ne "") { + if (!defined($cfg_obj_2x->{$object}->{$obj_2x_key}->{'__I2CONVERT_TEMPLATE_NAMES'})) { + @{$cfg_obj_2x->{$object}->{$obj_2x_key}->{'__I2CONVERT_TEMPLATE_NAMES'}} = (); + } + push @{$cfg_obj_2x->{$object}->{$obj_2x_key}->{'__I2CONVERT_TEMPLATE_NAMES'}}, $icinga2_cfg->{'customtmpl'}->{$obj_template}; + $cfg_obj_2x->{$object}->{$obj_2x_key}->{'__I2CONVERT_USES_TEMPLATE'} = 1; + } + } + } + # export takes place outside again return $cfg_obj_2x; diff --git a/tools/configconvert/Icinga2/Utils.pm b/tools/configconvert/Icinga2/Utils.pm index 6f432721b..557b2b5e0 100644 --- a/tools/configconvert/Icinga2/Utils.pm +++ b/tools/configconvert/Icinga2/Utils.pm @@ -26,7 +26,7 @@ package Icinga2::Utils; use strict; #use Icinga2; -our $dbg_lvl = 1; +our $dbg_lvl; ################################################################################ # HELPER FUNCTIONS diff --git a/tools/configconvert/icinga2_convert_v1_v2.pl b/tools/configconvert/icinga2_convert_v1_v2.pl index 701937c18..df97553b2 100755 --- a/tools/configconvert/icinga2_convert_v1_v2.pl +++ b/tools/configconvert/icinga2_convert_v1_v2.pl @@ -83,18 +83,28 @@ my $version = "0.0.1"; # get command-line parameters our $opt; GetOptions( - "c|icingacfgfile=s" => \$opt->{icinga1xcfg}, - "o|outputcfgdir=s" => \$opt->{icinga2xoutputprefix}, - "v|verbose" => \$opt->{verbose}, - "h|help" => \$opt->{help}, - "V|version" => \$opt->{version} + "c|icingacfgfile=s" => \$opt->{icinga1xcfg}, + "o|outputcfgdir=s" => \$opt->{icinga2xoutputprefix}, + "hosttmpl=s" => \$opt->{hosttmpl}, + "servicetmpl=s" => \$opt->{servicetmpl}, + "usertmpl=s" => \$opt->{usertmpl}, + "notificationtmpl=s" => \$opt->{notificationtmpl}, + "timeperiodtmpl=s" => \$opt->{timeperiodtmpl}, + "checkcommandtmpl=s" => \$opt->{checkcommandtmpl}, + "notificationcommandtmpl=s" => \$opt->{notificationcommandtmpl}, + "eventcommandtmpl=s" => \$opt->{eventcommandtmpl}, + "tmpl=s" => \$opt->{tmpl}, + "tmpl=s" => \$opt->{tmpl}, + "v|verbose" => \$opt->{verbose}, + "h|help" => \$opt->{help}, + "V|version" => \$opt->{version} ); my $icinga1_cfg; my $icinga2_cfg = {}; my $conf_prefix = "./conf"; -my $verbose = 1; -our $dbg_lvl = 1; +my $verbose = 0; +our $dbg_lvl = 0; $icinga2_cfg->{'__I2EXPORT_DEBUG'} = 0; if(defined($opt->{icinga1xcfg})) { @@ -106,6 +116,7 @@ if(defined($opt->{icinga2xoutputprefix})) { if(defined($opt->{verbose})) { $verbose = $opt->{verbose}; $icinga2_cfg->{'__I2EXPORT_DEBUG'} = 1; + $dbg_lvl = 1; } if (defined $opt->{version}) { print $version."\n"; exit 0; } @@ -130,6 +141,40 @@ $icinga2_cfg->{'itl'}->{'checkcommand-template'} = "plugin-check-command"; $icinga2_cfg->{'itl'}->{'notificationcommand-template'} = "plugin-notification-command"; $icinga2_cfg->{'itl'}->{'eventcommand-template'} = "plugin-event-command"; +if (defined($opt->{hosttmpl})) { + $icinga2_cfg->{'customtmpl'}->{'host-template'} = $opt->{hosttmpl}; + print "Custom host template: '" . $icinga2_cfg->{'customtmpl'}->{'host-template'} . "'\n"; +} +if (defined($opt->{servicetmpl})) { + $icinga2_cfg->{'customtmpl'}->{'service-template'} = $opt->{servicetmpl}; + print "Custom service template: '" . $icinga2_cfg->{'customtmpl'}->{'service-template'} . "'\n"; +} +if (defined($opt->{usertmpl})) { + $icinga2_cfg->{'customtmpl'}->{'user-template'} = $opt->{usertmpl}; + print "Custom user template: '" . $icinga2_cfg->{'customtmpl'}->{'user-template'} . "'\n"; +} +if (defined($opt->{notificationtmpl})) { + $icinga2_cfg->{'customtmpl'}->{'notification-template'} = $opt->{notificationtmpl}; + print "Custom notification template: '" . $icinga2_cfg->{'customtmpl'}->{'notification-template'} . "'\n"; +} +if (defined($opt->{timeperiodtmpl})) { + $icinga2_cfg->{'customtmpl'}->{'timeperiod-template'} = $opt->{timeperiodtmpl}; + print "Custom timeperiod template: '" . $icinga2_cfg->{'customtmpl'}->{'timeperiod-template'} . "'\n"; +} +if (defined($opt->{checkcommandtmpl})) { + $icinga2_cfg->{'customtmpl'}->{'checkcommand-template'} = $opt->{checkcommandtmpl}; + print "Custom checkcommand template: '" . $icinga2_cfg->{'customtmpl'}->{'checkcommand-template'} . "'\n"; +} +if (defined($opt->{notificationcommandtmpl})) { + $icinga2_cfg->{'customtmpl'}->{'notificationcommand-template'} = $opt->{notificationcommandtmpl}; + print "Custom notificationcommand template: '" . $icinga2_cfg->{'customtmpl'}->{'notificationcommand-template'} . "'\n"; +} +if (defined($opt->{eventcommandtmpl})) { + $icinga2_cfg->{'customtmpl'}->{'eventcommand-template'} = $opt->{eventcommandtmpl}; + print "Custom eventcommand template: '" . $icinga2_cfg->{'customtmpl'}->{'eventcommand-template'} . "'\n"; +} + + my $type_cnt; -- 2.40.0