Nagios Core Configuration - command

In the case that you have followed steps mentioned at installation of the Nagios Core document at this web page the predefined configuration file with commands objects is located at:

/opt/nagios-<VERSION>/etc/objects/commands.cfg

Preface

During the installation process it was possible to see that Nagios Core is released without Nagios Plugins. It mean that Nagios Core itself is not really providing any kind of monitoring scripts that can be directly used for monitoring of your infrastructure.

When I have seen this design for first time I was wondering that Nagios is so popular (as I have been working already several years with enterprise monitoring tools, with massive overhead of useless stuff). Later on when I have got more experience and deeper understanding of Nagios Core, I have realized how brilliant this framework design is.

According to this please don’t be scarred.

command(s)

Description

Nagios Core is using configuration object called “command” to specify how to call external script that will do the particular action for us.

In this way it is possible to specify object that will call monitoring script, or to specify notification script that will be used in case of failure to send a notification.

In general it is possible to call any script that do follow basic rules (describing of check scripts is not goal of this document, I will go deeper in this at document):
- To provide exit code that will be taken over at Nagios Core as monitoring status.
- To provide message text and performance data on STDIO (in common format) that will be taken over to Nagios Core.
- To be effective enough.

Location

Nagios Core with help of Nagios Plugins is providing to you already predefined set of “command” objects.

Located at (in the case that you have followed the installation of Nagios Core described on this website): /opt/nagios-<VERSION>/etc/objects/commands.cfg

#  grep -e command_name -e command_line /opt/nagios-4.0.7/etc/objects/commands.cfg

        command_name    notify-host-by-email
        command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$

        command_name    notify-service-by-email
        command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$command_name    check-host-alive

        command_name    check-host-alive
        command_line    $USER1$/check_ping -H $HOSTADDRESS$ -w 3000.0,80% -c 5000.0,100% -p 5

        command_name    check_local_disk
        command_line    $USER1$/check_disk -w $ARG1$ -c $ARG2$ -p $ARG3$

        command_name    check_local_load
        command_line    $USER1$/check_load -w $ARG1$ -c $ARG2$

        command_name    check_local_procs
        command_line    $USER1$/check_procs -w $ARG1$ -c $ARG2$ -s $ARG3$

        command_name    check_local_users
        command_line    $USER1$/check_users -w $ARG1$ -c $ARG2$

        command_name    check_local_swap
        command_line    $USER1$/check_swap -w $ARG1$ -c $ARG2$

        command_name    check_local_mrtgtraf
        command_line    $USER1$/check_mrtgtraf -F $ARG1$ -a $ARG2$ -w $ARG3$ -c $ARG4$ -e $ARG5$

        command_name    check_ftp
        command_line    $USER1$/check_ftp -H $HOSTADDRESS$ $ARG1$

        command_name    check_hpjd
        command_line    $USER1$/check_hpjd -H $HOSTADDRESS$ $ARG1$

        command_name    check_snmp
        command_line    $USER1$/check_snmp -H $HOSTADDRESS$ $ARG1$

        command_name    check_http
        command_line    $USER1$/check_http -I $HOSTADDRESS$ $ARG1$

        command_name    check_ssh
        command_line    $USER1$/check_ssh $ARG1$ $HOSTADDRESS$

        command_name    check_dhcp
        command_line    $USER1$/check_dhcp $ARG1$

        command_name    check_ping
        command_line    $USER1$/check_ping -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$ -p 5

        command_name    check_pop
        command_line    $USER1$/check_pop -H $HOSTADDRESS$ $ARG1$

        command_name    check_imap
        command_line    $USER1$/check_imap -H $HOSTADDRESS$ $ARG1$

        command_name    check_smtp
        command_line    $USER1$/check_smtp -H $HOSTADDRESS$ $ARG1$

        command_name    check_tcp
        command_line    $USER1$/check_tcp -H $HOSTADDRESS$ -p $ARG1$ $ARG2$

        command_name    check_udp
        command_line    $USER1$/check_udp -H $HOSTADDRESS$ -p $ARG1$ $ARG2$

        command_name    check_nt
        command_line    $USER1$/check_nt -H $HOSTADDRESS$ -p 12489 -v $ARG1$ $ARG2$

        command_name    process-host-perfdata
        command_line    /usr/bin/printf "%b" "$LASTHOSTCHECK$\t$HOSTNAME$\t$HOSTSTATE$\t$HOSTATTEMPT$\t$HOSTSTATETYPE$\t$HOSTEXECUTIONTIME$\t$HOSTOUTPUT$\t$HOSTPERFDATA$\n">> /opt/nagios-4.0.7/var/host-perfdata.out

        command_name    process-service-perfdata
        command_line    /usr/bin/printf "%b" "$LASTSERVICECHECK$\t$HOSTNAME$\t$SERVICEDESC$\t$SERVICESTATE$\t$SERVICEATTEMPT$\t$SERVICESTATETYPE$\t$SERVICEEXECUTIONTIME$\t$SERVICELATENCY$\t$SERVICEOUTPUT$\t$SERVICEPERFDATA$\n">> /opt/nagios-4.0.7/var/service-perfdata.out

