-
Notifications
You must be signed in to change notification settings - Fork 46
/
app.js
80 lines (60 loc) · 1.92 KB
/
app.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
"use strict";
const Ping = require('ping-monitor');
const http = require('http');
const websites = require('./websites');
const mailer = require('./mailer');
const router = require('./router');
const monitors = [];
const port = process.env.PORT || 3008;
const urls = [];
function pingServers() {
// Iterate through websites and create a ping monitor for each website
websites.forEach(function (website) {
let monitor = new Ping ({
website: website.url,
interval: website.interval
});
monitor.on('up', function (res) {
console.log('Yay!! ' + res.website + ' is up.');
});
monitor.on('down', function (res) {
mailer.sendEmail({
subject: res.website + ' is down',
body: '<p>Time: ' + monitor.getFormatedDate(res.time) + '</p><p>Website: ' + res.website + ' </p><p>Message: ' + res.statusMessage + ' </p>'
},
function (err, message) {
if (err) {
console.error(err.message);
}
else {
console.log(res.website + ' is down. Email sent!');
}
});
});
monitor.on('error', function (res) {
mailer.sendEmail({
subject: res.website + ' is down',
body: '<p>Time: ' + monitor.getFormatedDate(res.time) + '</p><p>Website: ' + res.website + ' </p><p>Message: ' + res.statusMessage + ' </p>'
},
function (err, message) {
if (err) {
console.error(err.message);
}
else {
console.log(res.website + ' is down. Email sent!');
}
});
});
monitor.on('stop', function (website) {
console.log(website + ' monitor has stopped.');
});
urls.push(website.url);
monitors.push(monitor);
});
}
// start monitoring servers
pingServers();
// create web server
const server = http.createServer(router(urls));
server.listen(port);
console.log('Listening to port %s', port);