-
Notifications
You must be signed in to change notification settings - Fork 1
/
check.js
99 lines (87 loc) · 2.5 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
require("dotenv").config();
const ConfigUtils = require("./src/config-utils");
const got = require("got");
const nodemailer = require("nodemailer");
const smtpTransport = require("nodemailer-smtp-transport");
const run = async () => {
const { targets, masterSmtpSettings } = new ConfigUtils();
const testResponses = await testTargets(targets);
const failedTestes = filterResponses(testResponses);
return sendAlerts(failedTestes, masterSmtpSettings);
};
const testTargets = (targets) =>
Promise.all(targets.map((target) => requestToTarget(target)));
const filterResponses = (responses) =>
responses.filter((response) => !!response);
const sendAlerts = (responses, masterSmtpSettings) =>
Promise.all(
responses.map((response) => {
const target = response.target;
const subject = `${target.app_name} is down!`;
const content = `${new Date().toISOString()}\n${subject}\nStatus Code: ${
response.statusCode
}\nError:\n${response.body}\nFull stack:\n${response.error}`;
return sendMail(
target.report_email,
subject,
target.smtp_settings || masterSmtpSettings,
content
);
})
);
const requestToTarget = (target) => {
if (!target.url) throw new Error("URL is mandatory");
let requestOpts = {
url: target.url,
method: target.method || "GET",
};
return got(requestOpts)
.then(() => {
console.log(`${new Date().toISOString()} ${target.app_name} is ok!`);
return false;
})
.catch((error) => {
console.log(
`${new Date().toISOString()} ${
target.app_name
} is down! E-mail will be sent.`
);
return {
target,
statusCode: error?.response?.statusCode,
body: error?.response?.body,
error,
};
});
};
const sendMail = async (to, subject, smtpSettings, content) => {
const transporter = nodemailer.createTransport(
smtpTransport({
host: smtpSettings.host,
port: smtpSettings.port,
secure: smtpSettings.port === 465 ? true : false,
auth: {
user: smtpSettings.user,
pass: smtpSettings.pass,
},
})
);
const mailOpts = {
to,
from: smtpSettings.from,
subject,
text: content, // plaintext body
envelope: {
to,
from: smtpSettings.from,
},
};
const response = await transporter.sendMail(mailOpts);
console.log(
`${new Date().toISOString()} "${subject}" is sent: ${response.response}`
);
};
(async () => {
await run();
process.exit(0);
})();