Mixed static and REST API config - issue?

I configured a bunch of hosts statically via config scripts. Then I used an external script to query ESX hosts to pull the virtual machine information from each ESX host and fed it into Icinga using REST API: For all virtual machines I added a host variable
vars.esxhost = host name

The Icinga web GUI easily shows these variables have been set correctly for each host.

Then I added in a config file:

object HostGroup “vm” {
display_name = “Virtual Machines”
assign where host.vars.esxhost
}

However, this hostgroup remains empty. Why?
Thx for help

Hello,
Can you provide output for following commands ?

icinga2 object list --type Host --name "[put here one host name which is supposed to work]"
icinga2 object list --type HostGroup --name "vm"

Output of first command. Variable esxhost ist not shown.

root@icinga:/etc/icinga2/conf.d# icinga2 object list --type Host --name listig
Object ‘listig’ of type ‘Host’:
% declared in ‘/etc/icinga2/conf.d/hosts/unix/listig.conf’, lines 1:0-1:19

  • __name = “listig”
  • action_url = “”
  • address = “10.192.2.91”
    % = modified in ‘/etc/icinga2/conf.d/hosts/unix/listig.conf’, lines 3:2-3:24
  • address6 = “”
  • check_command = “hostalive”
    % = modified in ‘/etc/icinga2/conf.d/templates.conf’, lines 19:3-19:29
  • check_interval = 60
    % = modified in ‘/etc/icinga2/conf.d/templates.conf’, lines 16:3-16:21
  • check_period = “”
  • check_timeout = null
  • command_endpoint = “”
  • display_name = “listig”
  • enable_active_checks = true
  • enable_event_handler = true
  • enable_flapping = false
  • enable_notifications = true
  • enable_passive_checks = true
  • enable_perfdata = true
  • event_command = “”
  • flapping_threshold = 0
  • flapping_threshold_high = 30
  • flapping_threshold_low = 25
  • groups = [ ]
  • icon_image = “”
  • icon_image_alt = “”
  • max_check_attempts = 3
    % = modified in ‘/etc/icinga2/conf.d/templates.conf’, lines 15:3-15:24
  • name = “listig”
  • notes = “”
  • notes_url = “”
  • package = “_etc”
  • retry_interval = 30
    % = modified in ‘/etc/icinga2/conf.d/templates.conf’, lines 17:3-17:22
  • source_location
    • first_column = 0
    • first_line = 1
    • last_column = 19
    • last_line = 1
    • path = “/etc/icinga2/conf.d/hosts/unix/listig.conf”
  • templates = [ “listig”, “tpl_unix_host”, “generic-host” ]
    % = modified in ‘/etc/icinga2/conf.d/hosts/unix/listig.conf’, lines 1:0-1:19
    % = modified in ‘/etc/icinga2/conf.d/hosts/unix/tpl_unix_host.conf’, lines 1:0-1:28
    % = modified in ‘/etc/icinga2/conf.d/templates.conf’, lines 14:1-14:28
  • type = “Host”
  • vars
    • apps = “master apache2 mysqld sympa_msg.pl bulk.pl archived.pl bounced.pl task_manager.pl”
      % = modified in ‘/etc/icinga2/conf.d/hosts/unix/listig.conf’, lines 5:2-5:96
    • os = “unix”
      % = modified in ‘/etc/icinga2/conf.d/hosts/unix/tpl_unix_host.conf’, lines 4:2-4:17
    • smtp
      • ports = [ 25 ]
        % = modified in ‘/etc/icinga2/conf.d/hosts/unix/listig.conf’, lines 7:9-7:32
    • snmp = true
      % = modified in ‘/etc/icinga2/conf.d/hosts/unix/listig.conf’, lines 8:9-8:24
    • sshport = 22
      % = modified in ‘/etc/icinga2/conf.d/hosts/unix/tpl_unix_host.conf’, lines 5:2-5:18
    • wusers = 3
      % = modified in ‘/etc/icinga2/conf.d/hosts/unix/listig.conf’, lines 6:2-6:16
    • www
      • sympa
        • display_name = “Sympa Admin Website”
          % = modified in ‘/etc/icinga2/conf.d/hosts/unix/listig.conf’, lines 10:9-10:59
        • pattern = “Mailing lists service”
          % = modified in ‘/etc/icinga2/conf.d/hosts/unix/listig.conf’, lines 12:9-12:56
        • url = “https://listig.some.domain/wws
          % = modified in ‘/etc/icinga2/conf.d/hosts/unix/listig.conf’, lines 11:9-11:62
  • volatile = false
  • zone = “”

