Webhook脚本范例

概述

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

Jira webhook (自定义)

Webhook脚本范例 - 图1

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

  1. try {
  2. Zabbix.Log(127, '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. proxy = params.HTTPProxy;
  11. req.SetProxy(proxy);
  12. fields = {},
  13. resp;
  14. req.AddHeader('Content-Type: application/json');
  15. req.AddHeader('Authorization: Basic '+params.authentication);
  16. fields.summary = params.summary;
  17. fields.description = params.description;
  18. fields.project = {"key": params.project_key};
  19. fields.issuetype = {"id": params.issue_id};
  20. resp = req.Post('https://tsupport.zabbix.lan/rest/api/2/issue/',
  21. JSON.stringify({"fields": fields})
  22. );
  23. if (req.Status() != 201) {
  24. throw 'Response code: '+req.Status();
  25. }
  26. resp = JSON.parse(resp);
  27. result.tags.issue_id = resp.id;
  28. result.tags.issue_key = resp.key;
  29. } catch (error) {
  30. Zabbix.Log(127, 'jira issue creation failed json : '+JSON.stringify({"fields": fields}));
  31. Zabbix.Log(127, 'jira issue creation failed : '+error);
  32. result = {};
  33. }
  34. return JSON.stringify(result);

Slack webhook (自定义)

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

Webhook脚本范例 - 图2

  1. var req = new CurlHttpRequest();
  2. params = JSON.parse(value);
  3. proxy = params.HTTPProxy;
  4. req.SetProxy(proxy);
  5. req.AddHeader('Content-Type: application/x-www-form-urlencoded');
  6. Zabbix.Log(127, 'webhook request value='+value);
  7. req.Post('https://hooks.slack.com/services/KLFDEI9KNL/ZNA76HGCF/h9MLKJMWoUcEAz8n',
  8. 'payload='+value
  9. );
  10. Zabbix.Log(127, 'response code: '+req.Status());
  11. return JSON.stringify({
  12. 'tags': {
  13. 'delivered': 'slack'
  14. }
  15. });