Using macros as environment variables in a plugin

The examples of commands we've seen so far have included all the information needed to execute the checks as command-line options, for example, specifying the host address for check_ping or the HTTP port for check_http. An alternative way to get the information required to execute checks is via environment variables. While this isn't enabled by default, it's a method of providing a large amount of context information to a plugin that may save some configuration effort. However, it's only suitable for specialized applications of Nagios Core and, because it's resource intensive, it may not scale very well, meaning it could be unsuitable for very busy Nagios Core servers.

In this example, we'll enable environment macros and then write a short wrapper script for check_ping that reads the address to PING from its executing environment, rather than from a command-line option.

Getting ready

You should have a Nagios Core 4.0 or newer server running with a few hosts and services configured already, and you should have some understanding of how command objects define the usage of plugins and of the macros defined by Nagios Core.

This is a reasonably advanced recipe that assumes some basic familiarity with environment variables in Unix-like operating systems.

How to do it…

We can enable environment macros and implement our command wrapper as follows:

  1. Change to the directory containing the main configuration file for Nagios Core. The default location is /usr/local/nagios/etc:
    # cd /usr/local/nagios/etc
    
  2. Edit the nagios.cfg file:
    # vi /usr/local/nagios/etc/nagios.cfg
    
  3. Find the definition for the enable_environment_macros option. It defaults to 0. Set it to 1 instead. Save the file and exit:
    enable_environment_macros=1
  4. Change to the directory containing the plugin binaries for Nagios Core. The default location is /usr/local/nagios/libexec:
    # cd /usr/local/nagios/libexec
    
  5. Edit a new file named check_ping_hostaddress:
    # vi check_ping_hostaddress
    
  6. Add the following code and then save the file and exit:
    #!/bin/sh
    exec /usr/local/nagios/libexec/check_ping -H "${NAGIOS_HOSTADDRESS:?}" "$@"
  7. Make the new file that is owned by the nagios group, and executable:
    # chown root.nagios check_ping_hostaddress
    # chmod 0770 check_ping_hostaddress
    
  8. Test the plugin by running it without and with the required NAGIOS_HOSTADDRESS environment variable set:
    # sudo -s -u nagios
    $ ./check_ping_hostaddress -w 100,20% -c 200,40%
    ./check_ping_hostaddress: 2: ./check_ping_hostaddress: NAGIOS_HOSTADDRESS: parameter not set or null
    $ NAGIOS_HOSTADDRESS=127.0.0.1 ./check_ping_hostaddress -w 100,20% -c 200,40%
    PING OK - Packet loss = 0%, RTA = 0.04 ms|rta=0.041000ms;100.000000;200.000000;0.000000 pl=0%;20;40;0
    
  9. Go back to the root shell, validate the configuration, and reload Nagios Core:
    $ exit
    # /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
    # /etc/init.d/nagios reload
    

We should now have a check_ping_hostaddress plugin available to us, which will allow us to use it in a command without having to pass $HOSTADDRESS$ in the command_line directive's value.

How it works...

We enabled macro environment variables in the Nagios Core configuration. This makes the set of macros normally useable in the command_line directive, such as $HOSTADDRESS$, available to the script as environment variables, with their names prefixed with NAGIOS_. We then wrote a wrapper script for the check_ping command that uses the $NAGIOS_HOSTADDRESS$ environment variable to define the address it should check, meaning that we don't have to specify the target host on the command line. It also copies any other arguments given to it to the check_ping call with the $@ expansion and throws an error if it doesn't have a value for NAGIOS_HOSTADDRESS.

We could add a command to use this plugin and a corresponding service check like this:

define command {
    command_name  check_ping_hostaddress
    command_line  $USER1$/check_ping_hostaddress -w 100,20% -c 200,40%
}
define service {
    use                  local-service
    host_name            localhost
    service_description  PING
    check_command        check_ping_hostaddress
}

We can get an idea of the set of variables passed to the plugin by having it dump its environment to a file somewhere within it to inspect its contents before the exec call:

export > /tmp/nagios-plugin-export

When the plugin is next executed by Nagios Core, we find a new file /tmp/nagios-plugin-export with contents that show the many macros now available to us as environment variables:

export LANG='en_US.UTF-8'
export LANGUAGE='en_US:en'
export NAGIOS_ADMINEMAIL='[email protected] '
export NAGIOS_ADMINPAGER='[email protected]'
export NAGIOS_ARG1=''
export NAGIOS_ARG10=''
export NAGIOS_ARG11=''
export NAGIOS_ARG12=''
...

Any of these variables can be used within the script. This includes the macros generated by any custom directives we might have declared for our host or service.

There's more...

Not all the Nagios macros are exported this way to plugins by default, even with enable_environment_macros turned on. For a complete breakdown of the macros available to you on the command line and in the environment, we can consult the relevant sections of the Nagios Core documentation at the following links:

It's important to note that the documentation for enable_environment_macros cautions that making all the macros available as environment variables is reasonably resource intensive and therefore might be inappropriate for large setups. We should only enable this option after careful consideration of whether we need it. It may be preferable to pass the required values explicitly in the command_line directive, as the example configuration does by default.

See also

  • The Creating a new command section in this chapter
  • Creating a new service, Chapter 1, Understanding Hosts, Services, and Contacts
  • The Writing a new plugin from scratch section in this chapter
  • The Implementing threshold checks in a plugin section in this chapter
  • Using custom directives, Chapter 9, Managing Configuration
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.189.228