While looking into Icinga 1.x configuration format, I found out that there are so many possible ways of doing things. One of them is to add a comma seperated host list as ‘host_name’ attribute to a service, in order to link it against multiple hosts (e.g. one ‘ping’ service to rule them all – and your editor, since the attribute line will be longer than 80 chars…).
This wouldn’t be much of a problem, unless you keep in mind, that trailing whitespaces are allowed (and the core does that in c!). Since my conversion script for Icinga2 is written in my favorite scripting language, namely Perl, I just want to use arrays instead of such string lists, attached to hash refs later on.
Others may put that into multiple lines, first splitting the string by the comma, pushing it into an array, and then stripping all white spaces in a while loop. The shorter way uses a sort-map-split magic, inspired by this post.
@host_name = sort (map { s/^s+//; s/s+$//; $_ } split (/,/, $obj_1x->{'host_name'}) );
You may wonder, on what purpose this information is stored in an array – later on, when unrolling all services as single unique object, I want to be able to link them against hosts. See the proposed draft version of the Icinga2 config format below. Source is this Icinga 1.x config file.
define host { host_name 3088localhost1 alias 3088localhost1 address 127.0.0.1 check_period 24x7 check_command 3088test contact_groups testconfig-group-admin notification_period workhours initial_state o check_interval 5.000000 retry_interval 1.000000 max_check_attempts 10 active_checks_enabled 1 passive_checks_enabled 1 obsess_over_host 1 event_handler_enabled 1 low_flap_threshold 0.000000 high_flap_threshold 0.000000 flap_detection_enabled 1 flap_detection_options o,d,u freshness_threshold 0 check_freshness 0 notification_options d,u,r notifications_enabled 1 notification_interval 120.000000 first_notification_delay 0.000000 stalking_options n process_perf_data 1 failure_prediction_enabled 1 retain_status_information 1 retain_nonstatus_information 1 } define host { host_name 3088localhost2 alias 3088localhost2 address 127.0.0.1 check_period 24x7 check_command 3088test contact_groups testconfig-group-admin notification_period workhours initial_state o check_interval 5.000000 retry_interval 1.000000 max_check_attempts 10 active_checks_enabled 1 passive_checks_enabled 1 obsess_over_host 1 event_handler_enabled 1 low_flap_threshold 0.000000 high_flap_threshold 0.000000 flap_detection_enabled 1 flap_detection_options o,d,u freshness_threshold 0 check_freshness 0 notification_options d,u,r notifications_enabled 1 notification_interval 120.000000 first_notification_delay 0.000000 stalking_options n process_perf_data 1 failure_prediction_enabled 1 retain_status_information 1 retain_nonstatus_information 1 } define service { service_description 3088service1 host_name 3088localhost1,3088localhost2 check_command 3088test2 use generic-service } define service { service_description 3088service2 host_name 3088localhost1,3088localhost2 check_command 3088test2 use generic-service }
must result basically into 2 hosts with 2 services. Please note that not all attributes are yet supported/migrated.
object Host "3088localhost1" { display_name = "3088localhost1", macros = { address = "127.0.0.1" }, hostgroups = [ "3088hg_A" ], check_interval = "5.000000m", retry_interval = "1.000000m", max_check_attempts = "10", services["3088service1"] = { templates = [ "generic-service" ], macros = { }, check_command = "/bin/echo "3088 service"", }, services["3088service2"] = { templates = [ "generic-service" ], macros = { }, check_command = "/bin/echo "3088 service"", }, } object Host "3088localhost2" { display_name = "3088localhost2", macros = { address = "127.0.0.1" }, hostgroups = [ "3088hg_B" ], check_interval = "5.000000m", retry_interval = "1.000000m", max_check_attempts = "10", services["3088service2"] = { templates = [ "generic-service" ], macros = { }, check_command = "/bin/echo "3088 service"", }, services["3088service1"] = { templates = [ "generic-service" ], macros = { }, check_command = "/bin/echo "3088 service"", }, }
You’ll also notice that the command object is now gone, and the command_line is converted as check_command, using the freely definable macros, if any – better example below.
template Service "proc:httpd" inherits "1066default-servicecheck" { macros = { ARG2 = "httpd", USER1 = "/usr/lib/nagios/plugins", ARG3 = "1", ARG1 = "$HOSTNAME$", ARG4 = "200", }, check_command = "$USER1$/check_procs -w $ARG1$ -c $ARG2$ -C ido2db", }