I took a step back yesterday and yes, I did make this too complicated and came 
up with a similar solution too. Yours is a little more interesting however and 
may need to adjust my monitoring check strategy to incorporate.

Thanks for the direction!!

On January 18, 2016 at 2:43:53 AM, Gunnar Beutner (gunnar.beut...@netways.de) 
wrote:

Hello,  

The if/else construct you’re using is evaluated when the CheckCommand is 
instantiated - i.e. when Icinga starts and parses its config. „vars.servertype“ 
actually refers to the CheckCommand’s custom variables.  

If you want the if/else to be evaluated for each check you’re going to have to 
use a lambda function, like so:  

object CheckCommand "check_timezone" {  
import „nrpe-common"  

command = [ PluginDir + "/check_nrpe", "-H", "$host.address$", "-c", 
"check_timezone" ]  

arguments += {  
"-a" = {{  
if (host.vars.servertype in [ "cassandra", "rabbit", "mq", "redis" ]) {  
var tz_permanent = "UTC"  
var tz_seasonal_comma = "UTC"  
var localtime_hash = "c79354b8dbee09e62bbc3fb544853283"  
} else {  
var tz_permanent = "America/New_York"  
var tz_seasonal_comma = "EST,EDT"  
var localtime_hash = "e4ca381035a34b7a852184cc0dd89baa"  
}  

return tz_permanent + " " + tz_seasonal_comma + " " + localtime_hash  
}}  
"-t" = "$timeout$"  
}  
}  



Does that feel overly complicated to you? Because… well, it should. Ideally you 
should keep as much domain-specific knowledge out of the CheckCommand 
definitions as possible.  

A far better - and easier - way would be to use host templates:  

template Host „utc-server“ {  
vars.tz_permanent = "UTC"  
vars.tz_seasonal_comma = "UTC"  
vars.localtime_hash = "c79354b8dbee09e62bbc3fb544853283"  
}  

template Host „est-server“ {  
vars.tz_permanent = "America/New_York"  
vars.tz_seasonal_comma = "EST,EDT"  
vars.localtime_hash = "e4ca381035a34b7a852184cc0dd89baa"  
}  

template Host „mq-server“ {  
import „utc-server“  

vars.servertype = „mq“  
}  

/* Additional templates for cassandra, rabbit, redis, etc. */  

object Host „mq-server-001“ {  
import „mq-server"  
}  


Kind regards,  
Gunnar Beutner  

On 16/01/16 19:32, "icinga-users on behalf of Rob DeSanno" 
<icinga-users-boun...@lists.icinga.org on behalf of rdesa...@icloud.com> wrote: 
 

>  
>I’m trying to wrap my head around using if/else conditions for my 
>CheckCommands but I can’t seem to get it to work. In this example, most of my 
>servers timezone is set to EST whereas only a handful (cassandra, rabbit, mq, 
>and redis) are set to UTC.  
>  
>  
>How can I reconfigure this check to pass the correct variables based on the 
>servertype defined in hosts.conf? I suspect that these checks are not picking 
>up either set of variables but having a hard time figuring out how it should 
>be set.  
>  
>  
>Any thoughts?  
>  
>  
>  
>  
>object CheckCommand "check_timezone" {  
> import "nrpe-common"  
> command = [ PluginDir + "/check_nrpe", "-H", "$host.address$", "-c", 
> "check_timezone" ]  
>  
>  
> arguments += {  
> "-a" = "$tz_permanent$ $tz_seasonal_comma$ $localtime_hash$"  
> "-t" = "$timeout$"  
> }  
>  
>  
> if ( vars.servertype == "cassandra" || vars.servertype == "rabbit" || 
> vars.servertype == “mq" || vars.servertype == "redis" ) {  
> vars.tz_permanent = "UTC"  
> vars.tz_seasonal_comma = "UTC"  
> vars.localtime_hash = "c79354b8dbee09e62bbc3fb544853283"  
> } else {  
> vars.tz_permanent = "America/New_York"  
> vars.tz_seasonal_comma = "EST,EDT"  
> vars.localtime_hash = "e4ca381035a34b7a852184cc0dd89baa"  
> }  
>}  
>  
>  
>apply Service "check_timezone" {  
> import "generic-service"  
> display_name = "timezone"  
> check_command = "check_timezone"  
>  
>  
> vars += {  
> "timeout" = "30"  
> }  
>  
>  
> assign where host.vars.servertype in [ … ]  
>  
>  

--   
Gunnar Beutner  
Senior Developer  

NETWAYS GmbH | Deutschherrnstr. 15-19 | D-90429 Nuernberg  
Tel: +49 911 92885-0 | Fax: +49 911 92885-77  
CEO: Julian Hein, Bernd Erk | AG Nuernberg HRB18461  
http://www.netways.de | gunnar.beut...@netways.de  

** OSDC 2016 - April - netways.de/osdc **  
** OSBConf 2016 - September - osbconf.org **  
_______________________________________________
icinga-users mailing list
icinga-users@lists.icinga.org
https://lists.icinga.org/mailman/listinfo/icinga-users
_______________________________________________
icinga-users mailing list
icinga-users@lists.icinga.org
https://lists.icinga.org/mailman/listinfo/icinga-users

Reply via email to