Creating a new command

In this recipe, we'll create a new command for a plugin that was just installed into the /usr/local/nagios/libexec directory in the Nagios Core server. This will define the way in which Nagios Core should use the plugin and thereby allow it to be used as part of a service definition.

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 a plugin installed for which you'd like to define a new command so that you can use it as part of a service definition. In this instance, we'll define a command for an installed check_rsync plugin.

How to do it...

We can define a new command in our configuration as follows:

  1. Change to the directory containing the command configuration files for Nagios Core. The default file is commands.cfg, located in /usr/local/nagios/etc/objects:
    # cd /usr/local/nagios/etc/objects
    
  2. Edit the commands.cfg file:
    # vi commands.cfg
    
  3. At the bottom of the file, add the following command definition:
    define command {
        command_name  check_rsync
        command_line  $USER1$/check_rsync -H $HOSTADDRESS$
    }
  4. Validate the configuration and restart the Nagios Core server:
    # /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
    # /etc/init.d/nagios reload
    

    If the validation passes and the server restarts successfully, we should now be able to use the check_rsync command in a service definition.

How it works...

The configuration we added to the preceding commands.cfg file defines a new command called check_rsync, which specifies a method for using the plugin of the same name to monitor a service. This enables us to use check_rsync as a value for the check_command directive in a service declaration, which might look like this:

define service {
    use                  generic-service
    host_name            troy.example.net
    service_description  RSYNC
    check_command        check_rsync
}

Only two directives are required for command definitions, and we've defined both:

  • command_name: This defines the unique name with which we can reference the command when we use it in host or service definitions.
  • command_line: This defines the command line that should be executed by Nagios Core to make the appropriate check.

This particular command line also uses two macros:

  • $USER1$: This expands to a string value, in our case /usr/local/nagios/libexec, the location of the plugin binaries, including check_rsync. This is defined in the sample configuration in the /usr/local/nagios/etc/resource.cfg file.
  • $HOSTADDRESS$: This expands to a string value, the address of any host for which this command is used as a host or service definition.

So, if we used the command in a service checking, the rsync(1) server on troy.example.net, the completed command might look like this:

$ /usr/local/nagios/libexec/check_rsync -H troy.example.net

We could run this straight from the command line as the nagios user to see what kind of results it returns:

$ /usr/local/nagios/libexec/check_rsync -H troy.example.net
OK: Rsync is up

There's more...

A plugin can be used for more than one command. If we had a particular rsync(1) module that we wanted to check named backup, we could write another command called check_rsync_backup as follows, perhaps added to commands.cfg:

define command {
    command_name  check_rsync_backup
    command_line  $USER1$/check_rsync -H $HOSTADDRESS$ -m backup
}

If one or more of our rsync(1) servers were running on an alternate port, say port 5873, we could define a separate command check_rsync_altport for that:

define command {
    command_name  check_rsync_altport
    command_line  $USER1$/check_rsync -H $HOSTADDRESS$ -p 5873
}

Commands can thus be defined as precisely as we need them to be. We explore this in more detail in the Customizing an existing command recipe in this chapter.

See also

  • The Installing a plugin section in this chapter
  • The Customizing an existing command section in this chapter
  • Creating a new service, Chapter 1, Understanding Hosts, Services, and Contacts
..................Content has been hidden....................

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