How to create a new command

A Command line, which defines a command, is referenced in contact, host, and service definitions among other places. Commands that can be defined include service checks, service notifications, service event handlers, host checks, host notifications, and host event handlers. By default, check command definitions are stored in the file /usr/local/nagios/etc/commands.cfg.

Command definitions can contain macros, but you must make sure you include only those macros which are valid for the circumstances when the command is to be used. Immediately prior to the execution of a command, Nagios™ will replace all macros in the command with their corresponding values. This allows you to define a few generic commands to handle all your needs.

Creating a new command

  1. Select Configuration > Nagios Monitoring > Commands, then select New. You also have the option to Copy, Search, and Modify existing commands.
  2. In the Select Resource screen, check the resource macro to configure. This is used to specify an optional resource file that can contain $USERn$ macro definitions. $USERn$ macros are useful for storing usernames, passwords, and items commonly used in command definitions (like directory paths). User macros can be defined in one or more resource files. Nagios supports up to thirty-two user macros ($USER1$ through $USER32$).
  3. After selecting the resource macro, the screen will change and the selected macro's options will appear at the top of the screen. Select a Plugin to associate with the resource macro from the drop-down list. The selected plugin will check local or remote resources or services of some type. This is incorporated into the command line. And, select Next to continue.

    resource macro
  4. In the Command Wizard screen, enter a name for the command.
  5. Next, specify a command type: Check = service/host check commands, handlers; Notify = notification commands; Other = other commands (i.e. performance data collectors).
  6. The Command line 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. This field can be edited and before the command line is executed, all valid macros are replaced with their respective values. See the Nagios documentation on macros for determining when you can use different macros.

    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.

  7. The Test area within the Command Wizard screen enables you to test the command line for a host with arguments. When you click the Test button this parses through the command definition, performs the macro substitutions, inserts the hostname from the host box, puts the resulting command into a shell command line and executes, returning the results of the execution in the box to the right. Specifically, the Test fields are used as follows when you click the Test button;
    • Host: To specify the name of an existing host within Monarch. Used to substitute corresponding values for $HOSTNAME$, $HOSTALIAS$, and $HOSTADDRESS$ macro references in the command.
    • Arguments: Specifies the command-argument values, separated by ! characters (exclamation points). These will be used to substitute corresponding values for $ARG1$, $ARG2$, and similar macro references in the command.
    • Service description: To specify the name of a host service within Monarch. This will be used to substitute a value for $SERVICEDESC$ macro references in the command.

  8. Select Add to add the new command. Cancel will exit the command wizard without saving. You will be prompted that the command has been saved. Select Continue.

    command details

Related articles