Output of second command:

root@icinga:/etc/icinga2/conf.d# icinga2 object list --type HostGroup --name “vm”
Object ‘vm’ of type ‘HostGroup’:
% declared in ‘/etc/icinga2/conf.d/groups/hgroups.conf’, lines 24:1-24:21

  • __name = “vm”
  • action_url = “”
  • display_name = “Virtual Machines”
    % = modified in ‘/etc/icinga2/conf.d/groups/hgroups.conf’, lines 25:3-25:35
  • groups = null
  • name = “vm”
  • notes = “”
  • notes_url = “”
  • package = “_etc”
  • source_location
    • first_column = 1
    • first_line = 24
    • last_column = 21
    • last_line = 24
    • path = “/etc/icinga2/conf.d/groups/hgroups.conf”
  • templates = [ “vm” ]
    % = modified in ‘/etc/icinga2/conf.d/groups/hgroups.conf’, lines 24:1-24:21
  • type = “HostGroup”
  • vars = null
  • zone = “”

However the WebUI shows:grafik

Thanks for your input, as i suspected, your hostgroups is not applying to your host because it is missing the var esxhost in vars in some way in the configuration.

You can directly define it in your static configuration host file (/etc/icinga2/conf.d/hosts/unix/listig.conf)

Did you affected the current esxhost to your hosts throught api ? if so, did you restarted icinga after this ?

Templates objects dont reapply at runtime for modifications done on the API, it may be the case too for HostGroups.

Hello @Hutzli
The where statement is you HostGroup object is bad. Could you try this where statement?

object HostGroup “vm” {
display_name = “Virtual Machines”
assign where match ( "*", host.vars.esxhost )
}

EDITED: Sorry I did not read your next post. The variable is missing when running the the object list command. The where statement will not work without the variable showing under the host object command first.

Regards
Alex

@someone:
Yes I did a: icinga2 daemon --validate && systemctl reload icinga2

I just tried systemctl restart icinga2 but with no change.

In /var/lib/icinga/modified-attributes I find lines of the kind:
var obj = get_object(“Host”, “listig”)
if (obj) {
obj.modify_attribute(“vars.esxhost”, “qux”)
obj.version = 1596634559.644526
}

I have the assumption that during startup these modifications are applied too late, i.e. AFTER apply rules for hostgroups were already executed. Is that possible?

My problem seems somewhat related to this one.
However then reloading/restarting icinga should fix it - that’s not the case, however.

I should add version information:
root@icinga:/etc/icinga2/conf.d# icinga2 --version
icinga2 - The Icinga 2 network monitoring daemon (version: r2.11.3-1)

Copyright © 2012-2020 Icinga GmbH (https://icinga.com/)
License GPLv2+: GNU GPL version 2 or later http://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: Ubuntu
Platform version: 18.04.4 LTS (Bionic Beaver)
Kernel: Linux
Kernel version: 4.15.0-112-generic
Architecture: x86_64

Build information:
Compiler: GNU 8.3.0
Build host: runner-LTrJQZ9N-project-298-concurrent-0

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

I think you indeed fall in that case, you should remove your modification made from api and directly make it in the static config file /etc/icinga2/conf.d/hosts/unix/listig.conf, actually the doc tell it explicitely :
https://icinga.com/docs/icinga2/latest/doc/12-icinga2-api/#modifying-objects

OK, it’s a pity but it seems indeed be covered by this non-implemented feature…
Thx for pointing me to that piece of documentation.