-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
122 lines (110 loc) · 3.57 KB
/
index.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
"use strict";
const express = require('express');
const bodyParser = require('body-parser');
const assert = require('assert');
const ping = require('ping');
const async = require('async');
const request = require('request');
const utils = require('./utils');
const mailer = require("./mailer");
const PORT = process.env.PORT || 7000;
const CONFIG = utils.parseConfig();
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, x-auth-token");
next();
});
app.use(express.static('static'));
app.set('view engine', 'ejs');
function pingTest(next) {
function pingHost(host, callback) {
ping.promise.probe(host.host)
.then(function (res) {
host.pingResponse = res.alive;
callback();
});
}
async.each(CONFIG.data, pingHost, function (err) {
if (err) {
next(utils.makeJSON(500, "Internal Server Error"));
} else {
next(utils.makeJSON(200, "Success", null));
}
});
}
function statusTest(next) {
function statusHost(host, callback) {
request(host.heartbeat, function (err, resp, data) {
if(err) {
callback(err);
} else {
host.heartbeatStatus = resp.statusCode;
host.heartbeatData = data;
callback();
}
});
}
async.each(CONFIG.data, statusHost, function (err) {
if (err) {
next(utils.makeJSON(500, "Internal Server Error"));
} else {
next(utils.makeJSON(200, "Success", null));
}
});
}
function notifyAll(next) {
function checkAndNotify(host, callback) {
let toNotify = false;
if (host.heartbeatStatus != 200 || host.pingResponse != 200)
toNotify = true;
if (toNotify) {
let subject = "StatusBoard: Alert! "+host.name+" is down";
let body = "";
body += "Ping Response: "+host.pingResponse+"\n";
body += "Heartbeat Status: "+host.heartbeatStatus+"\n";
body += "Heartbeat Response: "+host.heartbeatData+"\n";
if(host.emails) {
for(let i=0;i<host.emails.length;i++) {
// mailer.sendMail(host.emails[i],"statusboard",subject,body);
}
}
if(host.slackwebhook) {
utils.notifySlack(host.slackwebhook || process.env.SLACK_WEBHOOK_URL,{text: subject+"\n\n"+body});
}
callback();
} else {
callback();
}
}
async.each(CONFIG.data, checkAndNotify, function (err) {
if (err) {
console.log("Err: ", err);
}
console.log("Checked services at: ", new Date().toString());
next();
});
}
function periodicCheck() {
console.log("Periodic Check started");
pingTest(function (pingVal) {
statusTest(function (statusVal) {
notifyAll(function () {
setTimeout(periodicCheck, 1000 * CONFIG.interval);
});
});
});
}
app.get('/', function (req, res) {
pingTest(function (pingVal) {
statusTest(function (statusVal) {
res.render('index.ejs', {
hosts: CONFIG.data
});
});
});
});
periodicCheck();
app.listen(PORT, () => console.log('Status board listening on port ' + PORT + '!'));