Additional JavaScript objects

Overview

This section describes Zabbix additions to the JavaScript language implemented with Duktape and supported global JavaScript functions.

Built-in objects

Zabbix

The Zabbix object provides interaction with the internal Zabbix functionality.

MethodDescription
log(loglevel, message)Writes <message> into Zabbix log using <loglevel> log level (see configuration file DebugLevel parameter).

Example:

  1. Zabbix.log(3, "this is a log entry written with 'Warning' log level")

You may use the following aliases:

AliasAlias to
console.log(object)Zabbix.log(4, JSON.stringify(object))
console.warn(object)Zabbix.log(3, JSON.stringify(object))
console.error(object)Zabbix.log(2, JSON.stringify(object))
MethodDescription
sleep(delay)Delay JavaScript execution by delay milliseconds.

Example (delay execution by 15 seconds):

  1. Zabbix.sleep(15000)

HttpRequest

This object encapsulates cURL handle allowing to make simple HTTP requests. Errors are thrown as exceptions.

HttpRequest is a new name for this object since Zabbix 5.4. Previously it used to be called CurlHttpRequest. Method names have also been changed in Zabbix 5.4. The old object/method names are now deprecated and their support will be discontinued after Zabbix 6.0.

MethodDescription
addHeader(name, value)Adds HTTP header field. This field is used for all following requests until cleared with the clearHeader() method.
clearHeader()Clears HTTP header. If no header fields are set, HttpRequest will set Content-Type to application/json if the data being posted is JSON-formatted; text/plain otherwise.
connect(url)Sends HTTP CONNECT request to the URL and returns the response.
customRequest(method, url, data)Allows to specify any HTTP method in the first parameter. Sends the method request to the URL with optional data payload and returns the response.
delete(url, data)Sends HTTP DELETE request to the URL with optional data payload and returns the response.
getHeaders()Returns object of received HTTP header fields.
get(url, data)Sends HTTP GET request to the URL with optional data payload and returns the response.
head(url)Sends HTTP HEAD request to the URL and returns the response.
options(url)Sends HTTP OPTIONS request to the URL and returns the response.
patch(url, data)Sends HTTP PATCH request to the URL with optional data payload and returns the response.
put(url, data)Sends HTTP PUT request to the URL with optional data payload and returns the response.
post(url, data)Sends HTTP POST request to the URL with optional data payload and returns the response.
getStatus()Returns the status code of the last HTTP request.
setProxy(proxy)Sets HTTP proxy to “proxy” value. If this parameter is empty then no proxy is used.
setHttpAuth(bitmask, username, password)Sets enabled HTTP authentication methods (HTTPAUTH_BASIC, HTTPAUTH_DIGEST, HTTPAUTH_NEGOTIATE, HTTPAUTH_NTLM, HTTPAUTH_NONE) in the ‘bitmask’ parameter.
The HTTPAUTH_NONE flag allows to disable HTTP authentication.
Examples:
request.setHttpAuth(HTTPAUTH_NTLM | HTTPAUTH_BASIC, username, password)
request.setHttpAuth(HTTPAUTH_NONE)
trace(url, data)Sends HTTP TRACE request to the URL with optional data payload and returns the response.

Example:

  1. try {
  2. Zabbix.log(4, 'jira webhook script value='+value);
  3. var result = {
  4. 'tags': {
  5. 'endpoint': 'jira'
  6. }
  7. },
  8. params = JSON.parse(value),
  9. req = new HttpRequest(),
  10. fields = {},
  11. resp;
  12. req.addHeader('Content-Type: application/json');
  13. req.addHeader('Authorization: Basic '+params.authentication);
  14. fields.summary = params.summary;
  15. fields.description = params.description;
  16. fields.project = {"key": params.project_key};
  17. fields.issuetype = {"id": params.issue_id};
  18. resp = req.post('https://tsupport.zabbix.lan/rest/api/2/issue/',
  19. JSON.stringify({"fields": fields})
  20. );
  21. if (req.getStatus() != 201) {
  22. throw 'Response code: '+req.getStatus();
  23. }
  24. resp = JSON.parse(resp);
  25. result.tags.issue_id = resp.id;
  26. result.tags.issue_key = resp.key;
  27. } catch (error) {
  28. Zabbix.log(4, 'jira issue creation failed json : '+JSON.stringify({"fields": fields}));
  29. Zabbix.log(4, 'jira issue creation failed : '+error);
  30. result = {};
  31. }
  32. return JSON.stringify(result);

