-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck.js
164 lines (135 loc) · 5.85 KB
/
check.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
'use strict';
var async = require('async');
var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');
var moment = require('moment');
var config = require('./lib/config');
var pool = require('./lib/database').pool;
var timechecker = require('./lib/timechecker');
var dns = require('./lib/dns');
setInterval(checkServers, 900000); // 15 min = 900000
// Mail transporter
var transporter = nodemailer.createTransport(smtpTransport({
port: 25,
host: 'localhost',
secure: false,
ignoreTLS: true
}));
var inCheck = false;
function checkServers(forceDnsUpdate) {
if (inCheck) {return;}
inCheck = true;
var reportList = [];
// Get servers from database
pool.query('SELECT server.*, maintainer.email FROM server, maintainer WHERE server.maintainerid = maintainer.maintainerid AND server.active = 1;', function (err, rows) {
if (err) {
console.error(err);
inCheck = false;
return;
}
// Hold if we need to update DNS
var DNSUpdate = false;
var AnyUpdate = false;
console.log('==== STARTING SERVER CHECKS ====');
/*console.log('Checking master server timestamp.txt...');
timechecker.checkMaster()
.then(function() {
// All okay!
console.log('Master server is good...');
console.log('');
checkAllServers();
})
.catch(function() {
// No good! The master server has a problem
// Send mail to admins and stop this checking round
console.log('Master server has troubles! Sending out mail to admins...');
transporter.sendMail({
from: config.admin.sender,
to: config.admin.email,
subject: 'ManiaCDN.net MASTER PROBLEMS!!',
text: 'The master timestamp.txt isnt updated or not reachable by the checker! All servers are not checked right now!'
});
});
*/
var checkAllServers = function() {
// Start timestamp checker
async.eachSeries(rows, function(row, callback) {
let serverid = row.serverid;
let ip4 = row.ip4;
let ip6 = row.ip6;
let status = row.status;
let email = row.email;
let ip = ip4 || ip6;
console.log(' Checking ' + ip + '...');
// Chech the server for status
timechecker.checkServer(ip, 80, status, function (err, needUpdate, needDNSUpdate, newStatus) {
console.log(' --> OldStatus: ' + status + ', newStatus: ' + newStatus);
// Add to report
if (needUpdate) {
reportList.push("Server (#" + serverid + ") " + ip + ": Change! " + status + " ===> " + newStatus);
AnyUpdate = true;
}else{
reportList.push("Server (#" + serverid + ") " + ip + ": No change.");
}
// Set the global dns update flag when status has changed and made the server inactive.
if (needDNSUpdate) {
DNSUpdate = true;
}
// Update database entry when status has been changed. And e-mail owner.
if (needUpdate) {
// Database update
pool.query('UPDATE server SET status = ? WHERE serverid = ?;', [newStatus, serverid], function (err) {
if (err) {
console.error(err);
}
return callback(null);
});
// If status >= 4 then send mail too
if (newStatus >= 4) {
transporter.sendMail({
from: config.admin.sender,
to: email,
bcc: config.admin.email,
subject: 'Your mirror is outdated! (ip: ' + ip + ')',
text: 'Hello, Your mirror (' + ip + ') is outdated, it hasn\'t been updating in over 24 hours! Please check the status of your sync script. (E-mail automaticly generated! Dont reply).'
});
}
}else{
return callback(null);
}
});
}, function() {
console.log('==== Server Checks Done ====');
// Force updating dns on first boot!
if (forceDnsUpdate) {
DNSUpdate = true;
}
// Do the DNS Update
if (DNSUpdate) {
console.log('==== DNS Update needed.. Executing... ====');
dns.updateRecords(function () { // err, success
inCheck = false;
sendReport(AnyUpdate, reportList);
console.log('==== Done ====');
});
}else{
inCheck = false;
//sendReport(AnyUpdate, reportList);
}
});
};
checkAllServers();
});
}
function sendReport(updated, reportList) {
if (updated && config.development) {
transporter.sendMail({
from: config.admin.sender,
to: config.admin.email,
subject: 'ManiaCDN.net: Update list (' + moment().format('DD-MM-YYYY HH:mm:ss') + ')',
text: 'Changes logged: \n\n' + reportList.join('\n') + '\n\n\nAuto Generated!'
});
}
}
// Run first time at boot of server checker
checkServers(true);