Location Customization

In the case that you prefer to use your own configuration file, to store your customized configuration it is possible to define path to your configuration file.

In this case Nagios Core need to know where to search for the customized configuration file.

According to this it is required to update the main Nagios Core configuration file – “nagios.cfg”
It is possible to specify:

cfg_file=/<path>/<to>/<your>/<config>/<file>         # Direct path to you customized configuration file
cfg_dir=/<path>/<to>/<your>/<config>/<dir>           # Path to the directory where to search for the config file.

Official documentation

In most of my documents I’m preventing to copying of the official documentation. On another hand I think at this point it is really handy as I will not reinvent the wheel.

Description:

A command definition is just that. It defines a command. Commands that can be defined include service checks, service notifications, service event handlers, host checks, host notifications, and host event handlers. Command definitions can contain macros, but you must make sure that you include only those macros that are “valid” for the circumstances when the command will be used.

Definition Format:

define command{
               command_name     command_name                  # Mandatory parameter
               command_line     command_line                  # Mandatory parameter
              }

Directive Descriptions:

command_name: This directive is the short name used to identify the command. It is referenced in contact, host, and service definitions (in notification, check, and event handler directives), among other places.
command_line: This directive is used to define what is actually executed by Nagios when the command is used for service or host checks, notifications, or event handlers. Before the command line is executed, all valid macros are replaced with their respective values. See the documentation on macros for determining when you can use different macros. Note that the command line is not surrounded in quotes. Also, if you want to pass a dollar sign ($) on the command line, you have to escape it with another dollar sign.

NOTE: You may not include a semicolon (;) in the command_line directive, because everything after it will be ignored as a config file comment. You can work around this limitation by setting one of the $USER$ macros in your resource file to a semicolon and then referencing the appropriate $USER$ macro in the command_line directive in place of the semicolon.

If you want to pass arguments to commands during runtime, you can use $ARGn$ macros in the command_line directive of the command definition and then separate individual arguments from the command name (and from each other) using bang (!) characters in the object definition directive (host check command, service event handler command, etc) that references the command. More information on how arguments in command definitions are processed during runtime can be found in the documentation on macros.

Macros

Goal of this document is not to describe Macros used at Nagios Core. On another hand for better understanding of command object definition I will try to provide short over view of macros.

Nagios Core is using macros to:
- Address parameters defined at another objects.
- To provide values to object that is called.
- To use environment values.

Command object definition is one of the please where understanding of Macros is crucial. In general it is possible to understand Macros as variables or pointers that are used for providing data for command object definition.

$USER$

This type of variable is defined at “resource.cfg” file. In the case that you have followed steps mentioned at installation of the Nagios Core document at this file is located at: “/opt/nagios-<VERSION>/etc/resource.cfg”

It is possible to define 1-32 of this variables / macros.

Usually it is used for definition of path to folder where are monitoring scripts located. On another hand as a standard variable / macro it can include nearly any string.

Example of macro definition

# grep -v -e "^$" -e "^#" /opt/nagios-4.0.7/etc/resource.cfg
$USER1$=/opt/nagios-4.0.7/libexec                                                  # Path to monitoring scripts used at Nagios Core

Example of using of the macro:

define command{
               command_name    check_local_disk
               command_line    $USER1$/check_disk -w $ARG1$ -c $ARG2$ -p $ARG3$    # Definition of the command is using $USER1$ macro to specify the path to file
              }

$ARGn$

This definition is used to provide values to command that is called from another object. For better understanding it is possible to think about this like about calling a function and providing values for the function at standard programming language.

Example of service definition:

