Additional JavaScript objects

Overview

This section describes Zabbix additions to the JavaScript language implemented with Duktape.

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

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