On this page
webhook 脚本示例
概述
尽管 Zabbix 开箱即提供了大量可用的 webhook 集成,您仍可能希望自行创建 webhook。 本节提供自定义 webhook 脚本的示例(用于 Script 参数)。 有关其他 webhook 参数的说明,请参见 webhook 部分。
不要在预处理 JavaScript 中使用未声明的赋值。
请使用 var 声明局部变量。
恢复事件(无论是自动生成,还是在 手动关闭 后生成)都包含已解决的事件标签(包括从模板、主机和触发器继承的标签)。 webhook 脚本在告警创建后执行;因此,webhook 脚本返回的标签会在初始告警创建之后才应用,可能不会出现在触发该 webhook 的告警中。 如果某个集成要求 webhook 生成的标签必须出现在初始通知中,请从服务器获取事件标签(例如,使用 Event API),或者将 webhook 生成的标签存储在外部持久化存储中并在那里进行关联。
Jira webhook(自定义)

此脚本将创建一个 JIRA 问题,并返回一些有关已创建问题的信息。
try {
Zabbix.log(4, '[ Jira webhook ] Started with params: ' + value);
var result = {
'tags': {
'endpoint': 'jira'
}
},
params = JSON.parse(value),
req = new HttpRequest(),
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://jira.example.com/rest/api/2/issue/',
JSON.stringify({"fields": fields})
);
if (req.getStatus() != 201) {
throw 'Response code: ' + req.getStatus();
}
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(自定义)
此 webhook 会将通知从 Zabbix 转发到 Slack 频道。

try {
var params = JSON.parse(value),
req = new HttpRequest(),
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.getStatus() + '. Response: ' + response);
try {
response = JSON.parse(response);
}
catch (error) {
if (req.getStatus() < 200 || req.getStatus() >= 300) {
throw 'Request failed with status code ' + req.getStatus();
}
else {
throw 'Request success, but response parsing failed.';
}
}
if (req.getStatus() !== 200 || !response.ok || response.ok === 'false') {
throw response.error;
}
return 'OK';
}
catch (error) {
Zabbix.log(3, '[ Slack webhook ] Sending failed. Error: ' + error);
throw 'Failed with error: ' + error;
}