-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
239 lines (210 loc) · 7.64 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// var request = require('request');
var nodemailer = require('nodemailer');
var express = require('express');
var twilio = require('twilio');
var loginInfo = require('./settings/loginInfo.js');
var config = require('./settings/servicesConfig.js');
var client = twilio(loginInfo.TWILIO_ACCOUNT_SID, loginInfo.TWILIO_AUTH_TOKEN);
var app = express();
var retry = require('retry');
var request = require('request');
var rp = require('request-promise');
var services = config.services;
var alertGroup = config.alertGroup;
var retryConfig = config.retry;
var port = process.env.PORT || 2615;
app.listen(port);
console.log('Magic happens on port ' + port +' - '+ new Date());
app.get('/', function(req,res){
res.send({'status':'200'});
});
checkForValidAlerts(alertGroup);
populateServicesObj(services);
var intervalTime = config.intervalTime;
if(config.debugMode){
intervalTime = config.debugIntervalTime;
retryAttempts = config.debugRetryAttempts;
console.log('_____________________________\nIn debug mode will not send any alerts!!\n_____________________________');
allServicesCheck(services);
}
setInterval(function(){
allServicesCheck(services);
}, intervalTime*60*1000);
function allServicesCheck(services){
for (var i=0;i<services.length;i++){
var healthCheckEndPoint = services[i].route ? services[i].route : config.healthCheckEndpoint;
var port = services[i].port ? ':'+services[i].port : '';
console.log("checking", services[i].name);
checkServiceHealth(services[i].ip+port+healthCheckEndPoint,services[i],services);
}
}
function sendMessage(alertInfo, msgContent){
if(alertInfo) {
for (var i = 0; i < alertInfo.length; i++) {
if(alertInfo[i].email){
sendEmail(alertInfo[i],msgContent);
}
if(alertInfo[i].number){
sendText(alertInfo[i],msgContent);
}
if(alertInfo[i].iftttUrl){
sendIftttPush(alertInfo[i],msgContent);
}
}
}
}
function sendIftttPush(alertInfo, msgContent){
var options = {
method: 'POST',
uri:alertInfo.iftttUrl,
body: {
value1: msgContent
},
json: true
};
rp(options)
.then(function (parsedBody) {
// POST succeeded...
console.log('Successfully sent alert to ifttt', alertInfo);
})
.catch(function (err) {
// POST failed...
console.error(new Date(), 'error sending to IFTTT',err);
});
}
function sendText(alertInfo, msgContent){
if(!config.debugMode){
client.messages.create({
to: alertInfo.number,
from: loginInfo.twilioFromNumber,
body: msgContent
}, function(err, message) {
if(err){
console.error(new Date(), ' Error sending text message for message: ', message, '\nFor error: ', err);
} else {
console.log(new Date(),' Text sent: ', msgContent);
}
});
} else {
console.log('not sending text in debug mode',msgContent);
}
}
function sendEmail(alertInfo, msgContent){
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: loginInfo.gmailUsername,
pass: loginInfo.gmailPass
}
});
var mailOptions = {
from: loginInfo.gmailUsername,
to: alertInfo.email,
subject: 'Server down Alert!',
text: msgContent
};
if(!config.debugMode){
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
console.log(new Date(),' Email sent: ', msgContent);
});
} else {
console.log(new Date(), 'not sending email in debug mode', msgContent);
}
}
function sendAlert(serviceObj, isOnline){
var serviceName = serviceObj.name;
if(isOnline){
sendMessage(serviceObj.alertInfo,serviceName+' is online!');
serviceObj.needsToSend = true;
} else if(serviceObj.needsToSend) {
sendMessage(serviceObj.alertInfo,serviceName+' is offline!');
serviceObj.needsToSend = false;
}
}
//new
function retryRequest(serviceObj, ip, cb){
console.log('retry request',serviceObj.name,ip);
var operation = retry.operation(retryConfig);
operation.attempt(function(currentAttempt) {
var requestObj = request;
if(serviceObj.isAlternateHealthCheck){
request.put({strictSSL:false,timeout:40000, url: ip, form: {access_token:config.accessToken}}, function (error, response, body) {
var statusCode = response && response.statusCode ? response.statusCode : null;
if(body){
body = JSON.parse(body);
if(statusCode!=200 && statusCode!=401 || !body.online){
console.log('errror',serviceObj.name);
error = new Error('Invalid route for '+ serviceObj.name);
} else if(serviceObj.dependantServices) {
console.log(new Date(),'checking all services');
allServicesCheck(serviceObj.dependantServices);
}
} else {
error = new Error('No body from request for '+ serviceObj.name);
}
if(operation.retry(error)){
return;
}
cb(error ? operation.mainError() : null, serviceObj.name, ip);
});
} else{
request({strictSSL:false,timeout:3000, url: ip}, function (error, response, body) {
var statusCode = response && response.statusCode ? response.statusCode : null;
if(statusCode!=200 && statusCode!=401){
console.log('errror',serviceObj.name);
error = new Error('Invalid route for '+ serviceObj.name);
} else if(serviceObj.dependantServices) {
console.log(new Date(),'checking all services');
allServicesCheck(serviceObj.dependantServices);
}
if(operation.retry(error)){
return;
}
cb(error ? operation.mainError() : null, serviceObj.name, ip);
});
}
});
}
//mixed
function checkServiceHealth(ip,serviceObject){
//new
retryRequest(serviceObject,ip, function(err, name, ip){
if(err){
console.log('offline');
serviceObject.isOnline = false;
sendAlert(serviceObject,serviceObject.isOnline);
} else if (!serviceObject.isOnline) {
console.log('online');
serviceObject.isOnline = true;
sendAlert(serviceObject,serviceObject.isOnline);
}
});
}
function populateServicesObj(services){
var i=0;
while(i<services.length){
services[i].isOnline = true;
services[i].needsToSend = true;
if(services[i].dependantServices){
populateServicesObj(services[i].dependantServices);
}
i++;
}
}
function checkForValidAlerts(alertGroup){
for (var key in alertGroup) {
// skip loop if the property is from prototype
if (!alertGroup.hasOwnProperty(key)) continue;
var obj = alertGroup[key];
for (var prop in obj) {
// skip loop if the property is from prototype
if(!obj.hasOwnProperty(prop)) continue;
if(!obj[prop].number && !obj[prop].email && !obj[prop].iftttUrl){
console.error('Need a number, iftttUrl or email for alert group:', key, '\nFor user:', obj[prop]);
}
}
}
}