Apply Service multiple if else's || general distinction between same checks with different "backends"

(Tony Boston) #1

Hi,

I have the following apply Service config for check_load

apply Service "[System] System Load" {
   import "generic-service"
   if (host.vars.os == "Windows") {
     check_command = "nscp-local-cpu"
   } else {
      if ("nrpe-host" in host.templates) {
         check_command = "load_by_nrpe"
         command_endpoint = NodeName
      }
   } else {
       if ("aok-host" in host.templates) {
         check_command = "load_by_ssh"
         command_endpoint = NodeName
       }
   } else {
       check_command = "load"
       command_endpoint = host.name
   }
   assign where host.vars.os
   ignore where host.vars.noagent && !"nrpe-host" in host.vars.templates
 }

Thing is, I get an error saying

Oct 02 16:44:58 icinga2-m01 icinga2[7045]: [2019-10-02 16:44:58 +0200] critical/config: Error: syntax error, unexpected else (T_ELSE)
Oct 02 16:44:58 icinga2-m01 icinga2[7045]: Location: in /etc/icinga2/zones.d/global-templates/services.conf: 95:4-95:7
Oct 02 16:44:58 icinga2-m01 icinga2[7045]: /etc/icinga2/zones.d/global-templates/services.conf(93):     command_endpoint = NodeName
Oct 02 16:44:58 icinga2-m01 icinga2[7045]: /etc/icinga2/zones.d/global-templates/services.conf(94):      }
Oct 02 16:44:58 icinga2-m01 icinga2[7045]: /etc/icinga2/zones.d/global-templates/services.conf(95):  } else {
Oct 02 16:44:58 icinga2-m01 icinga2[7045]:                                                             ^^^^
Oct 02 16:44:58 icinga2-m01 icinga2[7045]: /etc/icinga2/zones.d/global-templates/services.conf(96):    if ("aok-host" in host.templates
Oct 02 16:44:58 icinga2-m01 icinga2[7045]: /etc/icinga2/zones.d/global-templates/services.conf(97):      check_command = "load_by_ssh"

Are multiple else’s just not possible?

Second thing is - if anyone has a better idea how to handle basically same checks for different “backends” like nrpe, ssh, agent - I’d appreciate any hint

Thanks!
Tony

(Aflatto) #2

you may want to follow the docs on how to define a conditional with “else if” logic

if (a < 5) {
  a *= 7
} else if (a > 10) {
  a *= 5
} else {
  a *= 2
}
(Tony Boston) #3

I did follow the docs there and my question was if it is NOT possible to use more than one Else if here since the usage of my two Else if’s enda with an error

(Aflatto) #4

I am sorry to say that you did not follow the example:
This

 if (host.vars.os == "Windows") {
    check_command = "nscp-local-cpu"
    } else {
       if ("nrpe-host" in host.templates) {
          check_command = "load_by_nrpe"
          command_endpoint = NodeName
      }

Does not equal

if (a < 5) {
  a *= 7
} else if (a > 10) {
  a *= 5
} else {
  a *= 2
}

your syntax should be like this :

apply Service "[System] System Load" {
   import "generic-service"
   if (host.vars.os == "Windows") {
     check_command = "nscp-local-cpu"
   } else {
      if ("nrpe-host" in host.templates) {
         check_command = "load_by_nrpe"
         command_endpoint = NodeName
      } else if ("aok-host" in host.templates) {
         check_command = "load_by_ssh"
         command_endpoint = NodeName
   		} else {
       check_command = "load"
       command_endpoint = host.name
    }
  }
   assign where host.vars.os
   ignore where host.vars.noagent && !"nrpe-host" in host.vars.templates
 }
(Tony Boston) #5

alright, that did it - there was an { too much

cheers @aflatto