1- ---
2- layout : doctoc
3- title : Understanding Macros and How They Work
4- ---
1+ # Understanding Macros and How They Work
52
6- <span class =" glyphicon glyphicon-arrow-right " ></span > See Also: <a href =" macrolist.html " >List of Available Macros</a >
73
8-
9-
10- ### Macros
4+ ## Macros
115
126One of the main features that make Naemon so flexible is the ability to use macros in command
137definitions. Macros allow you to reference information from hosts, services, and other sources in your commands.
148
159
1610
17- ### Macro Substitution - How Macros Work
11+ ## Macro Substitution - How Macros Work
1812
1913Before Naemon executes a command, it will replace any macros it finds in the command definition
2014with their corresponding values. This macro substitution occurs for all types of commands
2115that Naemon executes - host and service checks, notifications, event handlers, etc.
2216
2317Certain macros may themselves contain other macros.
24- These include the $HOSTNOTES$, $HOSTNOTESURL$, $HOSTACTIONURL$, $SERVICENOTES$, $SERVICENOTESURL$, and $SERVICEACTIONURL$ macros.
18+ These include the ` $HOSTNOTES$ ` , ` $HOSTNOTESURL$ ` , ` $HOSTACTIONURL$ ` , ` $SERVICENOTES$ ` , ` $SERVICENOTESURL$ ` , and ` $SERVICEACTIONURL$ ` macros.
2519
2620
2721
28- ### Example 1: Host Address Macro
22+ ## Example 1: Host Address Macro
2923
3024When you use host and service macros in command definitions, they refer to values for the host or service
3125for which the command is being run. Let's try an example.
32- Assuming we are using a host definition and a < i > check_ping</ i > command defined like this:
26+ Assuming we are using a host definition and a ` check_ping ` command defined like this:
3327
34- <pre >
28+ <div class =" language- vp-adaptive-theme " >
29+ <pre class =" shiki shiki-themes github-light github-dark vp-code " tabindex =" 0 " >
30+ <code >
3531define host {
3632 host_name linuxbox
37- address <font color = " red " >192.168.1.2</font >
33+ address <span class = " text- red" >192.168.1.2</span >
3834 check_command check_ping
3935 ...
4036}
37+ </code >
4138
39+ <code >
4240define command {
4341 command_name check_ping
44- command_line /usr/lib/naemon/plugins/check_ping -H <font color = " red " >$HOSTADDRESS$</font > -w 100.0,90% -c 200.0,60%
42+ command_line /usr/lib/naemon/plugins/check_ping -H <span class = " text- red" >$HOSTADDRESS$</span > -w 100.0,90% -c 200.0,60%
4543}
44+ </code >
4645</pre >
46+ </div >
4747
4848the expanded/final command line to be executed for the host's check command would look like this:
4949
50- <pre >
51- /usr/lib/naemon/plugins/check_ping -H <font color =" red " >192.168.1.2</font > -w 100.0,90% -c 200.0,60%
50+ <div class =" language- vp-adaptive-theme " >
51+ <pre class =" shiki shiki-themes github-light github-dark vp-code " tabindex =" 0 " >
52+ <code >
53+ /usr/lib/naemon/plugins/check_ping -H <span class =" text-red " >192.168.1.2</span > -w 100.0,90% -c 200.0,60%
54+ </code >
5255</pre >
56+ </div >
57+
5358
5459Pretty simple, right?
5560The beauty in this is that you can use a single command definition to check an unlimited
@@ -58,50 +63,65 @@ host's address is automatically substituted in the command line before execution
5863
5964
6065
61- ### Example 2: Command Argument Macros
66+ ## Example 2: Command Argument Macros
6267
6368You can pass arguments to commands as well, which is quite handy if you'd like to keep
6469your command definitions rather generic. Arguments are specified in the object
6570(i.e. host or service) definition, by separating them from the command name with
66- exclamation points (! ) like so:
71+ exclamation points (` ! ` ) like so:
6772
68- <pre >
73+ <div class =" language- vp-adaptive-theme " >
74+ <pre class =" shiki shiki-themes github-light github-dark vp-code " tabindex =" 0 " >
75+ <code >
6976define service {
7077 host_name linuxbox
7178 service_description PING
72- check_command check_ping!<font color = " red " >200.0,80%</font >!<font color = " red " >400.0,40%</font >
79+ check_command check_ping!<span class = " text- red" >200.0,80%</span >!<span class = " text- red" >400.0,40%</span >
7380 ...
7481}
82+ </code >
7583</pre >
84+ </div >
85+
7686
7787In the example above, the service check command has two arguments (which can be referenced
78- with < a href = " macrolist.html#arg " > $ARGn$</ a > macros). The $ARG1$ macro will be
79- "<font color = " red " >200.0,80%</font >" and $ARG2$ will be "<font color = " red " >400.0,40%</font >"
88+ with [ ` $ARGn$ ` ] ( macrolist#arg ) macros). The ` $ARG1$ ` macro will be
89+ "<span class = " text- red" >200.0,80%</span >" and ` $ARG2$ ` will be "<span class = " text- red" >400.0,40%</span >"
8090(both without quotes). Assuming we are using the host definition given
81- earlier and a < i > check_ping</ i > command defined like this:
91+ earlier and a ` check_ping ` command defined like this:
8292
83- <pre >
93+ <div class =" language- vp-adaptive-theme " >
94+ <pre class =" shiki shiki-themes github-light github-dark vp-code " tabindex =" 0 " >
95+ <code >
8496define command {
8597 command_name check_ping
86- command_line /usr/lib/naemon/plugins/check_ping -H <font color = " red " >$HOSTADDRESS$</font > -w <font color = " red " >$ARG1$</font > -c <font color = " red " >$ARG2$</font >
98+ command_line /usr/lib/naemon/plugins/check_ping -H <span class = " text- red" >$HOSTADDRESS$</span > -w <span class = " text- red" >$ARG1$</span > -c <span class = " text- red" >$ARG2$</span >
8799}
100+ </code >
88101</pre >
102+ </div >
103+
89104
90105the expanded/final command line to be executed for the service's check command would look like this:
91106
92- <pre >
93- /usr/lib/naemon/plugins/check_ping -H <font color =" red " >192.168.1.2</font > -w <font color =" red " >200.0,80%</font > -c <font color =" red " >400.0,40%</font >
107+ <div class =" language- vp-adaptive-theme " >
108+ <pre class =" shiki shiki-themes github-light github-dark vp-code " tabindex =" 0 " >
109+ <code >
110+ /usr/lib/naemon/plugins/check_ping -H <span class =" text-red " >192.168.1.2</span > -w <span class =" text-red " >200.0,80%</span > -c <span class =" text-red " >400.0,40%</span >
111+ </code >
94112</pre >
113+ </div >
95114
96- {{ site.hint }}If you need to pass bang (!) characters in your command arguments, you can do so by escaping them with a backslash (\) .{{ site.end }}
115+ > [ !TIP]
116+ > If you need to pass bang (` ! ` ) characters in your command arguments, you can do so by escaping them with a backslash (` \ ` ).
97117
98118If you need to include backslashes in your command arguments, they should also be escaped with a backslash.
99119
100- ### On-Demand Macros
120+ ## On-Demand Macros
101121
102122Normally when you use host and service macros in command definitions, they refer to values
103123for the host or service for which the command is being run. For instance, if a host
104- check command is being executed for a host named "linuxbox", all the < a href = " macrolist.html " > standard host macros</ a > will
124+ check command is being executed for a host named "linuxbox", all the [ standard host macros] ( macrolist ) will
105125refer to values for that host ("linuxbox").
106126
107127If you would like to reference values for another host or service in a command (for which the command is
@@ -115,34 +135,43 @@ they should get their value. Here's the basic format for on-demand macros:
115135</ul >
116136
117137Replace <i >HOSTMACRONAME</i > and <i >SERVICEMACRONAME</i > with the name of one of the standard host
118- of service macros found < a href = " macrolist.html " > here</ a > .
138+ of service macros found [ here] ( macrolist ) .
119139
120140Note that the macro name is separated from the host or service identifier by a colon (:).
121141For on-demand service macros, the service identifier consists of both a host name and a
122142service description - these are separated by a colon (:) as well.
123143
124- {{ site.hint }}On-demand service macros can contain an empty host name field. In this case the name of the host associated with the service will automatically be used.{{ site.end }}
144+ > [ !TIP]
145+ > On-demand service macros can contain an empty host name field. In this case the name of the host associated with the service will automatically be used.
125146
126147Examples of on-demand host and service macros follow:
127148
128- <pre >
149+ <div class =" language- vp-adaptive-theme " >
150+ <pre class =" shiki shiki-themes github-light github-dark vp-code " tabindex =" 0 " >
151+ <code >
129152$HOSTDOWNTIME:myhost$ <--- On-demand host macro
130153$SERVICESTATEID:novellserver:DS Database$ <--- On-demand service macro
131154$SERVICESTATEID::CPU Load$ <--- On-demand service macro with blank host name field
155+ </code >
132156</pre >
157+ </div >
133158
134159On-demand macros are also available for hostgroup, servicegroup, contact, and contactgroup macros. For example:
135160
136- <pre >
161+ <div class =" language- vp-adaptive-theme " >
162+ <pre class =" shiki shiki-themes github-light github-dark vp-code " tabindex =" 0 " >
163+ <code >
137164$CONTACTEMAIL:john$ <--- On-demand contact macro
138165$CONTACTGROUPMEMBERS:linux-admins$ <--- On-demand contactgroup macro
139166$HOSTGROUPALIAS:linux-servers$ <--- On-demand hostgroup macro
140167$SERVICEGROUPALIAS:DNS-Cluster$ <--- On-demand servicegroup macro
168+ </code >
141169</pre >
170+ </div >
142171
143172
144173
145- ### On-Demand Group Macros
174+ ## On-Demand Group Macros
146175
147176You can obtain the values of a macro across all contacts, hosts, or services
148177in a specific group by using a special format for your on-demand macro declaration.
@@ -156,76 +185,85 @@ in an on-demand macro, like so:
156185</ul >
157186
158187Replace <i >HOSTMACRONAME</i >, <i >SERVICEMACRONAME</i >, and <i >CONTACTMACRONAME</i > with
159- the name of one of the standard host, service, or contact macros found < a href = " macrolist.html " > here</ a > .
188+ the name of one of the standard host, service, or contact macros found [ here] ( macrolist ) .
160189The delimiter you specify is used to separate macro values for each group member.
161190
162191For example, the following macro will return a comma-separated list of host state ids for hosts
163192that are members of the <i >hg1</i > hostgroup:
164193
165- <pre >
194+ <div class =" language- vp-adaptive-theme " >
195+ <pre class =" shiki shiki-themes github-light github-dark vp-code " tabindex =" 0 " >
196+ <code >
166197$HOSTSTATEID:hg1:,$
198+ </code >
167199</pre >
200+ </div >
168201
169202This macro definition will return something that looks like this:
170203
171- <pre >
204+ <div class =" language- vp-adaptive-theme " >
205+ <pre class =" shiki shiki-themes github-light github-dark vp-code " tabindex =" 0 " >
206+ <code >
1722070,2,1,1,0,0,2
208+ </code >
173209</pre >
210+ </div >
174211
175212
176213
177- ### Custom Variable Macros
214+ ## Custom Variable Macros
178215
179- Any < a href = " customobjectvars.html " > custom object variables</ a > that you define in host,
216+ Any [ custom object variables] ( customobjectvars ) that you define in host,
180217service, or contact definitions are also available as macros. Custom variable macros are named as follows:
181218
182- <ul >
183- <li >$_HOST<i >varname</i >$</li >
184- <li >$_SERVICE<i >varname</i >$</li >
185- <li >$_CONTACT<i >varname</i >$</li >
186- </ul >
219+ - ` $_HOSTvarname$ `
220+ - ` $_SERVICEvarname$ `
221+ - ` $_CONTACTvarname$ `
222+
187223
188224Take the following host definition with a custom variable called ` _MACADDRESS ` ...
189225
190- <pre >
226+ <div class =" language- vp-adaptive-theme " >
227+ <pre class =" shiki shiki-themes github-light github-dark vp-code " tabindex =" 0 " >
228+ <code >
191229define host {
192230 host_name linuxbox
193231 address 192.168.1.1
194- <font color = " red " >_MACADDRESS 00:01:02:03:04:05</font >
232+ <span class = " text- red" >_MACADDRESS 00:01:02:03:04:05</span >
195233 ...
196234}
235+ </code >
197236</pre >
237+ </div >
198238
199- The ` _MACADDRESS ` custom variable would be available in a macro called <font color = " red " >` $_HOSTMACADDRESS$ ` </font >.
200- More information on custom object variables and how they can be used in macros can be found < a href = " customobjectvars.html " > here</ a > .
239+ The ` _MACADDRESS ` custom variable would be available in a macro called <span class = " text- red" >` $_HOSTMACADDRESS$ ` </span >.
240+ More information on custom object variables and how they can be used in macros can be found [ here] ( customobjectvars ) .
201241
202242
203243
204- ### Macro Cleansing
244+ ## Macro Cleansing
205245
206246Some macros are stripped of potentially dangerous shell metacharacters before being substituted into commands to be executed.
207247Which characters are stripped from the macros depends on the setting of
208- the < a href = " configmain.html#illegal_macro_output_chars " > ` illegal_macro_output_chars ` </ a > directive.
248+ the [ ` illegal_macro_output_chars ` ] ( configmain#illegal_macro_output_chars ) directive.
209249The following macros are stripped of potentially dangerous characters:
210250
211- <ol >
212- <li ><a href =" macrolist.html#hostoutput " >$HOSTOUTPUT$</a ></li >
213- <li ><a href =" macrolist.html#longhostoutput " >$LONGHOSTOUTPUT$</a ></li >
214- <li ><a href =" macrolist.html#hostperfdata " >$HOSTPERFDATA$</a ></li >
215- <li ><a href =" macrolist.html#hostackauthor " >$HOSTACKAUTHOR$</a ></li >
216- <li ><a href =" macrolist.html#hostackcomment " >$HOSTACKCOMMENT$</a ></li >
217- <li ><a href =" macrolist.html#serviceoutput " >$SERVICEOUTPUT$</a ></li >
218- <li ><a href =" macrolist.html#longserviceoutput " >$LONGSERVICEOUTPUT$</a ></li >
219- <li ><a href =" macrolist.html#serviceperfdata " >$SERVICEPERFDATA$</a ></li >
220- <li ><a href =" macrolist.html#serviceackauthor " >$SERVICEACKAUTHOR$</a ></li >
221- <li ><a href =" macrolist.html#serviceackcomment " >$SERVICEACKCOMMENT$</a ></li >
222- </ol >
251+ 1 . [ ` $HOSTOUTPUT$ ` ] ( macrolist#hostoutput )
252+ 2 . [ ` $LONGHOSTOUTPUT$ ` ] ( macrolist#longhostoutput )
253+ 3 . [ ` $HOSTPERFDATA$ ` ] ( macrolist#hostperfdata )
254+ 4 . [ ` $HOSTACKAUTHOR$ ` ] ( macrolist#hostackauthor )
255+ 5 . [ ` $HOSTACKCOMMENT$ ` ] ( macrolist#hostackcomment )
256+ 6 . [ ` $SERVICEOUTPUT$ ` ] ( macrolist#serviceoutput )
257+ 7 . [ ` $LONGSERVICEOUTPUT$ ` ] ( macrolist#longserviceoutput )
258+ 8 . [ ` $SERVICEPERFDATA$ ` ] ( macrolist#serviceperfdata )
259+ 9 . [ ` $SERVICEACKAUTHOR$ ` ] ( macrolist#serviceackauthor )
260+ 10 . [ ` $SERVICEACKCOMMENT$ ` ] ( macrolist#serviceackcomment )
223261
224- Additionally, any macros that contain < a href = " customobjectvars.html " > custom variables</ a > are stripped for safety and security.
262+ Additionally, any macros that contain [ custom variables] ( customobjectvars ) are stripped for safety and security.
225263
226264
227265
228- ### Macros as Environment Variables
266+ ## Macros as Environment Variables
229267
230268Since Naemon macros are no longer available in the environment for performance reasons. Its
231269a huge waste of resources to calculate all macros all the time even if they are not used.
@@ -235,18 +273,22 @@ can edit your command to export specific macros.
235273
236274Example notification command:
237275
238- <pre >
276+ <div class =" language- vp-adaptive-theme " >
277+ <pre class =" shiki shiki-themes github-light github-dark vp-code " tabindex =" 0 " >
278+ <code >
239279define command {
240280 command_name my_old_notification_script
241- command_line <font color = " red " >NAGIOS_HOSTNAME="$HOSTNAME$"</font > /usr/local/bin/notifiy.pl ...
281+ command_line <span class = " text- red" >NAGIOS_HOSTNAME="$HOSTNAME$"</span > /usr/local/bin/notifiy.pl ...
242282}
283+ </code >
243284</pre >
285+ </div >
244286
245287This way you can define whatever environment macro you need and only those are calculated on runtime.
246288
247289
248290
249- ### Available Macros
291+ ## Available Macros
250292
251293A list of all the macros that are available in Naemon, as well as
252- a chart of when they can be used, can be found < a href = " macrolist.html " > here</ a > .
294+ a chart of when they can be used, can be found [ here] ( macrolist ) .
0 commit comments