Icinga / icinga2

The core of our monitoring platform with a powerful configuration language and REST API.
https://icinga.com/docs/icinga2/latest
GNU General Public License v2.0
2.03k stars 578 forks source link

default templates behave as if imported last instead of first as is documented #9220

Closed dwt closed 2 years ago

dwt commented 2 years ago

Describe the bug

default templates for services and apply service interaction is buggy.

The documentation on default templates states that: 'Default templates are imported before any other user-specified statement in an object definition is evaluated.'.

However, when default templates and service apply rules are combined, default templates are applied after all the rules of the apply rule. The effect is that the default template overrides settings done in the apply rule.

To Reproduce

Example:

template Service "default-service" default {
  retry_interval = 34s
}

apply Service "ssh-users" {
  assign where host.vars.os == "Linux" && host.vars.agent_type == "ssh"
  check_command = "users"
  import "ssh-service"
  import "sntl-service"
  retry_interval = 33s
}

This resolves retry_interval to 34.

Expected behavior

I expect to be able to override attributes in apply rules. In the given example, the attribute retry_interval should resolve to 33

Screenshots

image

Your Environment

dwt@nathan ~/Code/Projekte/sntl/opensource/icinga (git)-[master] % docker exec icinga_icinga2_1 icinga2 --version
icinga2 - The Icinga 2 network monitoring daemon (version: v2.13.0-197-g8016b013a)

