Sidebar

manual:appendix:preprocessing:javascript:javascript_objects

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:

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

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://tsupport.zabbix.lan/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);