|
| 1 | +var AWS = require('aws-sdk'); |
| 2 | +var s3 = new AWS.S3(); |
| 3 | +var https = require('https'); |
| 4 | +var zlib = require('zlib'); |
| 5 | + |
| 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 | + |
| 11 | +var options = { 'hostname': 'endpoint1.collection.sumologic.com', |
| 12 | + 'path': 'https://endpoint1.collection.sumologic.com/receiver/v1/http/<XXXX>', |
| 13 | + 'method': 'POST' |
| 14 | + }; |
| 15 | + |
| 16 | + |
| 17 | +function s3LogsToSumo(bucket, objKey,context) { |
| 18 | + var req = https.request(options, function(res) { |
| 19 | + var body = ''; |
| 20 | + console.log('Status:', res.statusCode); |
| 21 | + res.setEncoding('utf8'); |
| 22 | + res.on('data', function(chunk) { body += chunk; }); |
| 23 | + res.on('end', function() { |
| 24 | + console.log('Successfully processed HTTPS response'); |
| 25 | + context.succeed(); |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + var finalData = ''; |
| 30 | + var totalBytes = 0; |
| 31 | + var isCompressed = false; |
| 32 | + if (objKey.match(/\.gz$/)) { |
| 33 | + isCompressed = true; |
| 34 | + } |
| 35 | + |
| 36 | + var finishFnc = function() { |
| 37 | + console.log("End of stream"); |
| 38 | + console.log("Final total byte read: "+totalBytes); |
| 39 | + req.end(); |
| 40 | + context.succeed(); |
| 41 | + } |
| 42 | + |
| 43 | + var s3Stream = s3.getObject({Bucket: bucket, Key: objKey}).createReadStream(); |
| 44 | + s3Stream.on('error', function() { |
| 45 | + console.log( |
| 46 | + 'Error getting object "' + objKey + '" from bucket "' + bucket + '". ' + |
| 47 | + 'Make sure they exist and your bucket is in the same region as this function.'); |
| 48 | + context.fail(); |
| 49 | + }); |
| 50 | + |
| 51 | + req.write('Bucket: '+bucket + ' ObjectKey: ' + objKey +'\n'); |
| 52 | + |
| 53 | + if (!isCompressed) { |
| 54 | + s3Stream.on('data',function(data) { |
| 55 | + //console.log("Read bytes:" +data.length); |
| 56 | + finalData += data; |
| 57 | + req.write(data+'\n'); |
| 58 | + totalBytes += data.length; |
| 59 | + }); |
| 60 | + s3Stream.on('end',finishFnc); |
| 61 | + } else { |
| 62 | + var gunzip = zlib.createGunzip(); |
| 63 | + s3Stream.pipe(gunzip); |
| 64 | + |
| 65 | + gunzip.on('data',function(data) { |
| 66 | + totalBytes += data.length; |
| 67 | + req.write(data.toString()+'\n'); |
| 68 | + finalData += data.toString(); |
| 69 | + }).on('end',finishFnc) |
| 70 | + .on('error',function(error) { |
| 71 | + context.fail(error); |
| 72 | + }) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +exports.handler = function(event, context) { |
| 77 | + options.agent = new https.Agent(options); |
| 78 | + event.Records.forEach(function(record) { |
| 79 | + var bucket = record.s3.bucket.name; |
| 80 | + var objKey = decodeURIComponent(record.s3.object.key.replace(/\+/g, ' ')); |
| 81 | + console.log('Bucket: '+bucket + ' ObjectKey: ' + objKey); |
| 82 | + s3LogsToSumo(bucket, objKey, context); |
| 83 | + }); |
| 84 | +} |
0 commit comments