-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·99 lines (90 loc) · 3.38 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
//require('dotenv').config(); //Uncomment this line of building from source.
const https = require("https");
const schedule = require('node-schedule');
const nodemailer = require("nodemailer");
let counter = 0;
console.log("started running");
schedule.scheduleJob(process.env.FREQUENCY, function(){
console.log("checking site status");
runJob();
});
const runJob = async ()=> {
require('dotenv').config();
const isOnline = await checkIfWebsiteIsOnline(process.env.URL,process.env.TIMEOUT);
if(isOnline===false){
console.log("site not reachable, checking in "+process.env.WAITUNTILRECHECK+"min again");
await sleep(process.env.WAITUNTILRECHECK);
const recheck = await checkIfWebsiteIsOnline(process.env.URL,process.env.TIMEOUT);
if(recheck===false){
counter++;
console.log("rechecking site, still not reachable.");
let transporter = nodemailer.createTransport({
host: process.env.SMTPSERVER,
port: process.env.SMTPPORT,
secure: false,
auth: {
user: process.env.FROMEMAIL,
pass: process.env.SMTPPASSWORD
},
});
//fout meer dan maxfailurecounter
if(counter>=process.env.MAXFAILURECOUNTER){
console.log("MAXFAILURECOUNTER exceeded");
try{
await transporter.sendMail({
from: `"EBCS DRP server checker" <${process.env.FROMEMAIL}>`, // sender address
to:process.env.ADMINEMAIL,
bcc: process.env.EMAILLIST.split(","), // list of receivers
subject: process.env.EMAILSUBJECT, // Subject line
text: process.env.EMAILTEXT, // plain text body
});
counter = 0;
}catch(e){
counter--;
console.log("Internet is probably down locally");
}
}else{
console.log("did not exceed MAXFAILURECOUNTER, counter on "+counter);
//fout minder dan maxfailurecounter
try{
await transporter.sendMail({
from: `"EBCS DRP server checker" <${process.env.FROMEMAIL}>`, // sender address
to:process.env.ADMINEMAIL,
subject: process.env.EMAILSUBJECT, // Subject line
text: process.env.EMAILTEXT, // plain text body
});
}catch(e){
counter--;
console.log("Internet is probably down locally");
}
}
}else{
}
}else{
counter = 0;
console.log("site it reachable");
}
}
/**
* @method checkIfWebsiteIsOnline
* @description checks if the provided link is offline or online
* @param {String} url - The link of the website it should check on
* @returns {Boolean} - returns true if online, false if offline
*/
const checkIfWebsiteIsOnline = (url,timeout=30*100/*ms*/) => {
return new Promise((resolve, reject) => {
https
.get(url, function(res) {
resolve(res.statusCode === 200);
})
.on("error", function(e) {
resolve(false);
});
setTimeout(()=>{
resolve(false);
},timeout);
})
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}