|
| 1 | +zabbix_export: |
| 2 | + version: '6.0' |
| 3 | + media_types: |
| 4 | + - |
| 5 | + name: StackStorm |
| 6 | + type: WEBHOOK |
| 7 | + description: StackStorm |
| 8 | + parameters: |
| 9 | + - |
| 10 | + name: alert_message |
| 11 | + value: '{ALERT.MESSAGE}' |
| 12 | + - |
| 13 | + name: alert_subject |
| 14 | + value: '{ALERT.SUBJECT}' |
| 15 | + - |
| 16 | + name: alert_sendto |
| 17 | + value: '{ALERT.SENDTO}' |
| 18 | + - |
| 19 | + name: event_source |
| 20 | + value: '{EVENT.SOURCE}' |
| 21 | + - |
| 22 | + name: st2_api_url |
| 23 | + value: '<PLACE ST2 API URL>' |
| 24 | + - |
| 25 | + name: st2_api_key |
| 26 | + value: '<PLACE ST2 API Key>' |
| 27 | + - |
| 28 | + name: st2_trigger |
| 29 | + value: zabbix.event_handler |
| 30 | + script: | |
| 31 | + var St2 = { |
| 32 | + params: {}, |
| 33 | +
|
| 34 | + setParams: function (params) { |
| 35 | + if (typeof params !== 'object') { |
| 36 | + return; |
| 37 | + } |
| 38 | + St2.params = params; |
| 39 | + }, |
| 40 | +
|
| 41 | + setProxy: function (HTTPProxy) { |
| 42 | + St2.HTTPProxy = HTTPProxy; |
| 43 | + }, |
| 44 | +
|
| 45 | + urlCheckFormat: function (api_url) { |
| 46 | + if (typeof api_url === 'string' && !api_url.endsWith('/')) { |
| 47 | + api_url += '/'; |
| 48 | + } |
| 49 | +
|
| 50 | + if (api_url.indexOf('http://') === -1 && api_url.indexOf('https://') === -1) { |
| 51 | + api_url = 'https://' + api_url; |
| 52 | + } |
| 53 | +
|
| 54 | + return api_url; |
| 55 | + }, |
| 56 | +
|
| 57 | + request: function (api_url, data) { |
| 58 | + if (typeof St2.params !== 'object' || typeof St2.params['api_key'] === 'undefined' || St2.params['api_key'] === '') { |
| 59 | + throw 'Required St2 param is not set: "api_key".'; |
| 60 | + } |
| 61 | +
|
| 62 | + var response, |
| 63 | + request = new HttpRequest(); |
| 64 | +
|
| 65 | + request.addHeader('Content-Type: application/json'); |
| 66 | + request.addHeader('St2-Api-Key: ' + St2.params.api_key); |
| 67 | +
|
| 68 | + const webhook_url = api_url + 'webhooks/st2'; |
| 69 | +
|
| 70 | + if (typeof St2.HTTPProxy !== 'undefined' && St2.HTTPProxy !== '') { |
| 71 | + request.setProxy(St2.HTTPProxy); |
| 72 | + } |
| 73 | +
|
| 74 | + if (typeof data !== 'undefined') { |
| 75 | + data = JSON.stringify(data); |
| 76 | + } |
| 77 | +
|
| 78 | + Zabbix.log(4, '[ StackStorm Webhook ] Sending request: ' + webhook_url + ((typeof data === 'string') |
| 79 | + ? ('\n' + data) |
| 80 | + : '')); |
| 81 | +
|
| 82 | + response = request.post(webhook_url, data); |
| 83 | +
|
| 84 | + Zabbix.log(4, '[ StackStorm Webhook ] Received response with status code ' + |
| 85 | + request.getStatus() + '\n' + response); |
| 86 | +
|
| 87 | + if (response !== null) { |
| 88 | + try { |
| 89 | + response = JSON.parse(response); |
| 90 | + } |
| 91 | + catch (error) { |
| 92 | + Zabbix.log(4, '[ StackStorm Webhook ] Failed to parse response received from StackStorm'); |
| 93 | + response = null; |
| 94 | + } |
| 95 | + } |
| 96 | +
|
| 97 | + if (typeof response !== 'object') { |
| 98 | + throw 'Failed to process response received from StackStorm. Check debug log for more information.'; |
| 99 | + } |
| 100 | +
|
| 101 | + if (request.getStatus() < 200 || request.getStatus() >= 300) { |
| 102 | + var message = 'Request failed with status code ' + request.getStatus(); |
| 103 | +
|
| 104 | + if (response.message) { |
| 105 | + message += ': ' + response.message; |
| 106 | + } |
| 107 | +
|
| 108 | + throw message + ' Check debug log for more information.'; |
| 109 | + } |
| 110 | +
|
| 111 | + return response; |
| 112 | + } |
| 113 | + }; |
| 114 | +
|
| 115 | + try { |
| 116 | + var params = JSON.parse(value), |
| 117 | + st2 = {}, |
| 118 | + data = {}, |
| 119 | + result |
| 120 | + required_params = [ |
| 121 | + 'alert_subject', 'alert_message', |
| 122 | + 'st2_api_url', 'st2_api_key', 'st2_trigger' |
| 123 | + ]; |
| 124 | +
|
| 125 | + Object.keys(params) |
| 126 | + .forEach(function (key) { |
| 127 | + if (key.startsWith('st2_')) { |
| 128 | + st2[key.substring(4)] = params[key]; |
| 129 | + } |
| 130 | + else if (required_params.indexOf(key) !== -1 && params[key] === '') { |
| 131 | + throw 'Parameter "' + key + '" can\'t be empty.'; |
| 132 | + } |
| 133 | + }); |
| 134 | +
|
| 135 | + // Check type of event. Possible values: 0 - Trigger |
| 136 | + if (params.event_source != 0) { |
| 137 | + throw ('Incorrect "event_source" parameter given: ' + params.event_source |
| 138 | + + '\nOnly trigger-based events are supported'); |
| 139 | + } |
| 140 | +
|
| 141 | + // Check for backslash in the end of url and schema. |
| 142 | + st2.api_url = St2.urlCheckFormat(st2.api_url); |
| 143 | +
|
| 144 | +
|
| 145 | + data.trigger = st2.trigger; |
| 146 | + data.payload = { |
| 147 | + alert_sendto: params.alert_sendto, |
| 148 | + alert_subject: params.alert_subject, |
| 149 | + alert_message: params.alert_message |
| 150 | + } |
| 151 | +
|
| 152 | + St2.setParams(st2); |
| 153 | + St2.setProxy(params.HTTPProxy); |
| 154 | +
|
| 155 | + var response = St2.request(st2.api_url, data); |
| 156 | +
|
| 157 | + Zabbix.log(4, '[ StackStorm Webhook ] Response: ' + JSON.stringify(response)); |
| 158 | + return JSON.stringify(response); |
| 159 | + } |
| 160 | + catch (error) { |
| 161 | + Zabbix.log(4, '[ StackStorm Webhook ] ERROR: ' + error); |
| 162 | + throw 'Sending failed: ' + error; |
| 163 | + } |
| 164 | + message_templates: |
| 165 | + - |
| 166 | + event_source: TRIGGERS |
| 167 | + operation_mode: PROBLEM |
| 168 | + subject: '[{TRIGGER.STATUS}] {TRIGGER.NAME}' |
| 169 | + message: | |
| 170 | + { |
| 171 | + "event": { |
| 172 | + "id": "{EVENT.ID}", |
| 173 | + "time": "{EVENT.TIME}" |
| 174 | + }, |
| 175 | + "trigger": { |
| 176 | + "id": "{TRIGGER.ID}", |
| 177 | + "name": "{TRIGGER.NAME}", |
| 178 | + "status": "{TRIGGER.STATUS}" |
| 179 | + }, |
| 180 | + "items": [ |
| 181 | + { |
| 182 | + "name": "{ITEM.NAME1}", |
| 183 | + "host": "{HOST.NAME1}", |
| 184 | + "key": "{ITEM.KEY1}", |
| 185 | + "value": "{ITEM.VALUE1}" |
| 186 | + }, |
| 187 | + { |
| 188 | + "name": "{ITEM.NAME2}", |
| 189 | + "host": "{HOST.NAME2}", |
| 190 | + "key": "{ITEM.KEY2}", |
| 191 | + "value": "{ITEM.VALUE2}" |
| 192 | + }, |
| 193 | + { |
| 194 | + "name": "{ITEM.NAME3}", |
| 195 | + "host": "{HOST.NAME3}", |
| 196 | + "key": "{ITEM.KEY3}", |
| 197 | + "value": "{ITEM.VALUE3}" |
| 198 | + }, |
| 199 | + { |
| 200 | + "name": "{ITEM.NAME4}", |
| 201 | + "host": "{HOST.NAME4}", |
| 202 | + "key": "{ITEM.KEY4}", |
| 203 | + "value": "{ITEM.VALUE4}" |
| 204 | + }, |
| 205 | + { |
| 206 | + "name": "{ITEM.NAME5}", |
| 207 | + "host": "{HOST.NAME5}", |
| 208 | + "key": "{ITEM.KEY5}", |
| 209 | + "value": "{ITEM.VALUE5}" |
| 210 | + }, |
| 211 | + { |
| 212 | + "name": "{ITEM.NAME6}", |
| 213 | + "host": "{HOST.NAME6}", |
| 214 | + "key": "{ITEM.KEY6}", |
| 215 | + "value": "{ITEM.VALUE6}" |
| 216 | + }, |
| 217 | + { |
| 218 | + "name": "{ITEM.NAME7}", |
| 219 | + "host": "{HOST.NAME7}", |
| 220 | + "key": "{ITEM.KEY7}", |
| 221 | + "value": "{ITEM.VALUE7}" |
| 222 | + }, |
| 223 | + { |
| 224 | + "name": "{ITEM.NAME8}", |
| 225 | + "host": "{HOST.NAME8}", |
| 226 | + "key": "{ITEM.KEY8}", |
| 227 | + "value": "{ITEM.VALUE8}" |
| 228 | + }, |
| 229 | + { |
| 230 | + "name": "{ITEM.NAME9}", |
| 231 | + "host": "{HOST.NAME9}", |
| 232 | + "key": "{ITEM.KEY9}", |
| 233 | + "value": "{ITEM.VALUE9}" |
| 234 | + } |
| 235 | + ] |
| 236 | + } |
0 commit comments