This repository has been archived by the owner on Mar 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
node_helper.js
58 lines (48 loc) · 1.62 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
var NodeHelper = require("node_helper");
var StatusFetcher = require("./statusfetcher.js");
module.exports = NodeHelper.create({
// Override start method.
start: function() {
this.fetchers = [];
console.log("Starting node helper for: " + this.name);
},
// Override socketNotificationReceived method.
socketNotificationReceived: function(notification, payload) {
if (notification === "ADD_STATUS_URL") {
this.createFetcher(payload.apiUrl, payload.username, payload.password, payload.reloadInterval);
}
},
/* createFetcher(station)
* Creates a fetcher for a url if it doesn't exist yet.
* Otherwise it reuses the existing one.
*
* attribute url string - The URL to the status page.
* attribute reloadInterval number - Reload interval in milliseconds.
*/
createFetcher: function(apiUrl, username, password, reloadInterval) {
var self = this;
var fetcher;
if (typeof self.fetchers[apiUrl] === "undefined") {
console.log("Create new status fetcher for URL: " + apiUrl + " - Interval: " + reloadInterval);
fetcher = new StatusFetcher(apiUrl, username, password, reloadInterval);
fetcher.onReceive(function(fetcher) {
self.sendSocketNotification("STATUS_EVENT", {
url: fetcher.url(),
status: fetcher.status()
});
});
fetcher.onError(function(fetcher, error) {
self.sendSocketNotification("FETCH_ERROR", {
url: fetcher.url(),
error: error
});
});
self.fetchers[apiUrl] = fetcher;
} else {
console.log("Use existing nagios fetcher for url: " + apiUrl);
fetcher = self.fetchers[apiUrl];
fetcher.broadcastStatus();
}
fetcher.startFetch();
}
});