XML

The XML object allows the processing of XML data in the item and low-level discovery preprocessing and webhooks.

In order to use XML object, server/proxy must be compiled with libxml2 support.

MethodDescription
XML.query(expression, data)Retrieves node content using XPath. Returns null if node is not found.
expression - an XPath expression;
data - XML data as a string.
XML.toJson(data)Converts data in XML format to JSON.
XML.fromJson(object)Converts data in JSON format to XML.

Example:

Input:

  1. <menu>
  2. <food type = "breakfast">
  3. <name>Chocolate</name>
  4. <price>$5.95</price>
  5. <description></description>
  6. <calories>650</calories>
  7. </food>
  8. </menu>

Output:

  1. {
  2. "menu": {
  3. "food": {
  4. "@type": "breakfast",
  5. "name": "Chocolate",
  6. "price": "$5.95",
  7. "description": null,
  8. "calories": "650"
  9. }
  10. }
  11. }
Serialization rules

XML to JSON conversion will be processed according to the following rules (for JSON to XML conversions reversed rules are applied):

1. XML attributes will be converted to keys that have their names prepended with ‘@’.

Example:

Input:

  1. <xml foo="FOO">
  2. <bar>
  3. <baz>BAZ</baz>
  4. </bar>
  5. </xml>

Output:

  1. {
  2. "xml": {
  3. "@foo": "FOO",
  4. "bar": {
  5. "baz": "BAZ"
  6. }
  7. }
  8. }

2. Self-closing elements (<foo/>) will be converted as having ‘null’ value.

Example:

Input:

  1. <xml>
  2. <foo/>
  3. </xml>

Output:

  1. {
  2. "xml": {
  3. "foo": null
  4. }
  5. }

3. Empty attributes (with “” value) will be converted as having empty string (‘’) value.

Example:

Input:

  1. <xml>
  2. <foo bar="" />
  3. </xml>

Output:

  1. {
  2. "xml": {
  3. "foo": {
  4. "@bar": ""
  5. }
  6. }
  7. }

4. Multiple child nodes with the same element name will be converted to a single key that has an array of values as its value.

Example:

Input:

  1. <xml>
  2. <foo>BAR</foo>
  3. <foo>BAZ</foo>
  4. <foo>QUX</foo>
  5. </xml>

Output:

  1. {
  2. "xml": {
  3. "foo": ["BAR", "BAZ", "QUX"]
  4. }
  5. }

5. If a text element has no attributes and no children, it will be converted as a string.

Example:

Input:

  1. <xml>
  2. <foo>BAZ</foo>
  3. </xml>

Output:

  1. {
  2. "xml": {
  3. "foo": "BAZ"
  4. }
  5. }

6. If a text element has no children, but has attributes: text content will be converted to an element with the key ‘#text’ and content as a value; attributes will be converted as described in the serialization rule 1.

Example:

Input:

  1. <xml>
  2. <foo bar="BAR">
  3. BAZ
  4. </foo>
  5. </xml>

Output:

  1. {
  2. "xml": {
  3. "foo": {
  4. "@bar": "BAR",
  5. "#text": "BAZ"
  6. }
  7. }
  8. }

Global JavaScript functions

Additional global JavaScript functions have been implemented with Duktape:

  • btoa(string) - encodes string to base64 string
  • atob(base64_string) - decodes base64 string
  1. try {
  2. b64 = btoa("utf8 string");
  3. utf8 = atob(b64);
  4. }
  5. catch (error) {
  6. return {'error.name' : error.name, 'error.message' : error.message}
  7. }
  • md5(string) - calculates the MD5 hash of a string
  • sha256(string) - calculates the SHA256 hash of a string