-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
67 lines (60 loc) · 2.25 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
const express = require('express')
const https = require('https')
const domainPing = require("domain-ping");
const connectivity = require('connectivity');
const CronJob = require('cron').CronJob;
const nodemailer = require('nodemailer');
require('dotenv').config()
const app = express()
const port = 3000
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: process.env.EMAIL,
pass: process.env.PASSWORD
}
});
let sites = ['www.salvinipremier.it', 'www.asst-franciacorta.it', 'onefirewall.com', 'app.onefirewall.com', 'onefirewall.cloud']
const job = new CronJob('0 0 */1 * * *', function () { // every 2 minutes 0 */2 * * * *
const d = new Date();
// first check for internet connection
connectivity(function (online) {
if (online) {
console.log('connected to the internet!')
// loop the sites
for (const site of sites) {
// ping each site
domainPing(site)
.then((res) => {
console.log(res.domain, ' is online');
})
.catch((error) => {
console.log(error.domain, ' is offline');
const mailOptions = {
from: process.env.EMAIL,
to: process.env.RECEIVER,
subject: 'Site down',
html: `<p>The site <strong> ${error.domain} </strong> is offline. Time: ${d} </p>`
};
// send email with the options above
transporter.sendMail(mailOptions, function (err, info) {
if (err)
console.log(err)
else
console.log('Email sent to notify the site was down: ', info);
});
console.error(error);
});
}
} else {
console.error('sorry, not connected!')
}
})
});
job.start();
app.listen(port, (err) => {
if (err) {
return console.log('Something bad happened', err)
}
console.log(`Server is listening on ${port}`)
})