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