]> granicus.if.org Git - icinga2/blob - doc/19-library-reference.md
2ac0914998149fc6dc5a89a86cf8e654d3dd341b
[icinga2] / doc / 19-library-reference.md
1 # <a id="library-reference"></a> Library Reference
2
3 ## <a id="global-functions"></a> Global functions
4
5 Function                        | Description
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).
30
31 ## <a id="object-accessor-functions"></a> Object Accessor Functions
32
33 These functions can be used to retrieve a reference to another object by name.
34
35 ### <a id="objref-get_check_command"></a> get_check_command
36
37 Signature:
38
39     function get_check_command(name);
40
41 Returns the CheckCommand object with the specified name, or `null` if no such CheckCommand object exists.
42
43 ### <a id="objref-get_event_command"></a> get_event_command
44
45 Signature:
46
47     function get_event_command(name);
48
49 Returns the EventCommand object with the specified name, or `null` if no such EventCommand object exists.
50
51 ### <a id="objref-get_notification_command"></a> get_notification_command
52
53 Signature:
54
55     function get_notification_command(name);
56
57 Returns the NotificationCommand object with the specified name, or `null` if no such NotificationCommand object exists.
58
59 ### <a id="objref-get_host"></a> get_host
60
61 Signature:
62
63     function get_host(host_name);
64
65 Returns the Host object with the specified name, or `null` if no such Host object exists.
66
67
68 ### <a id="objref-get_service"></a> get_service
69
70 Signature:
71
72     function get_service(host_name, service_name);
73
74 Returns the Service object with the specified name, or `null` if no such Service object exists.
75
76
77 ### <a id="objref-get_user"></a> get_user
78
79 Signature:
80
81     function get_user(name);
82
83 Returns the User object with the specified name, or `null` if no such User object exists.
84
85 ### <a id="objref-get_host_group"></a> get_host_group
86
87 Signature:
88
89     function get_host_group(name);
90
91 Returns the HostGroup object with the specified name, or `null` if no such HostGroup object exists.
92
93
94 ### <a id="objref-get_service_group"></a> get_service_group
95
96 Signature:
97
98     function get_service_group(name);
99
100 Returns the ServiceGroup object with the specified name, or `null` if no such ServiceGroup object exists.
101
102 ### <a id="objref-get_user_group"></a> get_user_group
103
104 Signature:
105
106     function get_user_group(name);
107
108 Returns the UserGroup object with the specified name, or `null` if no such UserGroup object exists.
109
110
111 ### <a id="objref-get_time_period"></a> get_time_period
112
113 Signature:
114
115     function get_time_period(name);
116
117 Returns the TimePeriod object with the specified name, or `null` if no such TimePeriod object exists.
118
119
120 ### <a id="objref-get_object"></a> get_object
121
122 Signature:
123
124     function get_object(type, name);
125
126 Returns the object with the specified type and name, or `null` if no such object exists. `type` must refer
127 to a type object.
128
129
130 ### <a id="objref-get_objects"></a> get_objects
131
132 Signature:
133
134     function get_objects(type);
135
136 Returns an array of objects whose type matches the specified type. `type` must refer
137 to a type object.
138
139
140 ## <a id="math-object"></a> Math object
141
142 The global `Math` object can be used to access a number of mathematical constants
143 and functions.
144
145 ### <a id="math-e"></a> Math.E
146
147 Euler's constant.
148
149 ### <a id="math-ln2"></a> Math.LN2
150
151 Natural logarithm of 2.
152
153 ### <a id="math-ln10"></a> Math.LN10
154
155 Natural logarithm of 10.
156
157 ### <a id="math-log2e"></a> Math.LOG2E
158
159 Base 2 logarithm of E.
160
161 ### <a id="math-pi"></a> Math.PI
162
163 The mathematical constant Pi.
164
165 ### <a id="math-sqrt1_2"></a> Math.SQRT1_2
166
167 Square root of 1/2.
168
169 ### <a id="math-sqrt2"></a> Math.SQRT2
170
171 Square root of 2.
172
173 ### <a id="math-abs"></a> Math.abs
174
175 Signature:
176
177     function abs(x);
178
179 Returns the absolute value of `x`.
180
181 ### <a id="math-acos"></a> Math.acos
182
183 Signature:
184
185     function acos(x);
186
187 Returns the arccosine of `x`.
188
189 ### <a id="math-asin"></a> Math.asin
190
191 Signature:
192
193     function asin(x);
194
195 Returns the arcsine of `x`.
196
197 ### <a id="math-atan"></a> Math.atan
198
199 Signature:
200
201     function atan(x);
202
203 Returns the arctangent of `x`.
204
205 ### <a id="math-atan2"></a> Math.atan2
206
207 Signature:
208
209     function atan2(y, x);
210
211 Returns the arctangent of the quotient of `y` and `x`.
212
213 ### <a id="math-ceil"></a> Math.ceil
214
215 Signature:
216
217     function ceil(x);
218
219 Returns the smallest integer value not less than `x`.
220
221 ### <a id="math-cos"></a> Math.cos
222
223 Signature:
224
225     function cos(x);
226
227 Returns the cosine of `x`.
228
229 ### <a id="math-exp"></a> Math.exp
230
231 Signature:
232
233     function exp(x);
234
235 Returns E raised to the `x`th power.
236
237 ### <a id="math-floor"></a> Math.floor
238
239 Signature:
240
241     function floor(x);
242
243 Returns the largest integer value not greater than `x`.
244
245 ### <a id="math-isinf"></a> Math.isinf
246
247 Signature:
248
249     function isinf(x);
250
251 Returns whether `x` is infinite.
252
253 ### <a id="math-isnan"></a> Math.isnan
254
255 Signature:
256
257     function isnan(x);
258
259 Returns whether `x` is NaN (not-a-number).
260
261 ### <a id="math-log"></a> Math.log
262
263 Signature:
264
265     function log(x);
266
267 Returns the natural logarithm of `x`.
268
269 ### <a id="math-max"></a> Math.max
270
271 Signature:
272
273     function max(...);
274
275 Returns the largest argument. A variable number of arguments can be specified.
276 If no arguments are given, -Infinity is returned.
277
278 ### <a id="math-min"></a> Math.min
279
280 Signature:
281
282     function min(...);
283
284 Returns the smallest argument. A variable number of arguments can be specified.
285 If no arguments are given, +Infinity is returned.
286
287 ### <a id="math-pow"></a> Math.pow
288
289 Signature:
290
291     function pow(x, y);
292
293 Returns `x` raised to the `y`th power.
294
295 ### <a id="math-random"></a> Math.random
296
297 Signature:
298
299     function random();
300
301 Returns a pseudo-random number between 0 and 1.
302
303 ### <a id="math-round"></a> Math.round
304
305 Signature:
306
307     function round(x);
308
309 Returns `x` rounded to the nearest integer value.
310
311 ### <a id="math-sign"></a> Math.sign
312
313 Signature:
314
315     function sign(x);
316
317 Returns -1 if `x` is negative, 1 if `x` is positive
318 and 0 if `x` is 0.
319
320 ### <a id="math-sin"></a> Math.sin
321
322 Signature:
323
324     function sin(x);
325
326 Returns the sine of `x`.
327
328 ### <a id="math-sqrt"></a> Math.sqrt
329
330 Signature:
331
332     function sqrt(x);
333
334 Returns the square root of `x`.
335
336 ### <a id="math-tan"></a> Math.tan
337
338 Signature:
339
340     function tan(x);
341
342 Returns the tangent of `x`.
343
344 ## <a id="json-object"></a> Json object
345
346 The global `Json` object can be used to encode and decode JSON.
347
348 ### <a id="json-encode"></a> Json.encode
349
350 Signature:
351
352     function encode(x);
353
354 Encodes an arbitrary value into JSON.
355
356 ### <a id="json-decode"></a> Json.decode
357
358 Signature:
359
360     function decode(x);
361
362 Decodes a JSON string.
363
364 ## <a id="number-type"></a> Number type
365
366 ### <a id="number-to_string"></a> Number#to_string
367
368 Signature:
369
370     function to_string();
371
372 The `to_string` method returns a string representation of the number.
373
374 Example:
375
376     var example = 7
377         example.to_string() /* Returns "7" */
378
379 ## <a id="boolean-type"></a> Boolean type
380
381 ### <a id="boolean-to_string"></a> Boolean#to_string
382
383 Signature:
384
385     function to_string();
386
387 The `to_string` method returns a string representation of the boolean value.
388
389 Example:
390
391     var example = true
392         example.to_string() /* Returns "true" */
393
394 ## <a id="string-type"></a> String type
395
396 ### <a id="string-find"></a> String#find
397
398 Signature:
399
400     function find(str, start);
401
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).
405
406 Example:
407
408     "Hello World".find("World") /* Returns 6 */
409
410 ### <a id="string-contains"></a> String#contains
411
412 Signature:
413
414     function contains(str);
415
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.
419
420 Example:
421
422     "Hello World".contains("World") /* Returns true */
423
424 ### <a id="string-len"></a> String#len
425
426 Signature
427
428     function len();
429
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.
432
433 Example:
434
435     "Hello World".len() /* Returns 11 */
436
437 ### <a id="string-lower"></a> String#lower
438
439 Signature:
440
441     function lower();
442
443 Returns a copy of the string with all of its characters converted to lower-case.
444
445 Example:
446
447     "Hello World".lower() /* Returns "hello world" */
448
449 ### <a id="string-upper"></a> String#upper
450
451 Signature:
452
453     function upper();
454
455 Returns a copy of the string with all of its characters converted to upper-case.
456
457 Example:
458
459     "Hello World".upper() /* Returns "HELLO WORLD" */
460
461 ### <a id="string-replace"></a> String#replace
462
463 Signature:
464
465     function replace(search, replacement);
466
467 Returns a copy of the string with all occurences of the string specified in `search` replaced
468 with the string specified in `replacement`.
469
470 ### <a id="string-split"></a> String#split
471
472 Signature:
473
474     function split(delimiters);
475
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.
478
479 Example:
480
481     "x-7,y".split("-,") /* Returns [ "x", "7", "y" ] */
482
483 ### <a id="string-substr"></a> String#substr
484
485 Signature:
486
487     function substr(start, len);
488
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).
491
492 Example:
493
494     "Hello World".substr(6) /* Returns "World" */
495
496 ### <a id="string-to_string"></a> String#to_string
497
498 Signature:
499
500     function to_string();
501
502 Returns a copy of the string.
503
504 ### <a id="string-reverse"></a> String#reverse
505
506 Signature:
507
508     function reverse();
509
510 Returns a copy of the string in reverse order.
511
512 ### <a id="string-trim"></a> String#trim
513
514 Signature:
515
516     function trim();
517
518 Removes trailing whitespaces and returns the string.
519
520 ## <a id="object-type"></a> Object type
521
522 This is the base type for all types in the Icinga application.
523
524 ### <a id="object-clone"></a> Object#clone
525
526 Signature:
527
528      function clone();
529
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.
533
534 ### <a id="object-to-string"></a> Object#to_string
535
536 Signature:
537
538     function to_string();
539
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
542 object's type.
543
544 Example:
545
546     [ 3, true ].to_string() /* Returns "[ 3.000000, true ]" */
547
548 ### <a id="object-type-field"></a> Object#type
549
550 Signature:
551
552     String type;
553
554 Returns the object's type name. This attribute is read-only.
555
556 Example:
557
558     get_host("localhost").type /* Returns "Host" */
559
560 ## <a id="type-type"></a> Type type
561
562 Inherits methods from the [Object type](19-library-reference.md#object-type).
563
564 The `Type` type provides information about the underlying type of an object or scalar value.
565
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.
567
568 ### <a id="type-base"></a> Type#base
569
570 Signature:
571
572     Type base;
573
574 Returns a reference to the type's base type. This attribute is read-only.
575
576 Example:
577
578     Dictionary.base == Object /* Returns true, because the Dictionary type inherits directly from the Object type. */
579
580 ### <a id="type-name"></a> Type#name
581
582 Signature:
583
584     String name;
585
586 Returns the name of the type.
587
588 ### <a id="type-prototype"></a> Type#prototype
589
590 Signature:
591
592     Object prototype;
593
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.
595
596 The prototype functionality is used to implement methods.
597
598 Example:
599
600     3.to_string() /* Even though '3' does not have a to_string property the Number type's prototype object does. */
601
602 ## <a id="array-type"></a> Array type
603
604 Inherits methods from the [Object type](19-library-reference.md#object-type).
605
606 ### <a id="array-add"></a> Array#add
607
608 Signature:
609
610     function add(value);
611
612 Adds a new value after the last element in the array.
613
614 ### <a id="array-clear"></a> Array#clear
615
616 Signature:
617
618     function clear();
619
620 Removes all elements from the array.
621
622 ### <a id="array-shallow-clone"></a> Array#shallow_clone
623
624     function shallow_clone();
625
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.
628
629 ### <a id="array-contains"></a> Array#contains
630
631 Signature:
632
633     function contains(value);
634
635 Returns true if the array contains the specified value, false otherwise.
636
637 ### <a id="array-len"></a> Array#len
638
639 Signature:
640
641     function len();
642
643 Returns the number of elements contained in the array.
644
645 ### <a id="array-remove"></a> Array#remove
646
647 Signature:
648
649     function remove(index);
650
651 Removes the element at the specified zero-based index.
652
653 ### <a id="array-set"></a> Array#set
654
655 Signature:
656
657     function set(index, value);
658
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.
661
662 ### <a id="array-get"></a> Array#get
663
664 Signature:
665
666     function get(index);
667
668 Retrieves the element at the specified zero-based index.
669
670 ### <a id="array-sort"></a> Array#sort
671
672 Signature:
673
674     function sort(less_cmp);
675
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.
679
680 ### <a id="array-join"></a> Array#join
681
682 Signature:
683
684     function join(separator);
685
686 Joins all elements of the array using the specified separator.
687
688 ### <a id="array-reverse"></a> Array#reverse
689
690 Signature:
691
692     function reverse();
693
694 Returns a new array with all elements of the current array in reverse order.
695
696 ### <a id="array-map"></a> Array#map
697
698 Signature:
699
700     function map(func);
701
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.
704
705 ### <a id="array-reduce"></a> Array#reduce
706
707 Signature:
708
709     function reduce(func);
710
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.
714
715 ### <a id="array-filter"></a> Array#filter
716
717 Signature:
718
719     function filter(func);
720
721 Returns a copy of the array containing only the elements for which `func(element)`
722 is true.
723
724 ### <a id="array-unique"></a> Array#unique
725
726 Signature:
727
728     function unique();
729
730 Returns a copy of the array with all duplicate elements removed. The original order
731 of the array is not preserved.
732
733 ## <a id="dictionary-type"></a> Dictionary type
734
735 Inherits methods from the [Object type](19-library-reference.md#object-type).
736
737 ### <a id="dictionary-shallow-clone"></a> Dictionary#shallow_clone
738
739 Signature:
740
741     function shallow_clone();
742
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.
745
746 ### <a id="dictionary-contains"></a> Dictionary#contains
747
748 Signature:
749
750     function contains(key);
751
752 Returns true if a dictionary item with the specified `key` exists, false otherwise.
753
754 ### <a id="dictionary-len"></a> Dictionary#len
755
756 Signature:
757
758     function len();
759
760 Returns the number of items contained in the dictionary.
761
762 ### <a id="dictionary-remove"></a> Dictionary#remove
763
764 Signature:
765
766     function remove(key);
767
768 Removes the item with the specified `key`. Trying to remove an item which does not exist
769 is a no-op.
770
771 ### <a id="dictionary-set"></a> Dictionary#set
772
773 Signature:
774
775     function set(key, value);
776
777 Creates or updates an item with the specified `key` and `value`.
778
779 ### <a id="dictionary-get"></a> Dictionary#get
780
781 Signature:
782
783     function get(key);
784
785 Retrieves the value for the specified `key`. Returns `null` if they `key` does not exist
786 in the dictionary.
787
788 ### <a id="dictionary-keys"></a> Dictionary#keys
789
790 Signature:
791
792     function keys();
793
794 Returns a list of keys for all items that are currently in the dictionary.
795
796 ## <a id="scriptfunction-type"></a> Function type
797
798 Inherits methods from the [Object type](19-library-reference.md#object-type).
799
800 ### <a id="scriptfunction-call"></a> Function#call
801
802 Signature:
803
804     function call(thisArg, ...);
805
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.
808
809 Example:
810
811     function set_x(val) {
812           this.x = val
813         }
814         
815         dict = {}
816         
817         set_x.call(dict, 7) /* Invokes set_x using `dict` as `this` */
818
819 ### <a id="scriptfunction-callv"></a> Function#callv
820
821 Signature:
822
823     function callv(thisArg, args);
824
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.
827
828 Example:
829
830     function set_x(val) {
831           this.x = val
832         }
833         
834         var dict = {}
835
836         var args = [ 7 ]
837
838         set_x.callv(dict, args) /* Invokes set_x using `dict` as `this` */
839
840 ## <a id="datetime-type"></a> DateTime type
841
842 Inherits methods from the [Object type](19-library-reference.md#object-type).
843
844 ### <a id="datetime-ctor"></a> DateTime constructor
845
846 Signature:
847
848     function DateTime()
849     function DateTime(unixTimestamp)
850     function DateTime(year, month, day)
851     function DateTime(year, month, day, hours, minutes, seconds)
852
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.
855
856 Example:
857
858     var d1 = DateTime() /* current time */
859     var d2 = DateTime(2016, 5, 21) /* midnight April 21st, 2016 (local time) */
860
861 ### <a id="datetime-arithmetic"></a> DateTime arithmetic
862
863 Subtracting two DateTime objects yields the interval between them, in seconds.
864
865 Example:
866
867     var delta = DateTime() - DateTime(2016, 5, 21) /* seconds since midnight April 21st, 2016 */
868
869 Subtracting a number from a DateTime object yields a new DateTime object that is further in the past:
870
871 Example:
872
873     var dt = DateTime() - 2 * 60 * 60 /* Current time minus 2 hours */
874
875 Adding a number to a DateTime object yields a new DateTime object that is in the future:
876
877 Example:
878
879     var dt = DateTime() + 24 * 60 60 /* Current time plus 24 hours */
880
881 ### <a id="datetime-format"></a> DateTime#format
882
883 Signature:
884
885     function format(fmt)
886
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).
889
890 Example:
891
892     var s = DateTime(2016, 4, 21).format("%A") /* Sets s to "Thursday". */
893
894 ### <a id="datetime-tostring"></a> DateTime#to_string
895
896 Signature:
897
898     function to_string()
899
900 Returns a string representation for the DateTime object. Uses a suitable default format.
901
902 Example:
903
904     var s = DateTime(2016, 4, 21).to_string() /* Sets s to "2016-04-21 00:00:00 +0200". */