1 ## <a id="commands"></a> Commands
3 Icinga 2 uses three different command object types to specify how
4 checks should be performed, notifications should be sent and
5 events should be handled.
7 ### <a id="command-environment-variables"></a> Environment Varialbes for Commands
9 Please check [Runtime Custom Attributes as Environment Variables](#runtime-custom-attribute-env-vars).
11 ### <a id="check-commands"></a> Check Commands
13 `CheckCommand` objects define the command line how a check is called.
15 `CheckCommand` objects require the ITL template `plugin-check-command`
16 to support native plugin based check methods.
18 Unless you have done so already, download your check plugin and put it
19 into the `PluginDir` directory. The following example uses the
20 `check_disk` plugin shipped with the Nagios Plugins package.
22 The plugin path and all command arguments are made a list of
23 double-quoted string arguments for proper shell escaping.
25 Call the `check_disk` plugin with the `--help` parameter to see
26 all available options. Our example defines warning (`-w`) and
27 critical (`-c`) thresholds for the disk usage. Without any
28 partition defined (`-p`) it will check all local partitions.
30 Define the default check command custom attribute `wfree` and `cfree` freely
31 definable naming schema) and their default threshold values. You can
32 then use these custom attributes as runtime macros on the command line.
34 The default custom attributes can be overridden by the custom attributes
35 defined in the service using the check command `disk`. The custom attributes
36 can also be inherited from a parent template using additive inheritance (`+=`).
38 object CheckCommand "disk" {
39 import "plugin-check-command"
42 PluginDir + "/check_disk",
51 The host `localhost` with the service `disk` checks all disks with modified
52 custom attributes (warning thresholds at `10%`, critical thresholds at `5%`
55 object Host "localhost" {
58 vars.address = "127.0.0.1"
62 object Service "disk" {
63 import "generic-service"
65 host_name = "localhost"
66 check_command = "disk"
73 ### <a id="notification-commands"></a> Notification Commands
75 `NotificationCommand` objects define how notifications are delivered to external
76 interfaces (E-Mail, XMPP, IRC, Twitter, etc).
78 `NotificationCommand` objects require the ITL template `plugin-notification-command`
79 to support native plugin-based notifications.
81 Below is an example using runtime macros from Icinga 2 (such as `$SERVICEOUTPUT$` for
82 the current check output) sending an email to the user(s) associated with the
83 notification itself (`email` custom attribute provided as `$USERMACRO$`).
85 If you require default custom attribute definitions, you can add a `vars` dictionary
86 as shown for the `CheckCommand` object.
90 object NotificationCommand "mail-service-notification" {
91 import "plugin-notification-command"
93 command = [ SysconfDir + "/icinga2/scripts/mail-notification.sh" ]
96 "NOTIFICATIONTYPE" = "$notification.type$"
97 "SERVICEDESC" = "$service.description$"
98 "HOSTALIAS" = "$host.displayname$",
99 "HOSTADDRESS" = "$host.vars.address$",
100 "SERVICESTATE" = "$service.state$",
101 "LONGDATETIME" = "$icinga.longdatetime$",
102 "SERVICEOUTPUT" = "$service.output$",
103 "NOTIFICATIONAUTHORNAME" = "$notification.author$",
104 "NOTIFICATIONCOMMENT" = "$notification.comment$",
105 "HOSTDISPLAYNAME" = "$host.displayname$",
106 "SERVICEDISPLAYNAME" = "$service.displayname$",
107 "USEREMAIL" = "$user.vars.email$"
111 The command attribute in the `mail-service-notification` command refers to the following
112 shell script. The macros specified in the `env` array are exported
113 as environment variables and can be used in the notification script:
116 template=$(cat <<TEMPLATE
119 Notification Type: $NOTIFICATIONTYPE
121 Service: $SERVICEDESC
123 Address: $HOSTADDRESS
126 Date/Time: $LONGDATETIME
128 Additional Info: $SERVICEOUTPUT
130 Comment: [$NOTIFICATIONAUTHORNAME] $NOTIFICATIONCOMMENT
134 /usr/bin/printf "%b" $template | mail -s "$NOTIFICATIONTYPE - $HOSTDISPLAYNAME - $SERVICEDISPLAYNAME is $SERVICESTATE" $USEREMAIL
136 While it's possible to specify the entire notification command right
137 in the NotificationCommand object it is generally advisable to create a
138 shell script in the `/etc/icinga2/scripts` directory and have the
139 NotificationCommand object refer to that.
141 ### <a id="event-commands"></a> Event Commands
143 Unlike notifications event commands are called on every service state change
144 if defined. Therefore the `EventCommand` object should define a command line
145 evaluating the current service state and other service runtime attributes
146 available through runtime vars. Runtime macros such as `$SERVICESTATETYPE$`
147 and `$SERVICESTATE$` will be processed by Icinga 2 helping on fine-granular
148 events being triggered.
150 Common use case scenarios are a failing HTTP check requiring an immediate
151 restart via event command, or if an application is locked and requires
152 a restart upon detection.
154 `EventCommand` objects require the ITL template `plugin-event-command`
155 to support native plugin based checks.
157 The example below is fictive and not necessarily meant for production use.
158 When the event command is triggered on a service state change, it will
159 send a check result using the `process_check_result` script forcibly
160 changing the service state back to `OK` (`-r 0`) providing some debug
161 information in the check output (`-o`).
163 object EventCommand "plugin-event-process-check-result" {
164 import "plugin-event-command"
167 PluginDir + "/process_check_result",
169 "-S", "$service.description$",
170 "-c", "/var/run/icinga2/cmd/icinga2.cmd",
172 "-o", "Event Handler triggered in state '$SERVICESTATE$' with output '$SERVICEOUTPUT$'."