check_multi evaluates macros as Nagios does, e.g.
command[ network_ping ] = check_icmp -H $HOSTADDRESS$ -w 500,5% -c 1000,10%
The list of macros available in Nagios 3 can be viewed here.
For each command two values are stored after execution:
- Returncode (RC) in the variable:
$STATE_<command tag>
$ - Command output in the variable:
$<command tag>$
These values can be used as macros in suksessive commands.
This macro substitution only works directly on the Nagios Server.
If you are running checks on a remote host you can use ''-H localhost''. Or use the --set-Option to provide variables via command line:
check_multi -f `<xyz.cmd>` --set HOSTNAME=host123
Note: Macro names are case sensitive - e.g. -s HOSTNAME=host123
is something different than -s hostname=host123
.
In Nagios the first is to be referred as $HOSTNAME$
while the latter is $hostname$
.
Nagios ondemand macros (compare here) can be used by check_multi in order to reuse the states or any particular attributes from foreign host or service checks in the own check.
Nagios needs the configuration info which data should be passed at runtime.
To illustrate this I will mention a short example. Our service is monitoring a web appliation called myapp. But additionally the state of the attached firewall mywall should be included.
Two steps to do this:
- check_multi call:
check_multi -f myapp.cmd -s MYWALL_STATEID=$SERVICESTATEID:myhost:mywall$ -s MYWALL_OUTPUT="$SERVICEOUTPUT:myhost:mywall$"
- command file myapp.cmd:
# myapp.cmd
command [ myapp ] = check_http -H myhost -u 'http://myapp'
command [ mywall ] = ( echo "$MYWALL_OUTPUT$"; exit $MYWALL_STATEID$ )
Note: Take care of the quoting here: the output string has to be included in quotes while the stateid may not (numerical RC).
The combined call of OUTPUT and STATEID provides a 1:1 presentation of the results of the mywall service without any need to reexecute the command for this service any more.