-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgaiatracker.js
40 lines (31 loc) · 1.19 KB
/
gaiatracker.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
const schedule = require('node-schedule');
const WebHooks = require('node-webhooks')
const ping = require('ping');
var fs = require('fs');
// read file with hosts to monitor
var hosts = fs.readFileSync('hosts.txt').toString().split("\n");
// read json file that stores webhook URLs
var webHooks = new WebHooks({
db: './webHooksDB.json',
})
// for tracking changes, we have to store the last ping in a hash
var status = {};
// scheduled for every minute
var schedulePing = schedule.scheduleJob('1 * * * * *', function() {
// looking for all hosts
hosts.forEach(function(host){
ping.sys.probe(host, function(isAlive){ // pinging and setting up the message
var msg = isAlive ? 'host ' + host + ' is alive' : 'host ' + host + ' is dead';
// changed or not?
if (isAlive != status[host] && typeof status[host] !== "undefined"){
// if changed and not clean sheet after init, then push to the webhook
console.log("status changed " + msg);
webHooks.trigger('MBD', {text: msg})
}else{
console.log("status UNchanged " + msg);
}
// change status to current one
status[host] = isAlive;
});
});
})