This is a translation of the original English documentation page. Help us make it better.

Webhook script examples

Overview

Though Zabbix offers a large number of webhook integrations available out-of-the-box, you may want to create your own webhooks instead. This section provides examples of custom webhook scripts (used in the Script parameter). See webhook section for description of other webhook parameters.

Jira webhook (custom)

This script will create a JIRA issue and return some info on the created issue.

try {
           Zabbix.Log(4, '[ Jira webhook ] Started with params: ' + value);
       
           var result = {
                   'tags': {
                       'endpoint': 'jira'
                   }
               },
               params = JSON.parse(value),
               req = new CurlHttpRequest(),
               fields = {},
               resp;
       
           if (params.HTTPProxy) {
               req.SetProxy(params.HTTPProxy);
           }
       
           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;
       
           return JSON.stringify(result);
       }
       catch (error) {
           Zabbix.Log(4, '[ Jira webhook ] Issue creation failed json : ' + JSON.stringify({"fields": fields}));
           Zabbix.Log(3, '[ Jira webhook ] issue creation failed : ' + error);
       
           throw 'Failed with error: ' + error;
       }

Slack webhook (custom)

This webhook will forward notifications from Zabbix to a Slack channel.

try {
           var params = JSON.parse(value),
               req = new CurlHttpRequest(),
               response;
       
           if (params.HTTPProxy) {
               req.SetProxy(params.HTTPProxy);
           }
       
           req.AddHeader('Content-Type: application/x-www-form-urlencoded');
       
           Zabbix.Log(4, '[ Slack webhook ] Webhook request with value=' + value);
       
           response = req.Post(params.hook_url, 'payload=' + encodeURIComponent(value));
           Zabbix.Log(4, '[ Slack webhook ] Responded with code: ' + req.Status() + '. Response: ' + response);
       
           try {
               response = JSON.parse(response);
           }
           catch (error) {
               if (req.getStatus() < 200 || req.getStatus() >= 300) {
                   throw 'Request failed with status code ' + req.Status();
               }
               else {
                   throw 'Request success, but response parsing failed.';
               }
           }
       
           if (req.Status() !== 200 || !response.ok || response.ok === 'false') {
               throw response.error;
           }
       
           return 'OK';
       }
       catch (error) {
           Zabbix.Log(3, '[ Jira webhook ] Sending failed. Error: ' + error);
       
           throw 'Failed with error: ' + error;
       }