-
Notifications
You must be signed in to change notification settings - Fork 1
/
watch_dog.js
111 lines (99 loc) · 3.19 KB
/
watch_dog.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
const dns = require("dns");
const { WebClient } = require("@slack/web-api");
const domains = process.env.CHECK_DOMAINS.split(",");
const slackToken = process.env.SLACK_BOT_TOKEN;
const slackChannelSuccess = process.env.SLACK_CHANNEL_ID_SUCCESS; // 성공 시 보낼 채널 ID
const slackChannelFailure = process.env.SLACK_CHANNEL_ID_FAILURE; // 실패 시 보낼 채널 ID
const slackUserIds = process.env.SLACK_USER_IDS
? process.env.SLACK_USER_IDS.split(",")
: []; // 여러 사용자 ID
const slackGroupIds = process.env.SLACK_USER_GROUP_IDS
? process.env.SLACK_USER_GROUP_IDS.split(",")
: []; // 여러 그룹 ID
const web = new WebClient(slackToken);
// DNS 서버 설정
const dnsServers = {
KT: ["168.126.63.1", "168.126.63.2"],
LGUplus: ["164.124.101.2", "203.248.252.2"],
SKT: ["210.220.163.82", "219.250.36.130"],
};
async function sendSlackMessage(channel, message) {
try {
await web.chat.postMessage({
channel: channel,
text: message.map((block) => block.text.text).join("\n"),
blocks: message,
});
} catch (error) {
console.error("Error sending message to Slack:", error);
}
}
async function resolveDomain(resolver, domain) {
return new Promise((resolve, reject) => {
resolver.resolve4(domain, (err, addresses) => {
if (err) {
reject(err);
} else {
resolve(addresses);
}
});
});
}
async function checkDNS() {
let allSuccessful = true;
const errorMessages = [];
for (const domain of domains) {
for (const isp of Object.keys(dnsServers)) {
for (const server of dnsServers[isp]) {
const resolver = new dns.Resolver();
resolver.setServers([server]);
try {
const addresses = await resolveDomain(resolver, domain);
console.log(
`:white_check_mark: DNS lookup for *${domain}* using *${isp}* DNS server *${server}* succeeded: ${addresses}`
);
} catch (err) {
allSuccessful = false;
const errorMessage = `:x: DNS lookup failed for *${domain}* using *${isp}* DNS server *${server}*: ${err.message}`;
console.error(errorMessage);
errorMessages.push(errorMessage);
}
}
}
}
const blocks = [];
const groupMentions = slackGroupIds
.map((groupId) => `<!subteam^${groupId}>`)
.join(" ");
const userMentions = slackUserIds.map((userId) => `<@${userId}>`).join(" ");
const mentionTags = `${groupMentions} ${userMentions}`.trim();
if (allSuccessful) {
blocks.push({
type: "section",
text: {
type: "mrkdwn",
text: ":white_check_mark: All DNS lookups succeeded without any issues.",
},
});
await sendSlackMessage(slackChannelSuccess, blocks);
} else {
blocks.push({
type: "section",
text: {
type: "mrkdwn",
text: `:warning: *DNS Lookup Failures Detected!* :warning:\n${mentionTags} Some DNS lookups failed. See the details below:`,
},
});
errorMessages.forEach((errorMessage) => {
blocks.push({
type: "section",
text: {
type: "mrkdwn",
text: errorMessage,
},
});
});
await sendSlackMessage(slackChannelFailure, blocks);
}
}
checkDNS();