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.

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

Example:

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

The total size of all logged messages is limited to 8 MB per script execution.

Method Description
sleep(delay) Delay JavaScript execution by delay milliseconds.
This method is supported since Zabbix 5.0.20.

Example (delay execution by 15 seconds):

Zabbix.sleep(15000)
CurlHttpRequest

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

The initialization of multiple CurlHttpRequest objects is limited to 10 per script execution.

Method Description
AddHeader(value) Adds HTTP header field. This field is used for all following requests until cleared with the ClearHeader() method.
The total length of header fields that can be added to a single CurlHttpRequest object is limited to 128 Kbytes (special characters and header names included).
ClearHeader() Clears HTTP header. If no header fields are set CurlHttpRequest will set Content-Type to application/json if the data being posted is json formatted and text/plain otherwise.
GetHeaders() Returns object of received HTTP header fields. This method is available since Zabbix 5.0.4.
Get(url, data) Sends HTTP GET 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.
Delete(url, data) Sends HTTP DELETE request to the URL with optional data payload and returns the response.
Status() 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)
This method is supported since Zabbix 5.0.9.

Example:

try {
           Zabbix.Log(4, 'jira webhook script value='+value);
         
           var result = {
               'tags': {
                   'endpoint': 'jira'
               }
           },
           params = JSON.parse(value),
           req = new CurlHttpRequest(),
           fields = {},
           resp;
         
           req.AddHeader('Content-Type: application/json');
           req.AddHeader('Authorization: Basic '+params.authentication);
         
           fields.summary = params.summary;
           fields.description = params.description;
           fields.project = {"key": params.project_key};
           fields.issuetype = {"id": params.issue_id};
           resp = req.Post('https://jira.example.com/rest/api/2/issue/',
               JSON.stringify({"fields": fields})
           );
         
           if (req.Status() != 201) {
               throw 'Response code: '+req.Status();
           }
         
           resp = JSON.parse(resp);
           result.tags.issue_id = resp.id;
           result.tags.issue_key = resp.key;
       } catch (error) {
           Zabbix.Log(4, 'jira issue creation failed json : '+JSON.stringify({"fields": fields}));
           Zabbix.Log(4, 'jira issue creation failed : '+error);
         
           result = {};
       }
         
       return JSON.stringify(result);

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
try {
           b64 = btoa("utf8 string");
           utf8 = atob(b64);
       } 
       catch (error) {
           return {'error.name' : error.name, 'error.message' : error.message}
       }
  • md5(string) - calculates the MD5 hash of a string; this function is supported since Zabbix 5.0.9
  • sha256(string) - calculates the SHA256 hash of a string; this function is supported since Zabbix 5.0.9