-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
185 lines (148 loc) · 4.19 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
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
var fs = require("fs");
var http = require("http");
var https = require("https");
var async = require("async");
function main(config, callback){
var tasks = [];
config.forEach(function(mon){
if(mon.service == "http" || mon.service == "https"){
tasks.push(function(callback){
monitorHttp(mon, function(result, data){
if(result === false){
if(data.responseCode !== undefined) var info = "Response code: " + data.responseCode;
if(data.timeout !== undefined) var info = "Server doesn't response within " + data.timeout + " seconds";
if(data.checkKeyword !== undefined) var info = "Page body doesn't contain specified keyword: " + data.checkKeyword;
return callback(null, {"status": "error", "host": data.host, "info": info});
}
callback(null, {"status": "success", "host": data.host});
});
});
}
});
var data = {"errors": {}, "cleared": {}};
var errors = {};
// load errors from file
loadFromFile("./data.json", function(err, result){
if(err) return callback(err);
if(result){
data = result;
data.cleared = {};
}
// execute monitoring tasks
executeMonitor(tasks, function(err, data){
if(err) return callback(err);
// save actual results
saveToFile("./data.json", data, function(err){
if(err){
return callback(err);
}
callback(null, data);
});
});
});
function executeMonitor(tasks, callback){
async.parallel(tasks, function(err, results){
if(err) return callback(err);
// check for actual results
for(k in results){
var r = results[k];
if(r.status == "error"){
var message = "Host " + r.host + " is down! " + r.info;
errors[r.host] = {"message": message, "count": 1, "outageAt": new Date().getTime()};
if(data.errors[r.host] == undefined){
data.errors[r.host] = errors[r.host];
}
else{
// error exists yet
// so update count
data.errors[r.host].count++;
data.errors[r.host].lastUpdatedAt = new Date().getTime();
}
}
}
// check for previous results
for(host in data.errors){
if(errors[host] === undefined){
// not exists, so move to cleared
data.cleared[host] = data.errors[host];
data.cleared[host].clearedAt = new Date().getTime();
delete data.errors[host];
}
}
callback(null, data);
});
}
}
function monitorHttp(mon, callback){
var options = {
hostname: mon.host,
port: 80,
path: mon.path,
method: 'GET',
};
var timeout = false;
var httpRequestCallback = function(res){
if(res.statusCode != 200){
return callback(false, {"responseCode": res.statusCode, "host": mon.host});
}
if(mon.checkKeyword === undefined)
{
return callback(true, {"host": mon.host});
}
var body = "";
res.on("data", function(data){
body += data;
});
res.on("end", function(){
if(body.indexOf(mon.checkKeyword) == -1){
return callback(false, {"checkKeyword": mon.checkKeyword, "host": mon.host});
}
callback(true, {"host": mon.host});
});
}
if(mon.service == "http")
{
var req = http.request(options, httpRequestCallback);
}
else if(mon.service == "https")
{
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
options.port = 443;
var req = https.request(options, httpRequestCallback);
}
req.on("error", function(err){
if(timeout){
return callback(false, {"timeout": mon.timeout, "host": mon.host});
}
return callback(false, {"host": mon.host, "error": err});
});
req.setTimeout(mon.timeout * 1000, function(){
timeout = true;
req.abort();
});
req.end();
}
function saveToFile(file, data, callback){
var json = JSON.stringify(data);
fs.writeFile(file, json, function(err){
if(err) return callback(err);
callback(null);
});
}
function loadFromFile(file, callback){
fs.readFile(file, function(err, body){
if(err){
if(err.code !== 'ENOENT'){
return callback(err);
}
return callback(null, null);
}
try{
callback(null, JSON.parse(body));
}
catch(err){
callback(err);
}
});
}
exports.monitor = main;