This repository has been archived by the owner on Aug 27, 2021. It is now read-only.
forked from mike-sol/ALB-Logs-to-Elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
334 lines (276 loc) · 9.45 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
* This project based on https://github.com/awslabs/amazon-elasticsearch-lambda-samples
* & https://github.com/blmr/aws-elb-logs-to-elasticsearch.git
*
* Function for AWS Lambda to get AWS ELB log files from S3, parse
* and add them to an Amazon Elasticsearch Service domain.
*
* Copyright 2015- Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Amazon Software License (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at http://aws.amazon.com/asl/
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
/* Imports */
var AWS = require('aws-sdk');
var LineStream = require('byline').LineStream;
var path = require('path');
var stream = require('stream');
var zlib = require('zlib');
var ES = require('elasticsearch');
/* Globals */
var indexTimestamp;
var esDomain;
var elasticsearch;
var s3 = new AWS.S3();
// Bulk indexing and stats
var totalIndexedLines = 0;
var totalStreamedLines = 0;
var bulkBuffer = [];
var bulkTransactions = 0;
// ES configs
var esTimeout = 100000;
var esMaxSockets = 20;
/* Lambda "main": Execution starts here */
exports.handler = function(event, context) {
// Set indexTimestamp and esDomain index fresh on each run
indexTimestamp = new Date().toISOString().replace(/\-/g, '.').replace(/T.+/, '');
esDomain = {
endpoint: process.env.ES_ENDPOINT,
index: process.env.ES_INDEX_PREFIX + '-' + indexTimestamp, // adds a timestamp to index. Example: alblogs-2015.03.31
doctype: process.env.ES_DOCTYPE,
maxBulkIndexLines: process.env.ES_BULKSIZE // Max Number of log lines to send per bulk interaction with ES
};
/**
* Get connected to Elasticsearch using the official elasticsearch.js
* client.
*/
elasticsearch = new ES.Client({
host: esDomain.endpoint,
apiVersion: '6.3',
//connectionClass: require('http-aws-es'),
log: 'error',
requestTimeout: esTimeout,
maxSockets: esMaxSockets
})
// Prepare bulk buffer
initBulkBuffer();
/* == Streams ==
* To avoid loading an entire (typically large) log file into memory,
* this is implemented as a pipeline of filters, streaming log data
* from S3 to ES.
* Flow: S3 file stream -> Log Line stream -> Log Record stream -> Lambda buffer -> ES Bulk API
*/
var lineStream = new LineStream();
// A stream of log records, from parsing each log line
var recordStream = new stream.Transform({
objectMode: true
})
recordStream._transform = function(line, encoding, done) {
var logRecord = parse(line.toString());
var serializedRecord = JSON.stringify(logRecord);
this.push(serializedRecord);
totalStreamedLines++;
done();
}
event.Records.forEach(function(record) {
var bucket = record.s3.bucket.name;
var objKey = decodeURIComponent(record.s3.object.key.replace(/\+/g, ' '));
s3LogsToES(bucket, objKey, context, lineStream, recordStream);
});
}
/*
* Get the log file from the given S3 bucket and key. Parse it and add
* each log record to the ES domain.
*
* Note: The Lambda function should be configured to filter for
* .log.gz files (as part of the Event Source "suffix" setting).
*/
function s3LogsToES(bucket, key, context, lineStream, recordStream) {
var s3Stream = s3.getObject({
Bucket: bucket,
Key: key
}).createReadStream();
var gunzipStream = zlib.createGunzip();
s3Stream
.pipe(gunzipStream)
.pipe(lineStream)
.pipe(recordStream)
.on('data', function(parsedEntry) {
// Add this log entry to the buffer
addToBulkBuffer(parsedEntry);
// See if it's time to flush and proceed
checkFlushBuffer();
})
.on('error', function() {
console.log(
'Error getting object "' + key + '" from bucket "' + bucket + '". ' +
'Make sure they exist and your bucket is in the same region as this function.');
context.fail();
})
.on('finish', function() {
flushBuffer();
console.log("Process complete. "+totalIndexedLines+" out of "+totalStreamedLines+" added in "+bulkTransactions+" transactions.");
context.succeed();
})
}
/*
* Bulk Buffering Functions
*/
function initBulkBuffer() {
bulkBuffer = [];
}
function addToBulkBuffer(doc) {
bulkBuffer.push(doc);
}
function checkFlushBuffer() {
if (bulkBuffer.length >= esDomain.maxBulkIndexLines) {
flushBuffer();
}
}
function flushBuffer() {
// Map the raw lines into an ES bulk transaction body
bulkBody = convertBufferToBulkBody(bulkBuffer);
// Submit to ES
postBulkDocumentsToES(bulkBody);
// Keep stats
numLines = bulkBody.length / 2;
totalIndexedLines += numLines;
bulkTransactions++;
// Clear the buffer
initBulkBuffer();
}
function convertBufferToBulkBody(buffer) {
bulkBody = [];
for (var i in buffer) {
logEntry = buffer[i];
bulkBody.push({ index: { _index: esDomain.index, _type: esDomain.doctype } });
bulkBody.push(logEntry);
}
return bulkBody;
}
function postBulkDocumentsToES(bulkBody) {
elasticsearch.bulk({ body: bulkBody });
}
/**
* Line Parser.
* It would have been much easier with Logstash Grok...
*/
function parse(line) {
var url = require('url');
// Fields in log lines are essentially space separated,
// but are also quote-enclosed for strings containing spaces.
var field_names = [
'type',
'timestamp',
'elb',
'client',
'target',
'request_processing_time',
'target_processing_time',
'response_processing_time',
'elb_status_code',
'target_status_code',
'received_bytes',
'sent_bytes',
'request',
'user_agent',
'ssl_cipher',
'ssl_protocol',
'target_group_arn',
'trace_id',
'domain_name',
'chosen_cert_arn',
'waf_number',
'waf_time',
'waf_message'
];
// First phase, separate out the fields
within_quotes = false;
current_field = 0;
current_value = '';
current_numeric = NaN;
var parsed = {};
// Remove trailing newline
if (line.match(/\n$/)) {
line = line.slice(0, line.length - 1);
}
// Character by character
for (var i in line) {
c = line[i];
if (!within_quotes) {
if (c == '"') {
// Beginning a quoted field.
within_quotes = true;
} else if (c == " ") {
// Separator. Moving on to the next field.
// Convert to numeric type if appropriate.
// This is needed to make sure Elasticsearch gets the
// dynamic mapping correct.
current_numeric = Number(current_value)
if (!isNaN(current_numeric)) {
current_value = current_numeric;
}
// Save current and reset.
parsed[field_names[current_field]] = current_value;
current_field++;
current_value = '';
} else {
// Part of this field.
current_value += c;
}
} else {
if (c == '"') {
// Ending a quoted field.
within_quotes = false;
} else {
// Part of this quoted field.
current_value += c;
}
}
}
// Save off the last one
parsed[field_names[current_field]] = current_value;
// Second phase, cleanups.
// Breaking out the port for the client and target, if there's a colon present
colon_sep = ['client', 'target']
for (var i in colon_sep) {
var orig = parsed[colon_sep[i]];
if (orig.indexOf(":") > 0) {
splat = orig.split(":");
parsed[colon_sep[i]] = splat[0]
parsed[colon_sep[i] + "_port"] = Number(splat[1])
}
}
// Dropping the target status code if there isn't one
if (parsed['target_status_code'] == '-') {
delete parsed['target_status_code']
}
if (parsed['undefined']) delete parsed['undefined']
// Third phase, parsing out the request into more fields
// Only do this if there's actually data in that field
if (parsed['request'].trim() != '- - -') {
splat = parsed['request'].split(" ");
// Basic values
parsed['request_method'] = splat[0]
parsed['request_uri'] = splat[1]
parsed['request_http_version'] = splat[2]
// If we can parse the URL, we can populate other fields properly
try {
uri = url.parse(splat[1]);
parsed['request_uri_scheme'] = uri.protocol ? uri.protocol : '';
parsed['request_uri_host'] = uri.hostname ? uri.hostname : '';
parsed['request_uri_port'] = uri.port ? uri.port : '';
parsed['request_uri_path'] = uri.pathname ? uri.pathname : '';
parsed['request_uri_query'] = uri.query ? uri.query : '';
}
// Otherwise, we just leave them out.
catch (e) {}
}
// All done.
return parsed;
}