var Alvao = { params: {}, setParams: function (params) { if (typeof params !== 'object') { return; } Alvao.params = params; if (typeof Alvao.params.url === 'string') { if (!Alvao.params.url.endsWith('/')) { Alvao.params.url += '/'; } Alvao.params.url += 'v1/tickets'; } }, setProxy: function (HTTPProxy) { Alvao.HTTPProxy = HTTPProxy; }, request: function (method, data) { ['url', 'user', 'password'].forEach(function (field) { if (typeof Alvao.params !== 'object' || typeof Alvao.params[field] === 'undefined' || Alvao.params[field] === '' ) { throw 'Required Alvao param is not set: "' + field + '".'; } }); var response, url = Alvao.params.url, request = new CurlHttpRequest(); request.AddHeader('Content-Type: application/json'); request.AddHeader('Authorization: Basic ' + btoa(Alvao.params.user + ':' + Alvao.params.password)); if (typeof Alvao.HTTPProxy !== 'undefined' && Alvao.HTTPProxy !== '') { request.setProxy(Alvao.HTTPProxy); } if (typeof data !== 'undefined') { data = JSON.stringify(data); } Zabbix.Log(4, '[ Alvao Webhook ] Sending request: ' + url + ((typeof data === 'string') ? ('\n' + data) : '')); switch (method) { case 'get': response = request.get(url, data); break; case 'post': response = request.Post(url, data); break; case 'put': response = request.put(url, data); break; default: throw 'Unsupported HTTP request method: ' + method; } Zabbix.Log(4, '[ Alvao Webhook ] Received response with status code ' + request.Status() + '\n' + response); if (response !== null) { try { response = JSON.parse(response); } catch (error) { Zabbix.Log(4, '[ Alvao Webhook ] Failed to parse response received from Alvao'); response = null; } } if (request.Status() < 200 || request.Status() >= 300) { var message = 'Request failed with status code ' + request.Status(); if (response !== null && typeof response.message !== 'undefined' && Object.keys(response.message).length > 0) { message += ' message: ' + response.message; } throw message + ' Check debug log for more information.'; } return response.id; } }; try { var params = JSON.parse(value), alvao = {}, data = {}, result = {tags: {}}, required_params = [ 'url', 'user', 'password', 'service_id', "requestor_id", "subject", "text" ], method = 'post'; Object.keys(params) .forEach(function (key) { if (key.startsWith('alvao_')) { alvao[key.substring(6)] = params[key]; } else if (required_params.indexOf(key) !== -1 && params[key] === '') { throw 'Parameter "' + key + '" can\'t be empty.'; } }); data.requester = { id: alvao.requestor_id}; data.serviceId = alvao.service_id; data.name = alvao.subject; data.descriptionHtml = alvao.text; Alvao.setParams(alvao); Alvao.setProxy(params.HTTPProxy); var response = Alvao.request(method, data); return JSON.stringify(result); } catch (error) { Zabbix.Log(3, '[ Alvao Webhook ] ERROR: ' + error); throw 'Sending failed: ' + error; }