How to check if a constant or a global variables is defined

Hello,
In a distributed context, I would like to set on each satellites the domain of our mail2sms gateways in a constant or a global variable and use it in a NotificationCommand definition (in global-templates zone). In our case, not all satellites have a mail2sms gateway and for satellites without, I would like to use our main mail2sms gateway.
I’m the declaration of my notification_useremail variable of the NotificationCommand object, I’m looking for a way to test if the constant or global variables is defined to use it in this case, on fallback to default value if not.
I try with macro(“$SMSDomain$”) with a SMSDomain contants or global variables set in my satellites zones, but it seem to does not works (it’s always fallback to default domaine). Do you have any idea how I could do this ?

Thanks !

We’ve defined additional host templates for each site with its variables and added them to the hosts accordingly. We call them site templates and every host has its site specific variables.

Thank and yes, host template and variable always set could be a solution, but I hoping a solution without to have to use different template for each site. Their is no way to test if a variable is set or not ?

I had the same issue and my solution was to use a “try/except” construct.
In my use case i needed a check command which works for hosts and for services and takes it’s parameters from either host.vars.parameters or service.vars.parameters. If none of the two are set, the check should use some default values.
So the CheckCommand definition looks like this:

 object CheckCommand "Ping_AllParams" {
    import "plugin-check-command"
   command = [ PluginDir + "/check_ping" ]
    arguments += {
        _X = {
            skip_key = true
            value = {{
              var params = []
              try { params = host.vars.parameters || service.vars.parameters } except {}
              if(! params) {
            	params = [ "-H", "$address$", "-w", "50,50%", "-c", "100,90%" ]
              }
              var res = []
              for( var elem in params) {
                res.add(macro(elem))
              }
              return res
            }}
        }
    }
 }

The important part is this:

try { params = host.vars.parameters || service.vars.parameters } except {}

So, variable “params” will be set to either the contents of host.vars.parameters or service.vars.parameters or will be “null”, if none of them exists.
After that, i’ll check for “null” and set the defaults.

Hope this helps…

1 Like