1 # <a id="language-reference"></a> Language Reference
3 ## <a id="object-definition"></a> Object Definition
5 Icinga 2 features an object-based configuration format. You can define new
6 objects using the `object` keyword:
8 object Host "host1.example.org" {
11 address = "192.168.0.1"
15 In general you need to write each statement on a new line. Expressions started
16 with `{`, `(` and `[` extend until the matching closing character and can be broken
17 up into multiple lines.
19 Alternatively you can write multiple statements on a single line by separating
20 them with a semicolon:
22 object Host "host1.example.org" {
23 display_name = "host1"
25 address = "192.168.0.1"; address6 = "::1"
28 Each object is uniquely identified by its type (`Host`) and name
29 (`host1.example.org`). Some types have composite names, e.g. the
30 `Service` type which uses the `host_name` attribute and the name
31 you specified to generate its object name.
33 Exclamation marks (!) are not permitted in object names.
35 Objects can contain a comma-separated list of property
36 declarations. Instead of commas semicolons may also be used.
37 The following data types are available for property values:
39 All objects have at least the following attributes:
41 Attribute | Description
42 ---------------------|-----------------------------
43 name | The name of the object. This attribute can be modified in the object definition to override the name specified with the `object` directive.
44 type | The type of the object.
48 The following expressions can be used on the right-hand side of assignments.
50 ### <a id="numeric-literals"></a> Numeric Literals
52 A floating-point number.
58 ### <a id="duration-literals"></a> Duration Literals
60 Similar to floating-point numbers except for the fact that they support
61 suffixes to help with specifying time durations.
67 Supported suffixes include ms (milliseconds), s (seconds), m (minutes),
68 h (hours) and d (days).
70 Duration literals are converted to seconds by the config parser and
71 are treated like numeric literals.
73 ### <a id="string-literals"></a> String Literals
81 Certain characters need to be escaped. The following escape sequences
84 Character | Escape sequence
85 --------------------------|------------------------------------
89 <CARRIAGE-RETURN> | \\r
90 <LINE-FEED> | \\n
92 <FORM-FEED> | \\f
94 In addition to these pre-defined escape sequences you can specify
95 arbitrary ASCII characters using the backslash character (\\) followed
96 by an ASCII character in octal encoding.
98 ### <a id="multiline-string-literals"></a> Multi-line String Literals
100 Strings spanning multiple lines can be specified by enclosing them in
110 Unlike in ordinary strings special characters do not have to be escaped
111 in multi-line string literals.
113 ### <a id="boolean-literals"></a> Boolean Literals
115 The keywords `true` and `false` are used to denote truth values.
117 ### <a id="null-value"></a> Null Value
119 The `null` keyword can be used to specify an empty value.
121 ### <a id="dictionary"></a> Dictionary
123 An unordered list of key-value pairs. Keys must be unique and are
124 compared in a case-sensitive manner.
126 Individual key-value pairs must either be comma-separated or on separate lines.
127 The comma after the last key-value pair is optional.
132 address = "192.168.0.1"
136 Identifiers may not contain certain characters (e.g. space) or start
137 with certain characters (e.g. digits). If you want to use a dictionary
138 key that is not a valid identifier you can enclose the key in double
141 ### <a id="array"></a> Array
143 An ordered list of values.
145 Individual array elements must be comma-separated.
146 The comma after the last element is optional.
152 An array may simultaneously contain values of different types, such as
155 ### <a id="expression-operators"></a> Operators
157 The following operators are supported in expressions. The operators are by descending precedence.
159 Operator | Precedence | Examples (Result) | Description
160 ---------|------------|-----------------------------------------------|--------------------------------
161 () | 1 | (3 + 3) * 5 | Groups sub-expressions
162 () | 1 | Math.random() | Calls a function
163 [] | 1 | a[3] | Array subscript
164 . | 1 | a.b | Element access
165 ! | 2 | !"Hello" (false), !false (true) | Logical negation of the operand
166 ~ | 2 | ~true (false) | Bitwise negation of the operand
167 + | 2 | +3 | Unary plus
168 - | 2 | -3 | Unary minus
169 * | 3 | 5m * 10 (3000) | Multiplies two numbers
170 / | 3 | 5m / 5 (60) | Divides two numbers
171 % | 3 | 17 % 12 (5) | Remainder after division
172 + | 4 | 1 + 3 (4), "hello " + "world" ("hello world") | Adds two numbers; concatenates strings
173 - | 4 | 3 - 1 (2) | Subtracts two numbers
174 << | 5 | 4 << 8 (1024) | Left shift
175 >> | 5 | 1024 >> 4 (64) | Right shift
176 < | 6 | 3 < 5 (true) | Less than
177 > | 6 | 3 > 5 (false) | Greater than
178 <= | 6 | 3 <= 3 (true) | Less than or equal
179 >= | 6 | 3 >= 3 (true) | Greater than or equal
180 in | 7 | "foo" in [ "foo", "bar" ] (true) | Element contained in array
181 !in | 7 | "foo" !in [ "bar", "baz" ] (true) | Element not contained in array
182 == | 8 | "hello" == "hello" (true), 3 == 5 (false) | Equal to
183 != | 8 | "hello" != "world" (true), 3 != 3 (false) | Not equal to
184 & | 9 | 7 & 3 (3) | Binary AND
185 ^ | 10 | 17 ^ 12 (29) | Bitwise XOR
186 | | 11 | 2 | 3 (3) | Binary OR
187 && | 13 | true && false (false), 3 && 7 (7), 0 && 7 (0) | Logical AND
188 || | 14 | true || false (true), 0 || 7 (7)| Logical OR
189 = | 12 | a = 3 | Assignment
190 => | 15 | x => x * x (function with arg x) | Lambda, for loop
192 ### <a id="function-calls"></a> Function Calls
194 Functions can be called using the `()` operator:
196 const MyGroups = [ "test1", "test" ]
199 check_interval = len(MyGroups) * 1m
202 A list of available functions is available in the [Library Reference](21-library-reference.md#library-reference) chapter.
204 ## <a id="dictionary-operators"></a> Assignments
206 In addition to the `=` operator shown above a number of other operators
207 to manipulate attributes are supported. Here's a list of all
210 ### <a id="operator-assignment"></a> Operator =
212 Sets an attribute to the specified value.
221 In this example `a` has the value `7` after both instructions are executed.
223 ### <a id="operator-additive-assignment"></a> Operator +=
225 The += operator is a shortcut. The following expression:
239 ### <a id="operator-substractive-assignment"></a> Operator -=
241 The -= operator is a shortcut. The following expression:
255 ### <a id="operator-multiply-assignment"></a> Operator \*=
257 The *= operator is a shortcut. The following expression:
271 ### <a id="operator-dividing-assignment"></a> Operator /=
273 The /= operator is a shortcut. The following expression:
287 ## <a id="indexer"></a> Indexer
289 The indexer syntax provides a convenient way to set dictionary elements.
297 Example (alternative syntax):
300 hello["key"] = "world"
303 This is equivalent to writing:
311 If the `hello` attribute does not already have a value it is automatically initialized to an empty dictionary.
313 ## <a id="template-imports"></a> Template Imports
315 Objects can import attributes from other objects.
319 template Host "default-host" {
323 template Host "test-host" {
324 import "default-host"
329 object Host "localhost" {
332 address = "127.0.0.1"
336 The `default-host` and `test-host` objects are marked as templates
337 using the `template` keyword. Unlike ordinary objects templates are not
338 instantiated at run-time. Parent objects do not necessarily have to be
339 templates, however in general they are.
341 The `vars` dictionary for the `localhost` object contains all three
342 custom attributes and the custom attribute `colour` has the value `"blue"`.
344 Parent objects are resolved in the order they're specified using the
347 ## <a id="constants"></a> Constants
349 Global constants can be set using the `const` keyword:
351 const VarName = "some value"
353 Once defined a constant can be accessed from any file. Constants cannot be changed
356 Icinga 2 provides a number of special global constants. Some of them can be overridden using the `--define` command line parameter:
358 Variable |Description
359 --------------------|-------------------
360 PrefixDir |**Read-only.** Contains the installation prefix that was specified with cmake -DCMAKE_INSTALL_PREFIX. Defaults to "/usr/local".
361 SysconfDir |**Read-only.** Contains the path of the sysconf directory. Defaults to PrefixDir + "/etc".
362 ZonesDir |**Read-only.** Contains the path of the zones.d directory. Defaults to SysconfDir + "/zones.d".
363 LocalStateDir |**Read-only.** Contains the path of the local state directory. Defaults to PrefixDir + "/var".
364 RunDir |**Read-only.** Contains the path of the run directory. Defaults to LocalStateDir + "/run".
365 PkgDataDir |**Read-only.** Contains the path of the package data directory. Defaults to PrefixDir + "/share/icinga2".
366 StatePath |**Read-write.** Contains the path of the Icinga 2 state file. Defaults to LocalStateDir + "/lib/icinga2/icinga2.state".
367 ObjectsPath |**Read-write.** Contains the path of the Icinga 2 objects file. Defaults to LocalStateDir + "/cache/icinga2/icinga2.debug".
368 PidPath |**Read-write.** Contains the path of the Icinga 2 PID file. Defaults to RunDir + "/icinga2/icinga2.pid".
369 Vars |**Read-write.** Contains a dictionary with global custom attributes. Not set by default.
370 NodeName |**Read-write.** Contains the cluster node name. Set to the local hostname by default.
371 UseVfork |**Read-write.** Whether to use vfork(). Only available on *NIX. Defaults to true.
372 AttachDebugger |**Read-write.** Whether to attach a debugger when Icinga 2 crashes. Defaults to false.
373 RunAsUser |**Read-write.** Defines the user the Icinga 2 daemon is running as. Used in the `init.conf` configuration file.
374 RunAsGroup |**Read-write.** Defines the group the Icinga 2 daemon is running as. Used in the `init.conf` configuration file.
376 ## <a id="apply"></a> Apply
378 The `apply` keyword can be used to create new objects which are associated with
379 another group of objects.
381 apply Service "ping" to Host {
382 import "generic-service"
384 check_command = "ping4"
386 assign where host.name == "localhost"
389 In this example the `assign where` condition is a boolean expression which is
390 evaluated for all objects of type `Host` and a new service with name "ping"
391 is created for each matching host. [Expression operators](20-language-reference.md#expression-operators)
392 may be used in `assign where` conditions.
394 The `to` keyword and the target type may be omitted if there is only one target
395 type, e.g. for the `Service` type.
397 Depending on the object type used in the `apply` expression additional local
398 variables may be available for use in the `where` condition:
400 Source Type | Target Type | Variables
401 ------------------|-------------|--------------
402 Service | Host | host
403 Dependency | Host | host
404 Dependency | Service | host, service
405 Notification | Host | host
406 Notification | Service | host, service
407 ScheduledDowntime | Host | host
408 ScheduledDowntime | Service | host, service
410 Any valid config attribute can be accessed using the `host` and `service`
411 variables. For example, `host.address` would return the value of the host's
412 "address" attribute - or null if that attribute isn't set.
414 ## <a id="group-assign"></a> Group Assign
416 Group objects can be assigned to specific member objects using the `assign where`
417 and `ignore where` conditions.
419 object HostGroup "linux-servers" {
420 display_name = "Linux Servers"
422 assign where host.vars.os == "Linux"
425 In this example the `assign where` condition is a boolean expression which is evaluated
426 for all objects of the type `Host`. Each matching host is added as member to the host group
427 with the name "linux-servers". Membership exclusion can be controlled using the `ignore where`
428 condition. [Expression operators](20-language-reference.md#expression-operators) may be used in `assign where` and
429 `ignore where` conditions.
431 Source Type | Variables
432 ------------------|--------------
434 ServiceGroup | host, service
438 ## <a id="boolean-values"></a> Boolean Values
440 The `assign where`, `ignore where`, `if` and `while` statements, the `!` operator as
441 well as the `bool()` function convert their arguments to a boolean value based on the
444 Description | Example Value | Boolean Value
445 ---------------------|-------------------|--------------
446 Empty value | null | false
448 Non-zero integer | -23945 | true
449 Empty string | "" | false
450 Non-empty string | "Hello" | true
451 Empty array | [] | false
452 Non-empty array | [ "Hello" ] | true
453 Empty dictionary | {} | false
454 Non-empty dictionary | { key = "value" } | true
456 For a list of supported expression operators for `assign where` and `ignore where`
457 statements, see [expression operators](20-language-reference.md#expression-operators).
459 ## <a id="comments"></a> Comments
461 The Icinga 2 configuration format supports C/C++-style and shell-style comments.
468 object Host "localhost" {
469 check_interval = 30 // this is also a comment.
470 retry_interval = 15 # yet another comment
473 ## <a id="includes"></a> Includes
475 Other configuration files can be included using the `include` directive.
476 Paths must be relative to the configuration file that contains the
481 include "some/other/file.conf"
482 include "conf.d/*.conf"
484 Wildcard includes are not recursive.
486 Icinga also supports include search paths similar to how they work in a
491 Note the use of angle brackets instead of double quotes. This causes the
492 config compiler to search the include search paths for the specified
493 file. By default $PREFIX/share/icinga2/include is included in the list of search
494 paths. Additional include search paths can be added using
495 [command-line options](8-cli-commands.md#config-include-path).
497 Wildcards are not permitted when using angle brackets.
499 ## <a id="recursive-includes"></a> Recursive Includes
501 The `include_recursive` directive can be used to recursively include all
502 files in a directory which match a certain pattern.
506 include_recursive "conf.d", "*.conf"
507 include_recursive "templates"
509 The first parameter specifies the directory from which files should be
510 recursively included.
512 The file names need to match the pattern given in the second parameter.
513 When no pattern is specified the default pattern "*.conf" is used.
515 ## <a id="zone-includes"></a> Zone Includes
517 The `include_zones` recursively includes all subdirectories for the
520 In addition to that it sets the `zone` attribute for all objects created
521 in these subdirectories to the name of the subdirectory.
525 include_zones "etc", "zones.d", "*.conf"
526 include_zones "puppet", "puppet-zones"
528 The first parameter specifies a tag name for this directive. Each `include_zones`
529 invocation should use a unique tag name. When copying the zones' configuration
530 files Icinga uses the tag name as the name for the destination directory in
531 `/var/lib/icinga2/api/config`.
533 The second parameter specifies the directory which contains the subdirectories.
535 The file names need to match the pattern given in the third parameter.
536 When no pattern is specified the default pattern "*.conf" is used.
538 ## <a id="library"></a> Library directive
540 The `library` directive can be used to manually load additional
541 libraries. Libraries can be used to provide additional object types and
548 ## <a id="functions"></a> Functions
550 Functions can be defined using the `function` keyword.
554 function multiply(a, b) {
558 When encountering the `return` keyword further execution of the function is terminated and
559 the specified value is supplied to the caller of the function:
563 In this example the `multiply` function we declared earlier is invoked with two arguments (3 and 5).
564 The function computes the product of those arguments and makes the result available to the
567 When no value is supplied for the `return` statement the function returns `null`.
569 Functions which do not have a `return` statement have their return value set to the value of the
570 last expression which was performed by the function. For example, we could have also written our
571 `multiply` function like this:
573 function multiply(a, b) {
577 Anonymous functions can be created by omitting the name in the function definition. The
578 resulting function object can be used like any other value:
580 var fn = function() { 3 }
584 ## <a id="lambdas"></a> Lambda Expressions
586 Functions can also be declared using the alternative lambda syntax.
592 Multiple statements can be used by putting the function body into braces:
599 Just like with ordinary functions the return value is the value of the last statement.
601 For lambdas which take exactly one argument the braces around the arguments can be omitted:
605 ## <a id="nullary-lambdas"></a> Abbreviated Lambda Syntax
607 Lambdas which take no arguments can also be written using the abbreviated lambda syntax.
613 This creates a new function which returns the value 3.
615 ## <a id="variable-scopes"></a> Variable Scopes
617 When setting a variable Icinga checks the following scopes in this order whether the variable
618 already exists there:
624 The local scope contains variables which only exist during the invocation of the current function,
625 object or apply statement. Local variables can be declared using the `var` keyword:
627 function multiply(a, b) {
632 Each time the `multiply` function is invoked a new `temp` variable is used which is in no way
633 related to previous invocations of the function.
635 When setting a variable which has not previously been declared as local using the `var` keyword
636 the `this` scope is used.
638 The `this` scope refers to the current object which the function or object/apply statement
641 object Host "localhost" {
645 In this example the `this` scope refers to the "localhost" object. The `check_interval` attribute
646 is set for this particular host.
648 You can explicitly access the `this` scope using the `this` keyword:
650 object Host "localhost" {
651 var check_interval = 5m
653 /* This explicitly specifies that the attribute should be set
654 * for the host, if we had omitted `this.` the (poorly named)
655 * local variable `check_interval` would have been modified instead.
657 this.check_interval = 1m
660 Similarly the keywords `locals` and `globals` are available to access the local and global scope.
662 Functions also have a `this` scope. However unlike for object/apply statements the `this` scope for
663 a function is set to whichever object was used to invoke the function. Here's an example:
668 function init(word) {
673 /* Let's invoke the init() function */
676 We're using `hm.init` to invoke the function which causes the value of `hm` to become the `this`
677 scope for this function call.
679 ## <a id="closures"></a> Closures
681 By default `function`s, `object`s and `apply` rules do not have access to variables declared
682 outside of their scope (except for global variables).
684 In order to access variables which are defined in the outer scope the `use` keyword can be used:
686 function MakeHelloFunction(name) {
687 return function() use(name) {
688 log("Hello, " + name)
692 In this case a new variable `name` is created inside the inner function's scope which has the
693 value of the `name` function argument.
695 Alternatively a different value for the inner variable can be specified:
697 function MakeHelloFunction(name) {
698 return function() use (greeting = "Hello, " + name) {
703 ## <a id="conditional-statements"></a> Conditional Statements
705 Sometimes it can be desirable to only evaluate statements when certain conditions are met. The if/else
706 construct can be used to accomplish this.
720 An if/else construct can also be used in place of any other value. The value of an if/else statement
721 is the value of the last statement which was evaluated for the branch which was taken:
724 log("Taking the 'true' branch")
727 log("Taking the 'false' branch")
731 This example prints the log message "Taking the 'true' branch" and the `a` variable is set to 21 (7 * 3).
733 The value of an if/else construct is null if the condition evaluates to false and no else branch is given.
735 ## <a id="while-loops"></a> While Loops
737 The `while` statement checks a condition and executes the loop body when the condition evaluates to `true`.
738 This is repeated until the condition is no longer true.
749 The `continue` and `break` keywords can be used to control how the loop is executed: The `continue` keyword
750 skips over the remaining expressions for the loop body and begins the next loop evaluation. The `break` keyword
751 breaks out of the loop.
753 ## <a id="for-loops"></a> For Loops
755 The `for` statement can be used to iterate over arrays and dictionaries.
759 var list = [ "a", "b", "c" ]
765 The loop body is evaluated once for each item in the array. The variable `item` is declared as a local
766 variable just as if the `var` keyword had been used.
768 Iterating over dictionaries can be accomplished in a similar manner:
770 var dict = { a = 3, b = 7 }
772 for (key => value in dict) {
773 log("Key: " + key + ", Value: " + value)
776 The `continue` and `break` keywords can be used to control how the loop is executed: The `continue` keyword
777 skips over the remaining expressions for the loop body and begins the next loop evaluation. The `break` keyword
778 breaks out of the loop.
780 ## <a id="throw"></a> Exceptions
782 Built-in commands may throw exceptions to signal errors such as invalid arguments. User scripts can throw exceptions
783 using the `throw` keyword.
787 throw "An error occurred."
789 There is currently no way for scripts to catch exceptions.
791 ## <a id="types"></a> Types
793 All values have a static type. The `typeof` function can be used to determine the type of a value:
795 typeof(3) /* Returns an object which represents the type for numbers */
797 The following built-in types are available:
799 Type | Examples | Description
800 -----------|-------------------|------------------------
801 Number | 3.7 | A numerical value.
802 Boolean | true, false | A boolean value.
803 String | "hello" | A string.
804 Array | [ "a", "b" ] | An array.
805 Dictionary | { a = 3 } | A dictionary.
807 Depending on which libraries are loaded additional types may become available. The `icinga`
808 library implements a whole bunch of other types, e.g. Host, Service, CheckCommand, etc.
810 Each type has an associated type object which describes the type's semantics. These
811 type objects are made available using global variables which match the type's name:
813 /* This logs 'true' */
814 log(typeof(3) == Number)
816 The type object's `prototype` property can be used to find out which methods a certain type
819 /* This returns: ["find","len","lower","replace","split","substr","to_string","upper"] */
820 keys(String.prototype)
822 ## <a id="reserved-keywords"></a> Reserved Keywords
824 These keywords are reserved and must not be used as constants or custom attributes.
851 You can escape reserved keywords using the `@` character. The following example
852 tries to set `vars.include` which references a reserved keyword and generates
855 [2014-09-15 17:24:00 +0200] critical/config: Location:
856 /etc/icinga2/conf.d/hosts/localhost.conf(13): vars.sla = "24x7"
857 /etc/icinga2/conf.d/hosts/localhost.conf(14):
858 /etc/icinga2/conf.d/hosts/localhost.conf(15): vars.include = "some cmdb export field"
860 /etc/icinga2/conf.d/hosts/localhost.conf(16): }
861 /etc/icinga2/conf.d/hosts/localhost.conf(17):
863 Config error: in /etc/icinga2/conf.d/hosts/localhost.conf: 15:8-15:14: syntax error, unexpected include (T_INCLUDE), expecting T_IDENTIFIER
864 [2014-09-15 17:24:00 +0200] critical/config: 1 errors, 0 warnings.
866 You can escape the `include` keyword by prefixing it with an additional `@` character:
868 object Host "localhost" {
869 import "generic-host"
871 address = "127.0.0.1"
877 vars.@include = "some cmdb export field"