mirror of
https://github.com/moudsen/mailGraph
synced 2025-10-26 07:07:39 +01:00
v2.11 - Improved error handling and ability to pick random problem via Zabbix for testing
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
// mailGraph v2.11
|
||||
try {
|
||||
// Pickup parameters
|
||||
params = JSON.parse(value),
|
||||
@@ -16,11 +17,23 @@ try {
|
||||
req.addHeader('Content-Type: application/json');
|
||||
|
||||
// Must have fields
|
||||
fields.itemId = params.itemId;
|
||||
fields.eventId = params.eventId;
|
||||
fields.itemId = params.itemId * 1;
|
||||
fields.eventId = params.eventId * 1;
|
||||
fields.recipient = params.recipient;
|
||||
fields.baseURL = params.baseURL;
|
||||
fields.duration = params.duration;
|
||||
fields.duration = params.duration * 1;
|
||||
|
||||
if (isNaN(fields.eventId)) {
|
||||
throw '[MailGraph Webhook] Invalid event ID? Integer required (use actual event ID from Zabbix!)';
|
||||
}
|
||||
|
||||
if (isNaN(fields.duration)) {
|
||||
throw '[MailGraph Webhook] Invalid duration? Integer required (set to zero if unknown)!';
|
||||
}
|
||||
|
||||
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; }
|
||||
@@ -41,14 +54,26 @@ try {
|
||||
// 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] Receiving response:' + resp);
|
||||
Zabbix.Log(4, '[Mailgraph Webhook] Received response:' + resp);
|
||||
|
||||
// If there was an error, report it
|
||||
// If blank the email address is likely incorrect
|
||||
if (resp==null) {
|
||||
throw '[MailGraph Webhook] No data received from mailGraph! Likely the email processing failed? Check mailGraph logging';
|
||||
}
|
||||
|
||||
// If there was an error, report it and stop
|
||||
if (req.getStatus() != 200) { throw JSON.parse(resp).errors[0]; }
|
||||
|
||||
// If no datas returned, report it and stop
|
||||
if (resp.charAt(0) == "!") {
|
||||
throw '[MailGraph Webhook] No data received from mailGraph! Likely the event ID provided does not exist? Check mailGraph logging';
|
||||
}
|
||||
|
||||
// We expect the message id back from the processing script
|
||||
resp = JSON.parse(resp);
|
||||
result.tags.__message_id = resp.messageId;
|
||||
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);
|
||||
@@ -56,6 +81,6 @@ try {
|
||||
catch (error)
|
||||
{
|
||||
// In case something went wrong in the processing, pass the error back to Zabbix
|
||||
Zabbix.Log(127, 'MailGraph notification failed : '+error);
|
||||
Zabbix.Log(127, 'MailGraph notification failed: '+error);
|
||||
throw 'MailGraph notification failed : '+error;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user