-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalert-error.js
158 lines (133 loc) · 3.5 KB
/
alert-error.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
// var zlib = require('zlib');
// exports.handler = function(input, context) {
// console.log(input);
// var payload = Buffer.from(input.awslogs.data, 'base64');
// zlib.gunzip(payload, function(e, result) {
// if (e) {
// context.fail(e);
// } else {
// result = JSON.parse(result.toString('ascii'));
// console.log("Event Data:", JSON.stringify(result, null, 2));
// context.succeed();
// }
// });
// };
// 구성 -> 환경변수로 webhook을 받도록 합니다.
const ENV = process.env;
if (!ENV.webhook) throw new Error("Missing environment variable: webhook");
const webhook = ENV.webhook;
const https = require("https");
const zlib = require("zlib");
exports.handler = (input, context) => {
var payload = Buffer.from(input.awslogs.data, "base64");
zlib.gunzip(payload, async (e, result) => {
if (e) {
context.fail(e);
}
const resultAscii = result.toString("ascii");
let resultJson;
try {
resultJson = JSON.parse(resultAscii);
} catch (e) {
console.log(
`[알람발송실패] JSON.parse(result.toString('ascii')) Fail, resultAscii= ${resultAscii}`
);
context.fail(e);
return;
}
// console.log(`result Ascii = ${resultAscii}`);
console.log(`logEvents = ${JSON.stringify(resultJson.logEvents[0])}`);
const logJson = toJson(resultJson.logEvents[0]);
try {
const message = slackMessage(logJson);
console.log(message);
await exports.postSlack(message, webhook);
} catch (e) {
console.log(`slack message fail= ${JSON.stringify(logJson)}`);
return;
}
});
};
function toJson(logEvent) {
let message = logEvent.message;
const currentTime = toYyyymmddhhmmss(logEvent.timestamp);
message = '"age" must be an interger';
return {
currentTime: currentTime,
message: message,
};
}
// 타임존 UTC -> KST
function toYyyymmddhhmmss(timestamp) {
if (!timestamp) {
return "";
}
function pad2(n) {
return n < 10 ? "0" + n : n;
}
var kstDate = new Date(timestamp + 32400000);
return (
kstDate.getFullYear().toString() +
"-" +
pad2(kstDate.getMonth() + 1) +
"-" +
pad2(kstDate.getDate()) +
" " +
pad2(kstDate.getHours()) +
":" +
pad2(kstDate.getMinutes()) +
":" +
pad2(kstDate.getSeconds())
);
}
function slackMessage(messageJson) {
const message = `When: ${messageJson.currentTime}\nMessage:${messageJson.message}`;
return {
attachments: [
{
color: "#2eb886",
title: `Error Detecting`,
fields: [
{
value: message,
short: false,
},
],
},
],
};
}
exports.postSlack = async (message, slackUrl) => {
return await request(exports.options(slackUrl), message);
};
exports.options = (slackUrl) => {
const { host, pathname } = new URL(slackUrl);
return {
hostname: host,
path: pathname,
method: "POST",
headers: {
"Content-Type": "application/json",
},
};
};
function request(options, data) {
return new Promise((resolve, reject) => {
const req = https.request(options, (res) => {
res.setEncoding("utf8");
let responseBody = "";
res.on("data", (chunk) => {
responseBody += chunk;
});
res.on("end", () => {
resolve(responseBody);
});
});
req.on("error", (err) => {
console.error(err);
reject(err);
});
req.write(JSON.stringify(data));
req.end();
});
}