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