1 # <a id="library-reference"></a> Library Reference
3 ## <a id="global-functions"></a> Global functions
6 --------------------------------|-----------------------
7 regex(pattern, text) | Returns true if the regex pattern matches the text, false otherwise.
8 match(pattern, text) | Returns true if the wildcard pattern matches the text, false otherwise.
9 cidr_match(pattern, ip) | Returns true if the CIDR pattern matches the IP address, false otherwise. IPv4 addresses are converted to IPv4-mapped IPv6 addresses before being matched against the pattern.
10 len(value) | Returns the length of the value, i.e. the number of elements for an array or dictionary, or the length of the string in bytes.
11 union(array, array, ...) | Returns an array containing all unique elements from the specified arrays.
12 intersection(array, array, ...) | Returns an array containing all unique elements which are common to all specified arrays.
13 keys(dict) | Returns an array containing the dictionary's keys.
14 string(value) | Converts the value to a string.
15 number(value) | Converts the value to a number.
16 bool(value) | Converts the value to a bool.
17 random() | Returns a random value between 0 and RAND_MAX (as defined in stdlib.h).
18 log(value) | Writes a message to the log. Non-string values are converted to a JSON string.
19 log(severity, facility, value) | Writes a message to the log. `severity` can be one of `LogDebug`, `LogNotice`, `LogInformation`, `LogWarning`, and `LogCritical`. Non-string values are converted to a JSON string.
20 typeof(value) | Returns the [Type](19-library-reference.md#type-type) object for a value.
21 get_time() | Returns the current UNIX timestamp.
22 parse_performance_data(pd) | Parses a performance data string and returns an array describing the values.
23 dirname(path) | Returns the directory portion of the specified path.
24 basename(path) | Returns the filename portion of the specified path.
25 escape\_shell\_arg(text) | Escapes a string for use as a single shell argument.
26 escape\_shell\_cmd(text) | Escapes shell meta characters in a string.
27 escape\_create\_process\_arg(text)| (Windows only) Escapes a string for use as an argument for CreateProcess().
28 exit(integer) | Terminates the application.
29 sleep(interval) | Sleeps for the specified amount of time (in seconds).
31 ## <a id="object-accessor-functions"></a> Object Accessor Functions
33 These functions can be used to retrieve a reference to another object by name.
35 ### <a id="objref-get_check_command"></a> get_check_command
39 function get_check_command(name);
41 Returns the CheckCommand object with the specified name, or `null` if no such CheckCommand object exists.
43 ### <a id="objref-get_event_command"></a> get_event_command
47 function get_event_command(name);
49 Returns the EventCommand object with the specified name, or `null` if no such EventCommand object exists.
51 ### <a id="objref-get_notification_command"></a> get_notification_command
55 function get_notification_command(name);
57 Returns the NotificationCommand object with the specified name, or `null` if no such NotificationCommand object exists.
59 ### <a id="objref-get_host"></a> get_host
63 function get_host(host_name);
65 Returns the Host object with the specified name, or `null` if no such Host object exists.
68 ### <a id="objref-get_service"></a> get_service
72 function get_service(host_name, service_name);
74 Returns the Service object with the specified name, or `null` if no such Service object exists.
77 ### <a id="objref-get_user"></a> get_user
81 function get_user(name);
83 Returns the User object with the specified name, or `null` if no such User object exists.
85 ### <a id="objref-get_host_group"></a> get_host_group
89 function get_host_group(name);
91 Returns the HostGroup object with the specified name, or `null` if no such HostGroup object exists.
94 ### <a id="objref-get_service_group"></a> get_service_group
98 function get_service_group(name);
100 Returns the ServiceGroup object with the specified name, or `null` if no such ServiceGroup object exists.
102 ### <a id="objref-get_user_group"></a> get_user_group
106 function get_user_group(name);
108 Returns the UserGroup object with the specified name, or `null` if no such UserGroup object exists.
111 ### <a id="objref-get_time_period"></a> get_time_period
115 function get_time_period(name);
117 Returns the TimePeriod object with the specified name, or `null` if no such TimePeriod object exists.
120 ### <a id="objref-get_object"></a> get_object
124 function get_object(type, name);
126 Returns the object with the specified type and name, or `null` if no such object exists. `type` must refer
130 ### <a id="objref-get_objects"></a> get_objects
134 function get_objects(type);
136 Returns an array of objects whose type matches the specified type. `type` must refer
140 ## <a id="math-object"></a> Math object
142 The global `Math` object can be used to access a number of mathematical constants
145 ### <a id="math-e"></a> Math.E
149 ### <a id="math-ln2"></a> Math.LN2
151 Natural logarithm of 2.
153 ### <a id="math-ln10"></a> Math.LN10
155 Natural logarithm of 10.
157 ### <a id="math-log2e"></a> Math.LOG2E
159 Base 2 logarithm of E.
161 ### <a id="math-pi"></a> Math.PI
163 The mathematical constant Pi.
165 ### <a id="math-sqrt1_2"></a> Math.SQRT1_2
169 ### <a id="math-sqrt2"></a> Math.SQRT2
173 ### <a id="math-abs"></a> Math.abs
179 Returns the absolute value of `x`.
181 ### <a id="math-acos"></a> Math.acos
187 Returns the arccosine of `x`.
189 ### <a id="math-asin"></a> Math.asin
195 Returns the arcsine of `x`.
197 ### <a id="math-atan"></a> Math.atan
203 Returns the arctangent of `x`.
205 ### <a id="math-atan2"></a> Math.atan2
209 function atan2(y, x);
211 Returns the arctangent of the quotient of `y` and `x`.
213 ### <a id="math-ceil"></a> Math.ceil
219 Returns the smallest integer value not less than `x`.
221 ### <a id="math-cos"></a> Math.cos
227 Returns the cosine of `x`.
229 ### <a id="math-exp"></a> Math.exp
235 Returns E raised to the `x`th power.
237 ### <a id="math-floor"></a> Math.floor
243 Returns the largest integer value not greater than `x`.
245 ### <a id="math-isinf"></a> Math.isinf
251 Returns whether `x` is infinite.
253 ### <a id="math-isnan"></a> Math.isnan
259 Returns whether `x` is NaN (not-a-number).
261 ### <a id="math-log"></a> Math.log
267 Returns the natural logarithm of `x`.
269 ### <a id="math-max"></a> Math.max
275 Returns the largest argument. A variable number of arguments can be specified.
276 If no arguments are given, -Infinity is returned.
278 ### <a id="math-min"></a> Math.min
284 Returns the smallest argument. A variable number of arguments can be specified.
285 If no arguments are given, +Infinity is returned.
287 ### <a id="math-pow"></a> Math.pow
293 Returns `x` raised to the `y`th power.
295 ### <a id="math-random"></a> Math.random
301 Returns a pseudo-random number between 0 and 1.
303 ### <a id="math-round"></a> Math.round
309 Returns `x` rounded to the nearest integer value.
311 ### <a id="math-sign"></a> Math.sign
317 Returns -1 if `x` is negative, 1 if `x` is positive
320 ### <a id="math-sin"></a> Math.sin
326 Returns the sine of `x`.
328 ### <a id="math-sqrt"></a> Math.sqrt
334 Returns the square root of `x`.
336 ### <a id="math-tan"></a> Math.tan
342 Returns the tangent of `x`.
344 ## <a id="json-object"></a> Json object
346 The global `Json` object can be used to encode and decode JSON.
348 ### <a id="json-encode"></a> Json.encode
354 Encodes an arbitrary value into JSON.
356 ### <a id="json-decode"></a> Json.decode
362 Decodes a JSON string.
364 ## <a id="number-type"></a> Number type
366 ### <a id="number-to_string"></a> Number#to_string
370 function to_string();
372 The `to_string` method returns a string representation of the number.
377 example.to_string() /* Returns "7" */
379 ## <a id="boolean-type"></a> Boolean type
381 ### <a id="boolean-to_string"></a> Boolean#to_string
385 function to_string();
387 The `to_string` method returns a string representation of the boolean value.
392 example.to_string() /* Returns "true" */
394 ## <a id="string-type"></a> String type
396 ### <a id="string-find"></a> String#find
400 function find(str, start);
402 Returns the zero-based index at which the string `str` was found in the string. If the string
403 was not found, -1 is returned. `start` specifies the zero-based index at which `find` should
404 start looking for the string (defaults to 0 when not specified).
408 "Hello World".find("World") /* Returns 6 */
410 ### <a id="string-contains"></a> String#contains
414 function contains(str);
416 Returns `true` if the string `str` was found in the string. If the string
417 was not found, `false` is returned. Use [find](19-library-reference.md#string-find)
418 for getting the index instead.
422 "Hello World".contains("World") /* Returns true */
424 ### <a id="string-len"></a> String#len
430 Returns the length of the string in bytes. Note that depending on the encoding type of the string
431 this is not necessarily the number of characters.
435 "Hello World".len() /* Returns 11 */
437 ### <a id="string-lower"></a> String#lower
443 Returns a copy of the string with all of its characters converted to lower-case.
447 "Hello World".lower() /* Returns "hello world" */
449 ### <a id="string-upper"></a> String#upper
455 Returns a copy of the string with all of its characters converted to upper-case.
459 "Hello World".upper() /* Returns "HELLO WORLD" */
461 ### <a id="string-replace"></a> String#replace
465 function replace(search, replacement);
467 Returns a copy of the string with all occurences of the string specified in `search` replaced
468 with the string specified in `replacement`.
470 ### <a id="string-split"></a> String#split
474 function split(delimiters);
476 Splits a string into individual parts and returns them as an array. The `delimiters` argument
477 specifies the characters which should be used as delimiters between parts.
481 "x-7,y".split("-,") /* Returns [ "x", "7", "y" ] */
483 ### <a id="string-substr"></a> String#substr
487 function substr(start, len);
489 Returns a part of a string. The `start` argument specifies the zero-based index at which the part begins.
490 The optional `len` argument specifies the length of the part ("until the end of the string" if omitted).
494 "Hello World".substr(6) /* Returns "World" */
496 ### <a id="string-to_string"></a> String#to_string
500 function to_string();
502 Returns a copy of the string.
504 ### <a id="string-reverse"></a> String#reverse
510 Returns a copy of the string in reverse order.
512 ### <a id="string-trim"></a> String#trim
518 Removes trailing whitespaces and returns the string.
520 ## <a id="object-type"></a> Object type
522 This is the base type for all types in the Icinga application.
524 ### <a id="object-clone"></a> Object#clone
530 Returns a copy of the object. Note that for object elements which are
531 reference values (e.g. objects such as arrays or dictionaries) the entire
532 object is recursively copied.
534 ### <a id="object-to-string"></a> Object#to_string
538 function to_string();
540 Returns a string representation for the object. Unless overridden this returns a string
541 of the format "Object of type '<typename>'" where <typename> is the name of the
546 [ 3, true ].to_string() /* Returns "[ 3.000000, true ]" */
548 ### <a id="object-type-field"></a> Object#type
554 Returns the object's type name. This attribute is read-only.
558 get_host("localhost").type /* Returns "Host" */
560 ## <a id="type-type"></a> Type type
562 Inherits methods from the [Object type](19-library-reference.md#object-type).
564 The `Type` type provides information about the underlying type of an object or scalar value.
566 All types are registered as global variables. For example, in order to obtain a reference to the `String` type the global variable `String` can be used.
568 ### <a id="type-base"></a> Type#base
574 Returns a reference to the type's base type. This attribute is read-only.
578 Dictionary.base == Object /* Returns true, because the Dictionary type inherits directly from the Object type. */
580 ### <a id="type-name"></a> Type#name
586 Returns the name of the type.
588 ### <a id="type-prototype"></a> Type#prototype
594 Returns the prototype object for the type. When an attribute is accessed on an object that doesn't exist the prototype object is checked to see if an attribute with the requested name exists. If it does, the attribute's value is returned.
596 The prototype functionality is used to implement methods.
600 3.to_string() /* Even though '3' does not have a to_string property the Number type's prototype object does. */
602 ## <a id="array-type"></a> Array type
604 Inherits methods from the [Object type](19-library-reference.md#object-type).
606 ### <a id="array-add"></a> Array#add
612 Adds a new value after the last element in the array.
614 ### <a id="array-clear"></a> Array#clear
620 Removes all elements from the array.
622 ### <a id="array-shallow-clone"></a> Array#shallow_clone
624 function shallow_clone();
626 Returns a copy of the array. Note that for elements which are reference values (e.g. objects such
627 as arrays and dictionaries) only the references are copied.
629 ### <a id="array-contains"></a> Array#contains
633 function contains(value);
635 Returns true if the array contains the specified value, false otherwise.
637 ### <a id="array-len"></a> Array#len
643 Returns the number of elements contained in the array.
645 ### <a id="array-remove"></a> Array#remove
649 function remove(index);
651 Removes the element at the specified zero-based index.
653 ### <a id="array-set"></a> Array#set
657 function set(index, value);
659 Sets the element at the zero-based index to the specified value. The `index` must refer to an element
660 which already exists in the array.
662 ### <a id="array-get"></a> Array#get
668 Retrieves the element at the specified zero-based index.
670 ### <a id="array-sort"></a> Array#sort
674 function sort(less_cmp);
676 Returns a copy of the array where all items are sorted. The items are
677 compared using the `<` (less-than) operator. A custom comparator function
678 can be specified with the `less_cmp` argument.
680 ### <a id="array-join"></a> Array#join
684 function join(separator);
686 Joins all elements of the array using the specified separator.
688 ### <a id="array-reverse"></a> Array#reverse
694 Returns a new array with all elements of the current array in reverse order.
696 ### <a id="array-map"></a> Array#map
702 Calls `func(element)` for each of the elements in the array and returns
703 a new array containing the return values of these function calls.
705 ### <a id="array-reduce"></a> Array#reduce
709 function reduce(func);
711 Reduces the elements of the array into a single value by calling the provided
712 function `func` as `func(a, b)` repeatedly where `a` and `b` are elements of the array
713 or results from previous function calls.
715 ### <a id="array-filter"></a> Array#filter
719 function filter(func);
721 Returns a copy of the array containing only the elements for which `func(element)`
724 ### <a id="array-unique"></a> Array#unique
730 Returns a copy of the array with all duplicate elements removed. The original order
731 of the array is not preserved.
733 ## <a id="dictionary-type"></a> Dictionary type
735 Inherits methods from the [Object type](19-library-reference.md#object-type).
737 ### <a id="dictionary-shallow-clone"></a> Dictionary#shallow_clone
741 function shallow_clone();
743 Returns a copy of the dictionary. Note that for elements which are reference values (e.g. objects such
744 as arrays and dictionaries) only the references are copied.
746 ### <a id="dictionary-contains"></a> Dictionary#contains
750 function contains(key);
752 Returns true if a dictionary item with the specified `key` exists, false otherwise.
754 ### <a id="dictionary-len"></a> Dictionary#len
760 Returns the number of items contained in the dictionary.
762 ### <a id="dictionary-remove"></a> Dictionary#remove
766 function remove(key);
768 Removes the item with the specified `key`. Trying to remove an item which does not exist
771 ### <a id="dictionary-set"></a> Dictionary#set
775 function set(key, value);
777 Creates or updates an item with the specified `key` and `value`.
779 ### <a id="dictionary-get"></a> Dictionary#get
785 Retrieves the value for the specified `key`. Returns `null` if they `key` does not exist
788 ### <a id="dictionary-keys"></a> Dictionary#keys
794 Returns a list of keys for all items that are currently in the dictionary.
796 ## <a id="scriptfunction-type"></a> Function type
798 Inherits methods from the [Object type](19-library-reference.md#object-type).
800 ### <a id="scriptfunction-call"></a> Function#call
804 function call(thisArg, ...);
806 Invokes the function using an alternative `this` scope. The `thisArg` argument specifies the `this`
807 scope for the function. All other arguments are passed directly to the function.
811 function set_x(val) {
817 set_x.call(dict, 7) /* Invokes set_x using `dict` as `this` */
819 ### <a id="scriptfunction-callv"></a> Function#callv
823 function callv(thisArg, args);
825 Invokes the function using an alternative `this` scope. The `thisArg` argument specifies the `this`
826 scope for the function. The items in the `args` array are passed to the function as individual arguments.
830 function set_x(val) {
838 set_x.callv(dict, args) /* Invokes set_x using `dict` as `this` */
840 ## <a id="datetime-type"></a> DateTime type
842 Inherits methods from the [Object type](19-library-reference.md#object-type).
844 ### <a id="datetime-ctor"></a> DateTime constructor
849 function DateTime(unixTimestamp)
850 function DateTime(year, month, day)
851 function DateTime(year, month, day, hours, minutes, seconds)
853 Constructs a new DateTime object. When no arguments are specified for the constructor a new
854 DateTime object representing the current time is created.
858 var d1 = DateTime() /* current time */
859 var d2 = DateTime(2016, 5, 21) /* midnight April 21st, 2016 (local time) */
861 ### <a id="datetime-arithmetic"></a> DateTime arithmetic
863 Subtracting two DateTime objects yields the interval between them, in seconds.
867 var delta = DateTime() - DateTime(2016, 5, 21) /* seconds since midnight April 21st, 2016 */
869 Subtracting a number from a DateTime object yields a new DateTime object that is further in the past:
873 var dt = DateTime() - 2 * 60 * 60 /* Current time minus 2 hours */
875 Adding a number to a DateTime object yields a new DateTime object that is in the future:
879 var dt = DateTime() + 24 * 60 60 /* Current time plus 24 hours */
881 ### <a id="datetime-format"></a> DateTime#format
887 Returns a string representation for the DateTime object using the specified format string.
888 The format string may contain format conversion placeholders as specified in strftime(3).
892 var s = DateTime(2016, 4, 21).format("%A") /* Sets s to "Thursday". */
894 ### <a id="datetime-tostring"></a> DateTime#to_string
900 Returns a string representation for the DateTime object. Uses a suitable default format.
904 var s = DateTime(2016, 4, 21).to_string() /* Sets s to "2016-04-21 00:00:00 +0200". */