abd
(abd)
September 18, 2023, 12:30pm
1
Hi to the community,
Am facing a problem.
I want to access a host variable from the service définition.
Host:
object Host "host1.domain" {
import "generic-host"
display_name = "host1"
address = "xx.xx.xx.xx"
vars.host_systemd_units = [
"sshd",
"sssd",
"rsyslog",
"chronyd" ]
vars.my_var_check_service_sshd = 1
}
Service:
apply Service for (config in host.vars.host_systemd_units) {
import "generic-service"
check_command = "check_service"
name = "Check Service : "+config
vars.service = config
command_endpoint = host.vars.client_endpoint
if ( "$host.vars.my_var_check_service_"+config+"$" == 1 ) {
vars.new_var = "My_value1"
} else {
vars.new_var = "My_value2"
}
assign where host.vars.host_systemd_units && host.vars.client_endpoint
}
Unfortunately, as a result, the “service.vars.new_var” is “My Value2”. It should be “My Value1”.
I think Icinga is comparing the string “host.vars.my_var_check_service_sshd” to ‘1’.
Can some one help me make this work?
Thank you in advance.
Best regards,
A.CHEGROUNI
I think this concatenation will not be evaluated there.
I have no idea if that works but you could try:
if ( macro("$host.vars.my_var_check_service_"+config+"$") == 1 ) {
abd
(abd)
September 18, 2023, 1:10pm
3
Hi @moreamazingnick
Thanks for the reply.
I already tried that and it don’t work.
[2023-09-18 15:06:11 +0200] critical/config: Error: Invalid field access (for value of type 'Service'): 'macro'
Location: in /etc/icinga2/zones.d/global-templates/services.conf: 2574:7-2574:63
/etc/icinga2/zones.d/global-templates/services.conf(2574): if ( macro("$host.vars.my_var_check_service_”+config+“$") == 1 ) {
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/etc/icinga2/zones.d/global-templates/services.conf(2575): vars.new_var = "My_value1"
/etc/icinga2/zones.d/global-templates/services.conf(2576): } else {
[2023-09-18 15:06:11 +0200] critical/config: 876 errors
these are the wrong double quotes
but you can do something like:
if (config =="sshd" && host.vars.my_var_check_service_sshd == 1 ) {
and so on.
it is not generic but it will work
abd
(abd)
September 18, 2023, 1:35pm
5
Hi
Thank you again.
It don’t work either:
[2023-09-18 15:29:13 +0200] critical/config: Error: Invalid field access (for value of type 'Service'): 'macro'
Location: in /etc/icinga2/zones.d/global-templates/services.conf: 2574:26-2574:76
/etc/icinga2/zones.d/global-templates/services.conf(2574): if ( config =="sshd" && macro("$host.vars.my_var_check_service_sshd$") == 1 ) {
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/etc/icinga2/zones.d/global-templates/services.conf(2575): vars.new_var = "My_value1"
/etc/icinga2/zones.d/global-templates/services.conf(2576): } else {
[2023-09-18 15:29:13 +0200] critical/config: 170 errors
sorry i edited my post to:
if (config =="sshd" && host.vars.my_var_check_service_sshd == 1 ) {
abd
(abd)
September 18, 2023, 1:42pm
7
Oh that works.
But it’s not what am tempting to do
abd
(abd)
September 18, 2023, 1:46pm
8
In have 1130 Services.
And the hosts can have up to 20services monitored.
ok so you need 20 if blocks
abd
(abd)
September 18, 2023, 2:06pm
10
Yes
that would be ridiculous
abd
(abd)
September 19, 2023, 6:46am
11
Does anyone know what this error mean?
abd
(abd)
September 19, 2023, 10:04am
13
I have this error ( Error: Invalid field access (for value of type 'Service'): 'macro'
), when i use the macro fonction. as @moreamazingnick suggested:
I think this concatenation will not be evaluated there.
I have no idea if that works but you could try:
if ( macro("$host.vars.my_var_check_service_"+config+"$") == 1 ) {
Reread my original post again, not the quote, I told you, I edited the post
abd
(abd)
September 19, 2023, 1:52pm
15
Am sorry but i really don’t think it’s a quote problem.
As you can see i tride the macro function differently but i hade the same error.
Hi
Thank you again.
It don’t work either:
[2023-09-18 15:29:13 +0200] critical/config: Error: Invalid field access (for value of type 'Service'): 'macro'
Location: in /etc/icinga2/zones.d/global-templates/services.conf: 2574:26-2574:76
/etc/icinga2/zones.d/global-templates/services.conf(2574): if ( config =="sshd" && macro("$host.vars.my_var_check_service_sshd$") == 1 ) {
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^…
that doesn’t work
you can access the var by
host.vars.my_var_check_service_sshd
without the macro part
something like that:
if (config ==“sshd” && host.vars.my_var_check_service_sshd == 1 ) {
abd
(abd)
September 19, 2023, 2:19pm
17
Yes i know that.
But it’s not what am attempting to do.
I need to accès this variable dynamically.
yhabteab
(Yonas Habteab)
September 19, 2023, 2:30pm
18
Hi, try to use something like this instead:
if (host.vars["my_var_check_service_"+config] == 1) {
...
1 Like
abd
(abd)
September 19, 2023, 5:31pm
19
@yhabteab thank you so much.
that works perfectly.