-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmailGraph.xml
executable file
·215 lines (194 loc) · 8.43 KB
/
mailGraph.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
<version>5.4</version>
<date>2023-08-16T20:38:38Z</date>
<media_types>
<media_type>
<name>MailGraph</name>
<type>WEBHOOK</type>
<parameters>
<parameter>
<name>baseURL</name>
<value>https://myzabbix.com/zabbix/</value>
</parameter>
<parameter>
<name>duration</name>
<value>{EVENT.DURATION}</value>
</parameter>
<parameter>
<name>eventId</name>
<value>{EVENT.ID}</value>
</parameter>
<parameter>
<name>graphHeight</name>
<value>120</value>
</parameter>
<parameter>
<name>graphWidth</name>
<value>300</value>
</parameter>
<parameter>
<name>HTTPProxy</name>
<value/>
</parameter>
<parameter>
<name>infoTest</name>
<value>Test</value>
</parameter>
<parameter>
<name>periods</name>
<value>10m,4h,1d,7d</value>
</parameter>
<parameter>
<name>periods_headers</name>
<value>Last 10 minutes,Last 4 hours,Last day,Last 7 days</value>
</parameter>
<parameter>
<name>recipient</name>
<value>{ALERT.SENDTO}</value>
</parameter>
<parameter>
<name>showLegend</name>
<value>0</value>
</parameter>
<parameter>
<name>subject</name>
<value>{{ HOST_NAME|raw }}: ({{ EVENT_SEVERITY }}) {{ EVENT_NAME|raw }}</value>
</parameter>
<parameter>
<name>URL</name>
<value>https://myzabbix.com/mailGraph.php</value>
</parameter>
</parameters>
<script>// mailGraph v2.20
// Function to test string
function isJSON(str) {
try {
JSON.stringify(JSON.parse(str));
return true;
} catch (e) {
return false;
}
}
try {
// Pickup parameters
params = JSON.parse(value),
req = new HttpRequest(),
fields = {},
resp = '',
result = { tags: {} };
// Set HTTP proxy if required
if (typeof params.HTTPProxy === 'string' && params.HTTPProxy.trim() !== '') {
req.setProxy(params.HTTPProxy);
fields.HTTPProxy = params.HTTPProxy;
}
// Declare output type
req.addHeader('Content-Type: application/json');
// Pick up fields relevant for mailGraph API level call while parsing/casting fields that should be integer
fields.itemId = params.itemId * 1;
fields.eventId = params.eventId * 1;
fields.recipient = params.recipient;
fields.baseURL = params.baseURL;
fields.duration = params.duration * 1;
if (fields.recipient.charAt(0) == '{') {
throw '[MailGraph Webhook] Please define recipient for the test message!';
}
// Optional fields
if (typeof params.graphWidth === 'string') { fields.graphWidth = params.graphWidth; }
if (typeof params.graphHeight === 'string') { fields.graphHeight = params.graphHeight; }
if (typeof params.subject === 'string') { fields.subject = params.subject; }
if (typeof params.showLegend === 'string') { fields.showLegend = params.showLegend; }
if (typeof params.periods === 'string') { fields.periods = params.periods; }
if (typeof params.periods_headers === 'string') { fields.periods_headers = params.periods_headers; }
if (typeof params.debug === 'string') { fields.debug = params.debug; }
// Add generic fields
Object.keys(params).forEach(function(key) {
if (key.substring(0, 4) == 'info') {
fields[key] = params[key];
}
});
// Post information to the processing script
Zabbix.Log(4, '[MailGraph Webhook] Sending request: ' + params.URL + '?' + JSON.stringify(fields));
var resp = req.post(params.URL,JSON.stringify(fields));
Zabbix.Log(4, '[Mailgraph Webhook] Received response:' + resp);
// The response can be
// - did not receive status 200 as result (contains HTTP server response)
// - null (no response received at all)
// - empty string (likely no e-mail sent due to recipient issue)
// - not json (debugging message for troubleshooting or configuration hints)
// - json (contains the mail message ID sent)
if (req.getStatus() != 200) {
throw '[MailGraph Webhook] Processing of mailGraph.php failed: ' + resp;
}
if (resp==null) {
throw '[MailGraph Webhook] No response received from mailGraph.php? This should not occur (check URL and your webserver!)';
}
if (resp=='') {
throw '[MailGraph Webhook] No data received from mailGraph - please check recipient address or mailGraph log and retry.';
}
// Check if JSON was returned
if (!isJSON(resp)) {
throw '[MailGraph Webhook] An error has occurred during processing: ' + resp;
}
// We expect the message id back from the processing script response in JSON format
msg = JSON.parse(resp);
result.tags.__message_id = msg.messageId;
Zabbix.Log(4, '[MailGraph Webhook] Message sent with identification "' + msg.messageId + '"');
// Pass the result back to Zabbix
return JSON.stringify(result);
}
catch (error)
{
// In case something else went wrong in the processing, pass the error back to Zabbix
Zabbix.Log(127, 'MailGraph notification failed: '+error);
throw 'MailGraph notification failed : '+error;
}</script>
<process_tags>YES</process_tags>
<description>The "URL" must point to the location of the processing script. If a proxy is required, define "HTTPProxy" for the proxy address.
Customization:
- "graphWidth" and "graphWidth" can be defined for the image size
- "showLegend" can be defined to show or hide the legend of the graph
- "subject" can be defined for a customized subject for the mail message
- "periods" and "periods_headers" can be defined for displaying multiple periods of the same graph, or
- "period" and "period_header" can be defined to display a single graph
The html.template and plain.template files can be adjusted (TWIG format).
More details are available at https://github.com/moudsen/mailGraph</description>
<message_templates>
<message_template>
<event_source>TRIGGERS</event_source>
<operation_mode>PROBLEM</operation_mode>
<subject>Problem: {EVENT.NAME}</subject>
<message>Problem started at {EVENT.TIME} on {EVENT.DATE}
Problem name: {EVENT.NAME}
Host: {HOST.NAME}
Severity: {EVENT.SEVERITY}
Operational data: {EVENT.OPDATA}
Original problem ID: {EVENT.ID}
Trigger ID (/url): {TRIGGER.ID} {TRIGGER.URL}</message>
</message_template>
<message_template>
<event_source>TRIGGERS</event_source>
<operation_mode>RECOVERY</operation_mode>
<subject>Resolved in {EVENT.DURATION}: {EVENT.RECOVERY.NAME}</subject>
<message>Problem has been resolved at {EVENT.RECOVERY.TIME} on {EVENT.RECOVERY.DATE}
Problem name: {EVENT.RECOVERY.NAME}
Problem duration: {EVENT.DURATION}
Host: {HOST.NAME}
Severity: {EVENT.SEVERITY}
Original problem ID: {EVENT.ID}
Trigger ID: {TRIGGER.ID} {TRIGGER.URL}</message>
</message_template>
<message_template>
<event_source>TRIGGERS</event_source>
<operation_mode>UPDATE</operation_mode>
<subject>Updated problem in {EVENT.AGE}: {EVENT.NAME}</subject>
<message>{USER.FULLNAME} {EVENT.UPDATE.ACTION} problem at {EVENT.UPDATE.DATE} {EVENT.UPDATE.TIME}.
{EVENT.UPDATE.MESSAGE}
Current problem status is {EVENT.STATUS}, age is {EVENT.AGE}, acknowledged: {EVENT.ACK.STATUS}.
Event ID: {EVENT.ID}
Trigger ID: {TRIGGER.ID}</message>
</message_template>
</message_templates>
</media_type>
</media_types>
</zabbix_export>