define service{
               use                      local-service                                              # Name of service template to use
               host_name                localhost
               service_description      PING
               check_command            check_ping!100.0,20%!500.0,60%                             # Command definition
              }

Example of command definition:

define command{
               command_name    check_ping
               command_line    $USER1$/check_ping -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$ -p 5        # In this case $ARG1$ = 100.0,20%  ;  $ARG2$ = 500.0,60%
              }

It’s possible to see at service definition that values delimiter in this case is “!” exclamation mark.

It’s possible to see at command specification that the position of the values is important.

In general it is possible to say that, in this way you can configure one “command” object and customize it at each time when you are using it inside of another object definition. This is providing the possibility to standardize of your monitoring commands, with the possibility to customize then during definition inside of another object.

$Nagios_Core_Macro$

Nagios Core is using object specification that is collection of mandatory, optional and custom attributes. In general it is possible to use all this information inside of the command configuration.

In the case that you have experience with programming languages you can compare it to the using of structure variables.

In this way it is possible to benefit from using of Nagios Core Macros as it is possible to specify plenty of objects with same structure but different values that will call the same command. Based on this it is enough to use same Macro at command object declaration to integrate this command with all mentioned objects.

Example of host and service definition:

define host{
             use                       linux-server
             host_name                 localhost                                                   # Host object name that is used at service
             alias                     localhost
             addres/                   127.0.0.1
            }

define service{
               use                      local-service
               host_name                localhost                                                  # Definition of related Host
               service_description      PING
               check_command            check_ping!100.0,20%!500.0,60%                             # Command definition
              }

Example of command definition:

define command{
               command_name    check_ping
               command_line    $USER1$/check_ping -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$ -p 5        # $HOSTADDRESS$ = 127.0.0.1
              }

In this example it is possible to see that even the Command will be called from Service that is related to the Host it is still possible to use Host related Macro to get the right value for executing of the monitoring script.

$_Custom$

In some cases it is really handy to extend Nagios Core object definition with additional variables that can be used as a custom macros.

In this way it is possible to specify additional information that can be used for better integration of the Nagios Core in to your environment.

Example 1:

It is best practice to create documentation and manuals as part of the implementation process of new monitoring solution. Probably the simplest way is to create Wiki server where each monitoring solution will be documented.

The question is how to provide this information to the support team so that they do not have to search for them too long. Probably the best way is to include URL related to the documentation in to the alarm notification.

In our example we will extend service definition with custom value including URL related to the documentation, so that we will be able to use this value in notification command.

define host{
             use                       linux-server
             host_name                 localhost                                                   # Host object name that is used at service
             alias                     localhost
             addres/                   127.0.0.1
            }

define service{
               use                      local-service
               host_name                localhost                                                  # Definition of related Host
               service_description      PING
               check_command            check_ping!100.0,20%!500.0,60%
               _WIKI                    www.wiki.org                                               # Definition of Custom Variable
              }

Notification command:

define command{
               command_name    notify-service-by-email
               command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n WIKI:$_SERVICEWIKI$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$
              }

Example 2:

In this example I'm going to use custom variable to specify thresholds for monitoring command at service object. In some situation it is handy as the same monitoring command is requiring another parameter based on the context of usage.

In this way you can use same command object without modification for several services.

define host{
             use                       linux-server
             host_name                 localhost                                                   # Host object name that is used at service
             alias                     localhost
             addres/                   127.0.0.1
            }

define service{
               use                      local-service
               host_name                localhost                                                  # Definition of related Host
               service_description      PING
               check_command            check_ping                                                 # Monitoring command
               _WARNING                 100.0,20%                                                  # Definition of Custom Variable
               _CRITICAL                500.0,60%                                                  # Definition of Custom Variable
              }

Monitoring command:

define command{
               command_name    check_ping
               command_line    $USER1$/check_ping -H $HOSTADDRESS$ -w $_SERVICEWARNING$ -c $_SERVICECRITICAL$ -p 5      # $_SERVICEWARNING$ = 100.0,20% ; $_SERVICECRITICAL$ = 500.0,60%
              }

To describe how to use macros and environment variables is not target of this document. On another hand for better understanding of command object definition it’s required to get familiar with mentioned variables.

URL's

Navigation
Print/export
QR Code
QR Code wiki:infrastructure_tools:nagios:nagios_core_configuration_-_command (generated for current page)