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")

CurlHttpRequest

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

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 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:

  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 CurlHttpRequest(),
  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.Status() != 201) {
  22. throw 'Response code: '+req.Status();
  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);

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. 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