forked from awslabs/cloudwatch-logs-customize-alarms
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
executable file
·161 lines (143 loc) · 4.89 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
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
159
160
161
'use strict'
let aws = require('aws-sdk');
let cwl = new aws.CloudWatchLogs();
let sns = new aws.SNS();
let ses = new aws.SES();
const FROM_MAIL = '[email protected]'
const NOTIFY_MAILS = ['[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]']
const NOTIFICATION_ARN = 'arn:aws:sns:us-west-2:378652543277:email-on-warn'
const REGION = 'us-west-2'
const SINGLE_ERROR_LENGTH = 255
exports.handler = function (event, context, callback) {
console.dir(JSON.stringify(event, null, 2))
let notificationContent = {}
let message = {}
let metricFilter = {}
Promise.resolve()
.then(() => {
message = JSON.parse(event.Records[0].Sns.Message)
return {
metricName: message.Trigger.MetricName,
metricNamespace: message.Trigger.Namespace
}
})
.then(requestParams => {
return new Promise((resolve, reject) => {
cwl.describeMetricFilters(requestParams, function (err, data) {
if (err) {
reject(err)
} else {
console.log('Metric Filter data is:', data);
resolve(data)
}
})
})
})
.then(metricFilterData => {
metricFilter = metricFilterData.metricFilters[0]
return getLogsAndSendNotification(message, metricFilterData)
})
.then(logEvents => {
notificationContent = generateNotificationContent(logEvents, message, metricFilter.logGroupName)
})
.then(() => {
console.log('Send an e-mail', notificationContent)
return new Promise((resolve, reject) => {
ses.sendEmail({
Destination: {
ToAddresses: NOTIFY_MAILS
},
Message: {
Body: {
Html: {
Data: notificationContent.Message
}
},
Subject: {
Data: notificationContent.Subject
}
},
Source: FROM_MAIL
}, (err, data) => {
console.log('MAIL_RESULT', err, data)
if (err) {
reject(err)
} else {
console.log("===EMAIL SENT===");
resolve(data)
}
})
})
})
.then(() => {
console.log('Success')
callback(null, 'Success sending notifications')
})
.catch(err => {
console.log('Error', err)
callback(err)
})
};
function getLogsAndSendNotification (message, metricFilterData) {
let timestamp = Date.parse(message.StateChangeTime);
let offset = 2 * message.Trigger.Period * message.Trigger.EvaluationPeriods * 1000; // double the offset
let metricFilter = metricFilterData.metricFilters[0];
let parameters = {
'logGroupName': metricFilter.logGroupName,
'filterPattern': metricFilter.filterPattern ? metricFilter.filterPattern : "",
'startTime': timestamp - offset,
'endTime': timestamp
}
return filterLogEvents(parameters)
}
function filterLogEvents (parameters) {
console.log('Filter log events for parameters', parameters)
let events = []
return new Promise((resolve, reject) => {
cwl.filterLogEvents(parameters, function (err, data) {
if (err) {
reject(err)
} else {
console.log('FilterLogEvents response', data)
events = data.events
return Promise.resolve()
.then(() => {
if (data.nextToken && data.nextToken.length > 0) {
console.log('NextToken available', data.nextToken)
parameters.nextToken = data.nextToken
return filterLogEvents(parameters)
.then(childEvents => {
console.log('ChildEvents', childEvents)
return events.concat(childEvents)
})
}
return events
})
.then(events => {
resolve(events)
})
}
});
})
}
function generateNotificationContent (events, message, logGroupName) {
console.log('Events are:', events);
let logData = '<br/>Logs:<br/><br />';
for (let i in events) {
logData += '<b>Message:</b>' + JSON.stringify(events[i]['message']).substr(0, SINGLE_ERROR_LENGTH) + '<br />';
logData += '<a href="https://console.aws.amazon.com/cloudwatch/home?region=' + REGION + '#logEventViewer:group=' + logGroupName + ';stream=' + events[i]['logStreamName'] + ';reftime=' + events[i]['timestamp'] + ';refid=' + events[i]['eventId'] + '">Click to open logs in CloudWatch console</a><br />'
logData += '<br />'
}
let date = new Date(message.StateChangeTime);
let text = 'Alarm Name: ' + '<b>' + message.AlarmName + '</b><br/>' +
'Account ID: ' + message.AWSAccountId + '<br/>' +
'Region: ' + message.Region + '<br/>' +
'Alarm Time: ' + date.toString() + '<br/>' +
logData;
text += '<br />Best regards,<br />Yours Log Provider'
return {
Message: text,
Subject: 'PRD/STG: Details for Alarm - ' + message.AlarmName,
TopicArn: NOTIFICATION_ARN
};
}