-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmm-zabbix-alerts.js
156 lines (133 loc) · 5.75 KB
/
mmm-zabbix-alerts.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
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
/* MMM2 Module */
/* spectroman's
* Module: Zabbix Alerts
*
* By Spectroman https://juliochegedus.com
* MIT Licensed.
*/
Module.register("mmm-zabbix-alerts",{
// Default module config.
defaults: {
zbx_user: "GETYOUROWNUSER", // zabbix auth user
abx_pass: "GETYOUROWNPASS", // zabbix auth pass
updateInterval: 60 * 1000, // every 1 min
animationSpeed: 1000,
listSize: 4, // amount of items to check
maxTitleSize: 40, // title size
fade: false, // fade on rendering
fadePoint: 0.25, // Start on 1/4th of the list.
initialLoadDelay: 2500, // 2.5 seconds delay.
retryDelay: 120000, // wait for retry on fail
apiSearch: "http://monitor.science.net/api_jsonrpc.php",
triggerIds: [ "1234", "1234", "1234", "1234" ],
},
// Define required scripts.
getScripts: function() {
return ["moment.js"];
},
// Define required scripts.
getStyles: function() {
return ["zabbix-alerts.css"];
},
// Define start sequence.
start: function() {
Log.info("Starting module: " + this.name);
this.loaded = false;
this.scheduleUpdate(this.config.initialLoadDelay);
this.updateTimer = null;
},
makeLogin: function() {
var self = this;
self.sendSocketNotification("LOGIN",{config: this.config});
},
socketNotificationReceived: function(notification, payload) {
var self = this;
if(notification === "GOT_TOKEN"){
// this.fetchAlerts(payload.result);
this.localToken = payload.result
self.sendSocketNotification("TRIGGERS",{config: this.config, token: this.localToken});
}
if(notification === "GOT_TRIGGERS"){
this.triggers = payload.result;
self.sendSocketNotification("ALERTS",{config: this.config, token: this.localToken});
}
if(notification === "GOT_ALERTS"){
this.problems = payload.result;
this.buildResponse(this.problems,this.triggers);
this.scheduleUpdate();
}
if(notification === "FAIL_TOKEN" || notification === "FAIL_TRIGGERS" || notification === "FAIL_ALERTS"){
this.scheduleUpdate(this.config.retryDelay);
}
},
buildResponse: function(problems,triggers) {
var self = this;
this.loaded = true;
self.triggList = [];
if (triggers.length > 0) {
triggers.forEach(function(item) {
var status='OK';
if (problems.length > 0) {
problems.forEach(function(itprob) {
if (item.triggerid == itprob.objectid) {
status = "Problemo"
}
});
}
// canont explain why it only works like this, which is horrible!!!!!!!!!!
if (self.triggList) {
self.triggList.push({ name: item.description, id: item.triggerid, status: status });
} else {
self.triggList = [{ name: item.description, id: item.triggerid, status: status }]
}
});
}
this.updateDom(this.config.animationSpeed);
},
scheduleUpdate: function(delay) {
var nextLoad = this.config.updateInterval;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
var self = this;
clearTimeout(this.updateTimer);
this.updateTimer = setTimeout(function() {
self.makeLogin();
}, nextLoad);
},
// Override dom generator.
getDom: function() {
var table = document.createElement("table");
var l = 0;
while ( l < this.config.listSize ) {
var trgr = this.triggList[l];
var row = document.createElement("tr");
table.appendChild(row);
var triggerA = document.createElement("td");
triggerA.className = trgr.status === "OK" ? "oktrigger" : "problemtrigger";
var triggerAValue = trgr.id+": "+trgr.name;
triggerA.innerHTML = triggerAValue;
row.appendChild(triggerA);
l++;
var trgr = this.triggList[l];
var triggerB = document.createElement("td");
triggerB.className = trgr.status === "OK" ? "oktrigger" : "problemtrigger";
var triggerAValue = trgr.id+": "+trgr.name;
triggerB.innerHTML = triggerAValue;
row.appendChild(triggerB);
l++;
if (this.config.fade && this.config.fadePoint < 1) {
if (this.config.fadePoint < 0) {
this.config.fadePoint = 0;
}
var startingPoint = (this.triggList.length + 1) * this.config.fadePoint;
var steps = this.triggList.length - startingPoint;
if (l >= startingPoint) {
var currentStep = l - startingPoint;
row.style.opacity = 1 - (1 / steps * currentStep);
}
}
}
return table
}
});