Nagios - Commands

A command definition defines a command. Commands include service checks, service notifications, service event handlers, host checks, host notifications, and host event handlers. Command definitions for Nagios are defined in commands.cfg file.

The following is the format for defining of a Command −

define command {
   command_name command_name
   command_line command_line

Command name − This directive is used to identify the command. The definitions of contact, host, and service is referenced by command name.

Command line − This directive is used to define what is executed by Nagios when the command is used for service or host checks, notifications, or event handlers.


define command{
   command_name check_ssh
   command_line /usr/lib/nagios/plugins/check_ssh ‘$HOSTADDRESS$’

This command will execute the plugin − /usr/libl/nagios/plugins/check_ssh with 1 parameter : '$HOSTADDRESS$'

A very short host definition that would use this check command could be similar to the one shown here −

define host{
   host_name host_tutorial
   check_command check_ssh

The command definitions tell how to perform host/service checks. The also define how to generate notifications if any issue is identified and to handle any event. There are several commands to perform the checks, such as commands to check if SSH is working properly or not, command to check that database is up and running, command to check if a host is alive or not and many more.

There are commands which tell users what issues are present in the infrastructure. You can create your own custom commands or use any third-party command in Nagios, and they are treated similar to Nagios plugins project, there is no distinction between them.

You can also pass arguments in the command, this give more flexibility in performing the checks. This is how you need to define a command with parameter −

define command {
   command_name check-host-alive-limits
   command_line $USER5$/check_ping -H $HOSTADDRESS$ -w $ARG1$ -c $ARG2$ -p 5

The host definition for the above command −

define host {
   host_name system2
   check_command check-host-alive-limits!1000.0,70%!5000.0,100%

You can run external commands in Nagios by adding them to commands file which is processed by Nagios daemon periodically.

With External commands you can achieve lot many checks while Nagios is running. You can temporarily disable few checks, or force some checks to run immediately, disable notifications temporarily etc. The following is the syntax of external commands in Nagios that must be written in command file −

[time] command_id;command_arguments

You can also check out the list of all external commands that can be used in Nagios here −