这是原厂英文文档的翻译页面. 欢迎帮助我们 完善文档.

Webhook脚本范例

概述

尽管Zabbix提供了大量现成的webhook集成,但您可能想要创建自己的webhook。本节提供了自定义webhook脚本的示例 (在 脚本 参数中使用)。有关其他webhook参数的说明,请参见 webhook 章节。

Jira webhook (自定义)

此脚本将创建一个JIRA问题,并返回关于所创建问题的一些信息。

try {
           Zabbix.Log(127, 'jira webhook script value='+value);
       
           var result = {
               'tags': {
                   'endpoint': 'jira'
               }
           },
           params = JSON.parse(value),
           req = new CurlHttpRequest(),
               proxy = params.HTTPProxy;
               req.SetProxy(proxy);
           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(127, 'jira issue creation failed json : '+JSON.stringify({"fields": fields}));
           Zabbix.Log(127, 'jira issue creation failed : '+error);
       
           result = {};
       }
       
       return JSON.stringify(result);

Slack webhook (自定义)

此webhook将把Zabbix的通知转发到Slack频道中。

var req = new CurlHttpRequest();
       params = JSON.parse(value);
       proxy = params.HTTPProxy;
       req.SetProxy(proxy);
       req.AddHeader('Content-Type: application/x-www-form-urlencoded');
       
       Zabbix.Log(127, 'webhook request value='+value);
       
       req.Post('https://hooks.slack.com/services/KLFDEI9KNL/ZNA76HGCF/h9MLKJMWoUcEAz8n',
         'payload='+value
       );
       
       Zabbix.Log(127, 'response code: '+req.Status());
       
       return JSON.stringify({
         'tags': {
           'delivered': 'slack'
         }
       });