Skip to content

Commit 34898d1

Browse files
committed
Add cloudwatchlogs.js for reading generic cloudwatch logs
1 parent ba6efb1 commit 34898d1

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

cloudwatchlogs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Sumo Logic Functions for AWS CloudWatch Logs
44
Files
55
-----
66
* *node.js/cloudwatchlogs.js*: node.js file to collect data from AWS CWL. Can also be used to collect AWS VPC Flowlogs sent via CWL.
7+
* *node.js/cloudwatchlogs_vpc.js*: node.js file to collect AWS VPC Flowlogs sent via CWL.
78
* *node.js/cloudwatchlogs_lambda.js*: node.js file to collect AWS Lambda logs via CWL. This version extracts and add a "RequestId" field to each log line to make correlations easier.
89

910
Usage
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
///////////////////////////////////////////////////////////////////////////////////////////////////////////
2+
// Remember to change the hostname and path to match your collection API and specific HTTP-source endpoint
3+
// See more at: https://service.sumologic.com/help/Default.htm#Collector_Management_API.htm
4+
///////////////////////////////////////////////////////////////////////////////////////////////////////////
5+
var sumoEndpoint = 'https://collectors.sumologic.com/receiver/v1/http/<XXX>'
6+
7+
var https = require('https');
8+
var zlib = require('zlib');
9+
var url = require('url');
10+
11+
exports.handler = function(event, context) {
12+
var urlObject = url.parse(sumoEndpoint);
13+
14+
var options = { 'hostname': urlObject.hostname,
15+
'path': urlObject.pathname,
16+
'method': 'POST'
17+
};
18+
var zippedInput = new Buffer(event.awslogs.data, 'base64');
19+
20+
zlib.gunzip(zippedInput, function(e, buffer) {
21+
if (e) { context.fail(e); }
22+
23+
var awslogsData = JSON.parse(buffer.toString('ascii'));
24+
25+
if (awslogsData.messageType === "CONTROL_MESSAGE") {
26+
console.log("Control message");
27+
context.succeed("Success");
28+
}
29+
30+
var req = https.request(options, function(res) {
31+
var body = '';
32+
console.log('Status:', res.statusCode);
33+
res.setEncoding('utf8');
34+
res.on('data', function(chunk) { body += chunk; });
35+
res.on('end', function() {
36+
console.log('Successfully processed HTTPS response');
37+
context.succeed("Success"); });
38+
});
39+
40+
req.on('error', context.fail);
41+
42+
stream=awslogsData.logStream;
43+
group=awslogsData.logGroup;
44+
awslogsData.logEvents.forEach(function(val, idx, arr) {
45+
val.logStream = stream;
46+
val.logGroup = group;
47+
req.write(JSON.stringify(val) + '\n');
48+
});
49+
req.end();
50+
});
51+
};
52+

0 commit comments

Comments
 (0)