forked from spydersoft-consulting/MMM-PrometheusAlerts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
90 lines (80 loc) · 2.35 KB
/
node_helper.js
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
/* Magic Mirror
* Module: MMM-PrometheusAlerts
*
*/
const { formatDistanceToNow } = require("date-fns");
const NodeHelper = require("node_helper");
const fetch = require("node-fetch");
const Log = require("logger");
const { json } = require("express");
module.exports = NodeHelper.create({
start: function () {
Log.info("MMM-PrometheusAlerts - Starting node_helper for: " + this.name);
},
socketNotificationReceived: function (notification, payload) {
if (notification === "GET_PROMTHEUS_ALERTS" && payload.prometheusUrl !== null) {
Log.info(`[MMM-PrometheusAlerts] - Processing GET_PROMTHEUS_ALERTS notification for ${payload.prometheusUrl}`);
this.getPrometheusAlerts(payload.prometheusUrl);
}
},
inGroup: function (comp, group = null) {
return comp.group_id === group;
},
hasIncident: function (incidents, comp) {
return incidents.findIndex((inc) => inc.componentId === comp.id) !== -1;
},
ignoreComponent: function (componentsToIgnore, compId) {
return componentsToIgnore.findIndex((comp) => comp === compId) !== -1;
},
getPrometheusAlerts: function (prometheusUrl) {
let self = this;
if (self.pending) {
return;
}
self.pending = true;
var url = prometheusUrl + "/api/v1/alerts";
fetch(url, {
method: "get"
})
.then(self.checkFetchStatus)
.then((response) => response.json())
.then((responseData) => {
var alerts = [];
responseData.data.alerts.forEach((alert) => {
var activeAt = Date.parse(alert.activeAt);
alerts.push({
labels: alert.labels,
annotations: alert.annotations,
state: alert.state,
value: alert.value,
age: formatDistanceToNow(activeAt, {}),
activeAt: activeAt
});
});
var summaryData = {
title: "Alerts",
alerts: alerts
};
Log.info("[MMM-PrometheusAlerts] - Sending Summary Data");
Log.debug("[MMM-PrometheusAlerts] - " + JSON.stringify(summaryData));
self.sendSocketNotification("PROMTHEUSALERT_RESULT", summaryData);
})
.catch((error) => {
self.logError(error);
self.sendSocketNotification("PROMTHEUSALERT_RETRIEVE_ERROR");
})
.finally(() => {
self.pending = false;
});
},
checkFetchStatus: function (response) {
if (response.ok) {
return response;
} else {
throw Error(response.statusText);
}
},
logError: function (error) {
Log.error(`[MMM-PrometheusAlerts]: ${error}`);
}
});