Copyright (c) 2012-2022 Icinga GmbH (https://icinga.com/)
License GPLv2+: GNU GPL version 2 or later <https://gnu.org/licenses/gpl2.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

System information:
  Platform: Debian GNU/Linux
  Platform version: 11 (bullseye)
  Kernel: Linux
  Kernel version: 5.10.76-linuxkit
  Architecture: x86_64

Build information:
  Compiler: GNU 10.2.1
  Build host: e2e8a1183e4d
  OpenSSL version: OpenSSL 1.1.1k  25 Mar 2021

Application information:

General paths:
  Config directory: /etc/icinga2
  Data directory: /var/lib/icinga2
  Log directory: /var/log/icinga2
  Cache directory: /var/cache/icinga2
  Spool directory: /var/spool/icinga2
  Run directory: /run/icinga2

Old paths (deprecated):
  Installation root: /usr
  Sysconf directory: /etc
  Run directory (base): /run
  Local state directory: /var

Internal paths:
  Package data directory: /usr/share/icinga2
  State path: /var/lib/icinga2/icinga2.state
  Modified attributes path: /var/lib/icinga2/modified-attributes.conf
  Objects path: /var/cache/icinga2/icinga2.debug
  Vars path: /var/cache/icinga2/icinga2.vars
  PID path: /run/icinga2/icinga2.pid
Linux ops 5.10.76-linuxkit #1 SMP Mon Nov 8 10:21:19 UTC 2021 x86_64 GNU/Linux
dwt commented 2 years ago

I should have added that ordinary defined services (without apply) seem to evaluate the templates correctly.

dwt commented 2 years ago

This has the rather unfortunate side effect that I cannot configure the defaults for a service in the apply rule, which currently leads to many unnecessary warnings because I cannot set a longer timeout for remote check that is rather slow. :/ Is this a bug you guys can reproduce?

dwt commented 2 years ago

ping - @developers can you guys reproduce the issue? Am I missing something? Is this a well disguised feature?

dwt commented 2 years ago

ping @maintainers? Any info on this? Do you require additional input for me to reproduce?

julianbrost commented 2 years ago

Looks strange indeed and the current behavior makes no sense for me:

root@1eaa341b7fa9:/# cat -n /etc/icinga2/conf.d/test.conf
     1  template Service "test" default {
     2    retry_interval = 23s
     3  }
     4  
     5  object Host "test" {
     6    check_command = "dummy"
     7  }
     8  
     9  apply Service "test" {
    10    assign where true
    11    check_command = "dummy"
    12    retry_interval = 42s
    13  }
root@1eaa341b7fa9:/# icinga2 daemon -C
[2022-03-08 15:45:27 +0000] information/cli: Icinga application loader (version: v2.13.0-235-gbf5b90570)
[2022-03-08 15:45:27 +0000] information/cli: Loading configuration file(s).
[2022-03-08 15:45:27 +0000] information/ConfigItem: Committing config item(s).
[2022-03-08 15:45:27 +0000] information/ConfigItem: Instantiated 1 NotificationComponent.
[2022-03-08 15:45:27 +0000] information/ConfigItem: Instantiated 1 CheckerComponent.
[2022-03-08 15:45:27 +0000] information/ConfigItem: Instantiated 1 Service.
[2022-03-08 15:45:27 +0000] information/ConfigItem: Instantiated 3 Zones.
[2022-03-08 15:45:27 +0000] information/ConfigItem: Instantiated 1 Host.
[2022-03-08 15:45:27 +0000] information/ConfigItem: Instantiated 1 IcingaApplication.
[2022-03-08 15:45:27 +0000] information/ConfigItem: Instantiated 1 Endpoint.
[2022-03-08 15:45:27 +0000] information/ConfigItem: Instantiated 244 CheckCommands.
[2022-03-08 15:45:27 +0000] information/ScriptGlobal: Dumping variables to file '/var/cache/icinga2/icinga2.vars'
[2022-03-08 15:45:27 +0000] information/cli: Finished validating the configuration file(s).
root@1eaa341b7fa9:/# icinga2 object list -t Service
Object 'test!test' of type 'Service':
  % declared in '/etc/icinga2/conf.d/test.conf', lines 9:1-9:20
  * __name = "test!test"
  * action_url = ""
  * check_command = "dummy"
    % = modified in '/etc/icinga2/conf.d/test.conf', lines 11:3-11:25
  * check_interval = 300
  * check_period = ""
  * check_timeout = null
  * command_endpoint = ""
  * display_name = "test"
  * enable_active_checks = true
  * enable_event_handler = true
  * enable_flapping = false
  * enable_notifications = true
  * enable_passive_checks = true
  * enable_perfdata = true
  * event_command = ""
  * flapping_ignore_states = null
  * flapping_threshold = 0
  * flapping_threshold_high = 30
  * flapping_threshold_low = 25
  * groups = [ ]
  * host_name = "test"
    % = modified in '/etc/icinga2/conf.d/test.conf', lines 9:1-9:20
  * icon_image = ""
  * icon_image_alt = ""
  * max_check_attempts = 3
  * name = "test"
    % = modified in '/etc/icinga2/conf.d/test.conf', lines 9:1-9:20
  * notes = ""
  * notes_url = ""
  * package = "_etc"
    % = modified in '/etc/icinga2/conf.d/test.conf', lines 9:1-9:20
  * retry_interval = 23
    % = modified in '/etc/icinga2/conf.d/test.conf', lines 12:3-12:22
    % = modified in '/etc/icinga2/conf.d/test.conf', lines 2:3-2:22
  * source_location
    * first_column = 1
    * first_line = 9
    * last_column = 20
    * last_line = 9
    * path = "/etc/icinga2/conf.d/test.conf"
  * templates = [ "test", "test" ]
    % = modified in '/etc/icinga2/conf.d/test.conf', lines 9:1-9:20
    % = modified in '/etc/icinga2/conf.d/test.conf', lines 1:0-1:30
  * type = "Service"
  * vars = null
  * volatile = false
  * zone = ""
dwt commented 2 years ago

@julianbrost Do you guys plan to fix this behaviour? Can you suggest a workaround until this gets fixed?

I had the idea pull out the customization into it's own template to enforce it overriding the inherited template that way. Not pretty, but might work? Do you maybe see a better way?

yhabteab commented 2 years ago

Duplicate of #7914

dwt commented 2 years ago

7929 seems to be an attempt at fixing this, but seems stale / blocked. Are there known workarounds in the meantime?