In this recipe, you'll learn how to define custom user macros in resource files. This is good practice for strings used in check_command
definitions or other directives that are shared by more than one host or service. For example, take a look at the following example of writing the full path in a command_name
directive:
command_name=/usr/local/nagios/libexec/check_ssh $HOSTADDRESS$
In lieu of this, we could write the following line:
command_name=$USER1$/check_ssh $HOSTADDRESS$
As a result, if the location of the check_ssh
script changes, we only need to change the value of $USER1$
in the appropriate resource file to update all of its uses throughout the configuration.
Most of the macros in Nagios Core are defined automatically by the monitoring server, but up to 32 user-defined macros can be used as well in the $USERn$
form.
You will need to have a server running Nagios Core 4.0 or later and have access to the command line to change its configuration, in particular the resource.cfg
file.
In this example, we'll add a new macro, $USER2$
, to contain the SNMP community name snagmp
as is used for various check_snmp
requests.
We can define our user macro like so:
/usr/local/nagios/etc
. Edit the resource.cfg
file in this directory, as follows:# cd /usr/local/nagios/etc # vi resource.cfg
$USER2$
macro is not already defined in the file. If it is, we could define $USER3$
instead and so on.$USER2$=snagmp
/usr/local/nagios/etc/objects
. Run the following line:# cd /usr/local/nagios/etc/objects
$USER2$
, as shown in the following code. In our example, we might find various uses of the literal checksnmp
community string:define command { ... command_line $USER1$/check_snmp -H $HOSTADDRESS$ -C snagmp -o .1.3.6.1.2.1.1.5.0 -r $ARG1$ } define service { ... check_command check_snmp!snagmp }
We could swap these out to use the macro instead:
define command { ... command_line $USER1$/check_snmp -H $HOSTADDRESS$ -C $USER2$ -o .1.3.6.1.2.1.1.5.0 -r $ARG1$ } define service { ... check_command check_snmp!$USER2$ }
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg # /etc/init.d/nagios restart
With this done, the monitoring should work the same way as it did before, but we will now use macro expansion to centralize the configuration.
Before Nagios Core processes directives such as command_line
and check_command
, it will expand all the macros referenced, including the user-defined macros we added in the resource file resource.cfg
.
One common use for the $USERn$
macros is for defining the full path of directories where Nagios Core resources such as plugins or event handler scripts are located. In fact, the sample configuration included in Nagios Core defines $USER1$
in resource.cfg
as /usr/local/nagios/libexec
, which is the default location for plugin scripts and binaries.
It's worth noting that you can load more than one resource file by adding more resource_file
directives in /usr/local/nagios/etc/nagios.cfg
. For example, to load another file called resource-extra.cfg
, we could add the second line here:
resource_file=/usr/local/nagios/etc/resource.cfg
resource_file=/usr/local/nagios/etc/resource-extra.cfg
There's also a security benefit to using resource files; for sensitive information, we can prevent users other than nagios
from reading them by making them readable only to the nagios
user, as follows:
# cd /usr/local/nagios/etc
# chown nagios.nagios resource.cfg
# chmod 0600 resource.cfg
This makes it a decent way to store credentials such as usernames and passwords, for example, to be used in check_command
for MySQL, as is shown in the following code:
define command {
command_name check_mysql_secure
command_line check_mysql -H $HOSTADDRESS$ -u naguser -d nagdb -p $USER3$
}
3.15.10.137