]> granicus.if.org Git - icinga2/blob - doc/17-language-reference.md
Ensure we don't leak file descriptors to child processes
[icinga2] / doc / 17-language-reference.md
1 # <a id="language-reference"></a> Language Reference
2
3 ## <a id="object-definition"></a> Object Definition
4
5 Icinga 2 features an object-based configuration format. You can define new
6 objects using the `object` keyword:
7
8     object Host "host1.example.org" {
9       display_name = "host1"
10
11       address = "192.168.0.1"
12       address6 = "::1"
13     }
14
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.
18
19 Alternatively you can write multiple statements on a single line by separating
20 them with a semicolon:
21
22     object Host "host1.example.org" {
23       display_name = "host1"
24
25       address = "192.168.0.1"; address6 = "::1"
26     }
27
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.
32
33 Exclamation marks (!) are not permitted in object names.
34
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:
38
39 All objects have at least the following attributes:
40
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.
45
46 ## <a id="expressions"></a> Expressions
47
48 The following expressions can be used on the right-hand side of assignments.
49
50 ### <a id="numeric-literals"></a> Numeric Literals
51
52 A floating-point number.
53
54 Example:
55
56     27.3
57
58 ### <a id="duration-literals"></a> Duration Literals
59
60 Similar to floating-point numbers except for the fact that they support
61 suffixes to help with specifying time durations.
62
63 Example:
64
65     2.5m
66
67 Supported suffixes include ms (milliseconds), s (seconds), m (minutes),
68 h (hours) and d (days).
69
70 Duration literals are converted to seconds by the config parser and
71 are treated like numeric literals.
72
73 ### <a id="string-literals"></a> String Literals
74
75 A string.
76
77 Example:
78
79     "Hello World!"
80
81 Certain characters need to be escaped. The following escape sequences
82 are supported:
83
84 Character                 | Escape sequence
85 --------------------------|------------------------------------
86 "                         | \\"
87 \\                        | \\\\
88 &lt;TAB&gt;               | \\t
89 &lt;CARRIAGE-RETURN&gt;   | \\r
90 &lt;LINE-FEED&gt;         | \\n
91 &lt;BEL&gt;               | \\b
92 &lt;FORM-FEED&gt;         | \\f
93
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.
97
98 ### <a id="multiline-string-literals"></a> Multi-line String Literals
99
100 Strings spanning multiple lines can be specified by enclosing them in
101 {{{ and }}}.
102
103 Example:
104
105     {{{This
106     is
107     a multi-line
108     string.}}}
109
110 Unlike in ordinary strings special characters do not have to be escaped
111 in multi-line string literals.
112
113 ### <a id="boolean-literals"></a> Boolean Literals
114
115 The keywords `true` and `false` are used to denote truth values.
116
117 ### <a id="null-value"></a> Null Value
118
119 The `null` keyword can be used to specify an empty value.
120
121 ### <a id="dictionary"></a> Dictionary
122
123 An unordered list of key-value pairs. Keys must be unique and are
124 compared in a case-sensitive manner.
125
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.
128
129 Example:
130
131     {
132       address = "192.168.0.1"
133       port = 443
134     }
135
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
139 quotes.
140
141 ### <a id="array"></a> Array
142
143 An ordered list of values.
144
145 Individual array elements must be comma-separated.
146 The comma after the last element is optional.
147
148 Example:
149
150     [ "hello", 42 ]
151
152 An array may simultaneously contain values of different types, such as
153 strings and numbers.
154
155 ### <a id="expression-operators"></a> Operators
156
157 The following operators are supported in expressions. The operators are by descending precedence.
158
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 &#124;   | 11          | 2 &#124; 3 (3)                                | Binary OR
187 &&       | 13         | true && false (false), 3 && 7 (7), 0 && 7 (0) | Logical AND
188 &#124;&#124; | 14     | true &#124;&#124; false (true), 0 &#124;&#124; 7 (7)| Logical OR
189 =        | 12         | a = 3                                         | Assignment
190 =>       | 15         | x => x * x (function with arg x)              | Lambda, for loop
191
192 ### <a id="function-calls"></a> Function Calls
193
194 Functions can be called using the `()` operator:
195
196     const MyGroups = [ "test1", "test" ]
197
198     {
199       check_interval = len(MyGroups) * 1m
200     }
201
202 A list of available functions is available in the [Library Reference](18-library-reference.md#library-reference) chapter.
203
204 ## <a id="dictionary-operators"></a> Assignments
205
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
208 available operators:
209
210 ### <a id="operator-assignment"></a> Operator =
211
212 Sets an attribute to the specified value.
213
214 Example:
215
216     {
217       a = 5
218       a = 7
219     }
220
221 In this example `a` has the value `7` after both instructions are executed.
222
223 ### <a id="operator-additive-assignment"></a> Operator +=
224
225 The += operator is a shortcut. The following expression:
226
227     {
228       a = [ "hello" ]
229       a += [ "world" ]
230     }
231
232 is equivalent to:
233
234     {
235       a = [ "hello" ]
236       a = a + [ "world" ]
237     }
238
239 ### <a id="operator-substractive-assignment"></a> Operator -=
240
241 The -= operator is a shortcut. The following expression:
242
243     {
244       a = 10
245       a -= 5
246     }
247
248 is equivalent to:
249
250     {
251       a = 10
252       a = a - 5
253     }
254
255 ### <a id="operator-multiply-assignment"></a> Operator \*=
256
257 The *= operator is a shortcut. The following expression:
258
259     {
260       a = 60
261       a *= 5
262     }
263
264 is equivalent to:
265
266     {
267       a = 60
268       a = a * 5
269     }
270
271 ### <a id="operator-dividing-assignment"></a> Operator /=
272
273 The /= operator is a shortcut. The following expression:
274
275     {
276       a = 300
277       a /= 5
278     }
279
280 is equivalent to:
281
282     {
283       a = 300
284       a = a / 5
285     }
286
287 ## <a id="indexer"></a> Indexer
288
289 The indexer syntax provides a convenient way to set dictionary elements.
290
291 Example:
292
293     {
294       hello.key = "world"
295     }
296
297 Example (alternative syntax):
298
299     {
300       hello["key"] = "world"
301     }
302
303 This is equivalent to writing:
304
305     {
306       hello += {
307         key = "world"
308       }
309     }
310
311 If the `hello` attribute does not already have a value, it is automatically initialized to an empty dictionary.
312
313 ## <a id="template-imports"></a> Template Imports
314
315 Objects can import attributes from other objects.
316
317 Example:
318
319     template Host "default-host" {
320       vars.colour = "red"
321     }
322
323     template Host "test-host" {
324       import "default-host"
325
326       vars.colour = "blue"
327     }
328
329     object Host "localhost" {
330       import "test-host"
331
332       address = "127.0.0.1"
333       address6 = "::1"
334     }
335
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.
340
341 The `vars` dictionary for the `localhost` object contains all three
342 custom attributes and the custom attribute `colour` has the value `"blue"`.
343
344 Parent objects are resolved in the order they're specified using the
345 `import` keyword.
346
347 Default templates which are automatically imported into all object definitions
348 can be specified using the `default` keyword:
349
350     template CheckCommand "plugin-check-command" default {
351       // ...
352     }
353
354 Default templates are imported before any other user-specified statement in an
355 object definition is evaluated.
356
357 If there are multiple default templates the order in which they are imported
358 is unspecified.
359
360 ## <a id="constants"></a> Constants
361
362 Global constants can be set using the `const` keyword:
363
364     const VarName = "some value"
365
366 Once defined a constant can be accessed from any file. Constants cannot be changed
367 once they are set.
368
369 Icinga 2 provides a number of special global constants. Some of them can be overridden using the `--define` command line parameter:
370
371 Variable            |Description
372 --------------------|-------------------
373 PrefixDir           |**Read-only.** Contains the installation prefix that was specified with cmake -DCMAKE_INSTALL_PREFIX. Defaults to "/usr/local".
374 SysconfDir          |**Read-only.** Contains the path of the sysconf directory. Defaults to PrefixDir + "/etc".
375 ZonesDir            |**Read-only.** Contains the path of the zones.d directory. Defaults to SysconfDir + "/zones.d".
376 LocalStateDir       |**Read-only.** Contains the path of the local state directory. Defaults to PrefixDir + "/var".
377 RunDir              |**Read-only.** Contains the path of the run directory. Defaults to LocalStateDir + "/run".
378 PkgDataDir          |**Read-only.** Contains the path of the package data directory. Defaults to PrefixDir + "/share/icinga2".
379 StatePath           |**Read-write.** Contains the path of the Icinga 2 state file. Defaults to LocalStateDir + "/lib/icinga2/icinga2.state".
380 ObjectsPath         |**Read-write.** Contains the path of the Icinga 2 objects file. Defaults to LocalStateDir + "/cache/icinga2/icinga2.debug".
381 PidPath             |**Read-write.** Contains the path of the Icinga 2 PID file. Defaults to RunDir + "/icinga2/icinga2.pid".
382 Vars                |**Read-write.** Contains a dictionary with global custom attributes. Not set by default.
383 NodeName            |**Read-write.** Contains the cluster node name. Set to the local hostname by default.
384 EventEngine         |**Read-write.** The name of the socket event engine, can be "poll" or "epoll". The epoll interface is only supported on Linux.
385 AttachDebugger      |**Read-write.** Whether to attach a debugger when Icinga 2 crashes. Defaults to false.
386 RunAsUser           |**Read-write.** Defines the user the Icinga 2 daemon is running as. Used in the `init.conf` configuration file.
387 RunAsGroup          |**Read-write.** Defines the group the Icinga 2 daemon is running as. Used in the `init.conf` configuration file.
388 PlatformName        |**Read-only.** The name of the operating system, e.g. "Ubuntu".
389 PlatformVersion     |**Read-only.** The version of the operating system, e.g. "14.04.3 LTS".
390 PlatformKernel      |**Read-only.** The name of the operating system kernel, e.g. "Linux".
391 PlatformKernelVersion|**Read-only.** The version of the operating system kernel, e.g. "3.13.0-63-generic".
392 BuildCompilerName   |**Read-only.** The name of the compiler Icinga was built with, e.g. "Clang".
393 BuildCompilerVersion|**Read-only.** The version of the compiler Icinga was built with, e.g. "7.3.0.7030031".
394 BuildHostName       |**Read-only.** The name of the host Icinga was built on, e.g. "acheron".
395
396 ## <a id="apply"></a> Apply
397
398 The `apply` keyword can be used to create new objects which are associated with
399 another group of objects.
400
401     apply Service "ping" to Host {
402       import "generic-service"
403
404       check_command = "ping4"
405
406       assign where host.name == "localhost"
407     }
408
409 In this example the `assign where` condition is a boolean expression which is
410 evaluated for all objects of type `Host` and a new service with name "ping"
411 is created for each matching host. [Expression operators](17-language-reference.md#expression-operators)
412 may be used in `assign where` conditions.
413
414 The `to` keyword and the target type may be omitted if there is only one target
415 type, e.g. for the `Service` type.
416
417 Depending on the object type used in the `apply` expression additional local
418 variables may be available for use in the `where` condition:
419
420 Source Type       | Target Type | Variables
421 ------------------|-------------|--------------
422 Service           | Host        | host
423 Dependency        | Host        | host
424 Dependency        | Service     | host, service
425 Notification      | Host        | host
426 Notification      | Service     | host, service
427 ScheduledDowntime | Host        | host
428 ScheduledDowntime | Service     | host, service
429
430 Any valid config attribute can be accessed using the `host` and `service`
431 variables. For example, `host.address` would return the value of the host's
432 "address" attribute -- or null if that attribute isn't set.
433
434 More usage examples are documented in the [monitoring basics](3-monitoring-basics.md#using-apply-expressions)
435 chapter.
436
437 ## <a id="apply-for"></a> Apply For
438
439 [Apply](17-language-reference.md#apply) rules can be extended with the
440 [for loop](17-language-reference.md#for-loops) keyword.
441
442     apply Service "prefix-" for (key => value in host.vars.dictionary) to Host {
443       import "generic-service"
444
445       check_command = "ping4"
446       vars.host_value = value
447     }
448
449
450 Any valid config attribute can be accessed using the `host` and `service`
451 variables. The attribute must be of the Array or Dictionary type. In this example
452 `host.vars.dictionary` is of the Dictionary type which needs a key-value-pair
453 as iterator.
454
455 In this example all generated service object names consist of `prefix-` and
456 the value of the `key` iterator. The prefix string can be omitted if not required.
457
458 The `key` and `value` variables can be used for object attribute assignment, e.g. for
459 setting the `check_command` attribute or custom attributes as command parameters.
460
461 `apply for` rules are first evaluated against all objects matching the `for loop` list
462 and afterwards the `assign where` and `ignore where` conditions are evaluated.
463
464 It is not necessary to check attributes referenced in the `for loop` expression
465 for their existance using an additional `assign where` condition.
466
467 More usage examples are documented in the [monitoring basics](3-monitoring-basics.md#using-apply-for)
468 chapter.
469
470 ## <a id="group-assign"></a> Group Assign
471
472 Group objects can be assigned to specific member objects using the `assign where`
473 and `ignore where` conditions.
474
475     object HostGroup "linux-servers" {
476       display_name = "Linux Servers"
477
478       assign where host.vars.os == "Linux"
479     }
480
481 In this example the `assign where` condition is a boolean expression which is evaluated
482 for all objects of the type `Host`. Each matching host is added as member to the host group
483 with the name "linux-servers". Membership exclusion can be controlled using the `ignore where`
484 condition. [Expression operators](17-language-reference.md#expression-operators) may be used in `assign where` and
485 `ignore where` conditions.
486
487 Source Type       | Variables
488 ------------------|--------------
489 HostGroup         | host
490 ServiceGroup      | host, service
491 UserGroup         | user
492
493
494 ## <a id="boolean-values"></a> Boolean Values
495
496 The `assign where`, `ignore where`, `if` and `while`  statements, the `!` operator as
497 well as the `bool()` function convert their arguments to a boolean value based on the
498 following rules:
499
500 Description          | Example Value     | Boolean Value
501 ---------------------|-------------------|--------------
502 Empty value          | null              | false
503 Zero                 | 0                 | false
504 Non-zero integer     | -23945            | true
505 Empty string         | ""                | false
506 Non-empty string     | "Hello"           | true
507 Empty array          | []                | false
508 Non-empty array      | [ "Hello" ]       | true
509 Empty dictionary     | {}                | false
510 Non-empty dictionary | { key = "value" } | true
511
512 For a list of supported expression operators for `assign where` and `ignore where`
513 statements, see [expression operators](17-language-reference.md#expression-operators).
514
515 ## <a id="comments"></a> Comments
516
517 The Icinga 2 configuration format supports C/C++-style and shell-style comments.
518
519 Example:
520
521     /*
522      This is a comment.
523      */
524     object Host "localhost" {
525       check_interval = 30 // this is also a comment.
526       retry_interval = 15 # yet another comment
527     }
528
529 ## <a id="includes"></a> Includes
530
531 Other configuration files can be included using the `include` directive.
532 Paths must be relative to the configuration file that contains the
533 `include` directive.
534
535 Example:
536
537     include "some/other/file.conf"
538     include "conf.d/*.conf"
539
540 Wildcard includes are not recursive.
541
542 Icinga also supports include search paths similar to how they work in a
543 C/C++ compiler:
544
545     include <itl>
546
547 Note the use of angle brackets instead of double quotes. This causes the
548 config compiler to search the include search paths for the specified
549 file. By default $PREFIX/share/icinga2/include is included in the list of search
550 paths. Additional include search paths can be added using
551 [command-line options](11-cli-commands.md#config-include-path).
552
553 Wildcards are not permitted when using angle brackets.
554
555 ## <a id="recursive-includes"></a> Recursive Includes
556
557 The `include_recursive` directive can be used to recursively include all
558 files in a directory which match a certain pattern.
559
560 Example:
561
562     include_recursive "conf.d", "*.conf"
563     include_recursive "templates"
564
565 The first parameter specifies the directory from which files should be
566 recursively included.
567
568 The file names need to match the pattern given in the second parameter.
569 When no pattern is specified the default pattern "*.conf" is used.
570
571 ## <a id="zone-includes"></a> Zone Includes
572
573 The `include_zones` recursively includes all subdirectories for the
574 given path.
575
576 In addition to that it sets the `zone` attribute for all objects created
577 in these subdirectories to the name of the subdirectory.
578
579 Example:
580
581     include_zones "etc", "zones.d", "*.conf"
582     include_zones "puppet", "puppet-zones"
583
584 The first parameter specifies a tag name for this directive. Each `include_zones`
585 invocation should use a unique tag name. When copying the zones' configuration
586 files Icinga uses the tag name as the name for the destination directory in
587 `/var/lib/icinga2/api/config`.
588
589 The second parameter specifies the directory which contains the subdirectories.
590
591 The file names need to match the pattern given in the third parameter.
592 When no pattern is specified the default pattern "*.conf" is used.
593
594 ## <a id="library"></a> Library directive
595
596 The `library` directive can be used to manually load additional
597 libraries. Libraries can be used to provide additional object types and
598 functions.
599
600 Example:
601
602     library "snmphelper"
603
604 ## <a id="functions"></a> Functions
605
606 Functions can be defined using the `function` keyword.
607
608 Example:
609
610     function multiply(a, b) {
611       return a * b
612     }
613
614 When encountering the `return` keyword further execution of the function is terminated and
615 the specified value is supplied to the caller of the function:
616
617     log(multiply(3, 5))
618
619 In this example the `multiply` function we declared earlier is invoked with two arguments (3 and 5).
620 The function computes the product of those arguments and makes the result available to the
621 function's caller.
622
623 When no value is supplied for the `return` statement the function returns `null`.
624
625 Functions which do not have a `return` statement have their return value set to the value of the
626 last expression which was performed by the function. For example, we could have also written our
627 `multiply` function like this:
628
629     function multiply(a, b) {
630       a * b
631     }
632
633 Anonymous functions can be created by omitting the name in the function definition. The
634 resulting function object can be used like any other value:
635
636     var fn = function() { 3 }
637
638     fn() /* Returns 3 */
639
640 ## <a id="lambdas"></a> Lambda Expressions
641
642 Functions can also be declared using the alternative lambda syntax.
643
644 Example:
645
646     f = (x) => x * x
647
648 Multiple statements can be used by putting the function body into braces:
649
650     f = (x) => {
651       log("Lambda called")
652       x * x
653     }
654
655 Just like with ordinary functions the return value is the value of the last statement.
656
657 For lambdas which take exactly one argument the braces around the arguments can be omitted:
658
659     f = x => x * x
660
661 ## <a id="nullary-lambdas"></a> Abbreviated Lambda Syntax
662
663 Lambdas which take no arguments can also be written using the abbreviated lambda syntax.
664
665 Example:
666
667     f = {{ 3 }}
668
669 This creates a new function which returns the value 3.
670
671 ## <a id="variable-scopes"></a> Variable Scopes
672
673 When setting a variable Icinga checks the following scopes in this order whether the variable
674 already exists there:
675
676 * Local Scope
677 * `this` Scope
678 * Global Scope
679
680 The local scope contains variables which only exist during the invocation of the current function,
681 object or apply statement. Local variables can be declared using the `var` keyword:
682
683     function multiply(a, b) {
684       var temp = a * b
685       return temp
686     }
687
688 Each time the `multiply` function is invoked a new `temp` variable is used which is in no way
689 related to previous invocations of the function.
690
691 When setting a variable which has not previously been declared as local using the `var` keyword
692 the `this` scope is used.
693
694 The `this` scope refers to the current object which the function or object/apply statement
695 operates on.
696
697     object Host "localhost" {
698       check_interval = 5m
699     }
700
701 In this example the `this` scope refers to the "localhost" object. The `check_interval` attribute
702 is set for this particular host.
703
704 You can explicitly access the `this` scope using the `this` keyword:
705
706     object Host "localhost" {
707       var check_interval = 5m
708   
709       /* This explicitly specifies that the attribute should be set
710        * for the host, if we had omitted `this.` the (poorly named)
711        * local variable `check_interval` would have been modified instead.
712        */
713       this.check_interval = 1m 
714   }
715
716 Similarly the keywords `locals` and `globals` are available to access the local and global scope.
717
718 Functions also have a `this` scope. However unlike for object/apply statements the `this` scope for
719 a function is set to whichever object was used to invoke the function. Here's an example:
720
721      hm = {
722        h_word = null
723  
724        function init(word) {
725          h_word = word
726        }
727      }
728
729      /* Let's invoke the init() function */
730      hm.init("hello")
731
732 We're using `hm.init` to invoke the function which causes the value of `hm` to become the `this`
733 scope for this function call.
734
735 ## <a id="closures"></a> Closures
736
737 By default `function`s, `object`s and `apply` rules do not have access to variables declared
738 outside of their scope (except for global variables).
739
740 In order to access variables which are defined in the outer scope the `use` keyword can be used:
741
742     function MakeHelloFunction(name) {
743       return function() use(name) {
744         log("Hello, " + name)
745       }
746     }
747
748 In this case a new variable `name` is created inside the inner function's scope which has the
749 value of the `name` function argument.
750
751 Alternatively a different value for the inner variable can be specified:
752
753     function MakeHelloFunction(name) {
754       return function() use (greeting = "Hello, " + name) {
755         log(greeting)
756       }
757     }
758
759 ## <a id="conditional-statements"></a> Conditional Statements
760
761 Sometimes it can be desirable to only evaluate statements when certain conditions are met. The if/else
762 construct can be used to accomplish this.
763
764 Example:
765
766     a = 3
767
768     if (a < 5) {
769       a *= 7
770     } else if (a > 10) {
771       a *= 5
772     } else {
773       a *= 2
774     }
775
776 An if/else construct can also be used in place of any other value. The value of an if/else statement
777 is the value of the last statement which was evaluated for the branch which was taken:
778
779     a = if (true) {
780       log("Taking the 'true' branch")
781       7 * 3
782     } else {
783       log("Taking the 'false' branch")
784       9
785     }
786
787 This example prints the log message "Taking the 'true' branch" and the `a` variable is set to 21 (7 * 3).
788
789 The value of an if/else construct is null if the condition evaluates to false and no else branch is given.
790
791 ## <a id="while-loops"></a> While Loops
792
793 The `while` statement checks a condition and executes the loop body when the condition evaluates to `true`.
794 This is repeated until the condition is no longer true.
795
796 Example:
797
798     var num = 5
799
800     while (num > 5) {
801         log("Test")
802         num -= 1
803     }
804
805 The `continue` and `break` keywords can be used to control how the loop is executed: The `continue` keyword
806 skips over the remaining expressions for the loop body and begins the next loop evaluation. The `break` keyword
807 breaks out of the loop.
808
809 ## <a id="for-loops"></a> For Loops
810
811 The `for` statement can be used to iterate over arrays and dictionaries.
812
813 Example:
814
815     var list = [ "a", "b", "c" ]
816
817     for (item in list) {
818       log("Item: " + item)
819     }
820
821 The loop body is evaluated once for each item in the array. The variable `item` is declared as a local
822 variable just as if the `var` keyword had been used.
823
824 Iterating over dictionaries can be accomplished in a similar manner:
825
826     var dict = { a = 3, b = 7 }
827
828     for (key => value in dict) {
829       log("Key: " + key + ", Value: " + value)
830     }
831
832 The `continue` and `break` keywords can be used to control how the loop is executed: The `continue` keyword
833 skips over the remaining expressions for the loop body and begins the next loop evaluation. The `break` keyword
834 breaks out of the loop.
835
836 ## <a id="constructor"></a> Constructors
837
838 In order to create a new value of a specific type constructor calls may be used.
839
840 Example:
841
842     var pd = PerfdataValue()
843     pd.label = "test"
844     pd.value = 10
845
846 You can also try to convert an existing value to another type by specifying it as an argument for the constructor call.
847
848 Example:
849
850     var s = String(3) /* Sets s to "3". */
851
852 ## <a id="throw"></a> Exceptions
853
854 Built-in commands may throw exceptions to signal errors such as invalid arguments. User scripts can throw exceptions
855 using the `throw` keyword.
856
857 Example:
858
859     throw "An error occurred."
860
861 There is currently no way for scripts to catch exceptions.
862
863 ## <a id="breakpoints"></a> Breakpoints
864
865 The `debugger` keyword can be used to insert a breakpoint. It may be used at any place where an assignment would also be a valid expression.
866
867 By default breakpoints have no effect unless Icinga is started with the `--script-debugger` command-line option. When the script debugger is enabled Icinga stops execution of the script when it encounters a breakpoint and spawns a console which lets the user inspect the current state of the execution environment.
868
869 ## <a id="types"></a> Types
870
871 All values have a static type. The `typeof` function can be used to determine the type of a value:
872
873     typeof(3) /* Returns an object which represents the type for numbers */
874
875 The following built-in types are available:
876
877 Type       | Examples          | Description
878 -----------|-------------------|------------------------
879 Number     | 3.7               | A numerical value.
880 Boolean    | true, false       | A boolean value.
881 String     | "hello"           | A string.
882 Array      | [ "a", "b" ]      | An array.
883 Dictionary | { a = 3 }         | A dictionary.
884
885 Depending on which libraries are loaded additional types may become available. The `icinga`
886 library implements a whole bunch of other [object types](9-object-types.md#object-types),
887 e.g. Host, Service, CheckCommand, etc.
888
889 Each type has an associated type object which describes the type's semantics. These
890 type objects are made available using global variables which match the type's name:
891
892     /* This logs 'true' */
893     log(typeof(3) == Number)
894
895 The type object's `prototype` property can be used to find out which methods a certain type
896 supports:
897
898     /* This returns: ["contains","find","len","lower","replace","reverse","split","substr","to_string","trim","upper"] */
899     keys(String.prototype)
900
901 Additional documentation on type methods is available in the
902 [library reference](18-library-reference.md#library-reference).
903
904 ## <a id="location-information"></a> Location Information
905
906 The location of the currently executing script can be obtained using the
907 `current_filename` and `current_line` keywords.
908
909 Example:
910
911     log("Hello from '" + current_filename + "' in line " + current_line)
912
913 ## <a id="reserved-keywords"></a> Reserved Keywords
914
915 These keywords are reserved and must not be used as constants or custom attributes.
916
917     object
918     template
919     include
920     include_recursive
921     ignore_on_error
922     library
923     null
924     true
925     false
926     const
927     var
928     this
929     use
930     apply
931     to
932     where
933     import
934     assign
935     ignore
936     function
937     return
938     for
939     if
940     else
941     in
942     current_filename
943     current_line
944
945 You can escape reserved keywords using the `@` character. The following example
946 tries to set `vars.include` which references a reserved keyword and generates
947 an error:
948
949     [2014-09-15 17:24:00 +0200] critical/config: Location:
950     /etc/icinga2/conf.d/hosts/localhost.conf(13):   vars.sla = "24x7"
951     /etc/icinga2/conf.d/hosts/localhost.conf(14):
952     /etc/icinga2/conf.d/hosts/localhost.conf(15):   vars.include = "some cmdb export field"
953                                                          ^^^^^^^
954     /etc/icinga2/conf.d/hosts/localhost.conf(16): }
955     /etc/icinga2/conf.d/hosts/localhost.conf(17):
956
957     Config error: in /etc/icinga2/conf.d/hosts/localhost.conf: 15:8-15:14: syntax error, unexpected include (T_INCLUDE), expecting T_IDENTIFIER
958     [2014-09-15 17:24:00 +0200] critical/config: 1 errors, 0 warnings.
959
960 You can escape the `include` keyword by prefixing it with an additional `@` character:
961
962     object Host "localhost" {
963       import "generic-host"
964
965       address = "127.0.0.1"
966       address6 = "::1"
967
968       vars.os = "Linux"
969       vars.sla = "24x7"
970
971       vars.@include = "some cmdb export field"
972     }
973