Created
January 5, 2017 06:08
Star
You must be signed in to star a gist
parsing cloudtrail log files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const fs = require('fs'); | |
function displayEC2Event(event, type) { | |
if (event.errorCode) { | |
// -- errors is when someone is using his fat thumbs | |
console.log('ERROR: ', event.errorCode, event.errorMessage); | |
} else { | |
let items = event.responseElements.instancesSet.items; | |
for (let item = 0, len = items.length; item <= len; item++) { | |
if (items[item] !== undefined) { | |
console.log(items[item].instanceId, event.eventTime, type); | |
} | |
} | |
} | |
} | |
function parseEvents(eventsData) { | |
let data = JSON.parse(eventsData); | |
for (let event = 0, len = data.Records.length; event < len; event++) { | |
switch(data.Records[event].eventName) { | |
case 'RunInstances': | |
case 'StartInstances': | |
case 'StopInstances': | |
case 'TerminateInstances': | |
displayEC2Event(data.Records[event], data.Records[event].eventName); | |
break; | |
} | |
} | |
} | |
var jsonLine = ''; | |
var totalEvents = 0; | |
const splitRE = new RegExp('}{', 'g'); | |
const jsonStream = fs.createReadStream(__dirname + '/combined-cloudtrail.json'); | |
jsonStream.on('data', function (chunk) { | |
let buffered = (jsonLine + chunk).replace(splitRE, "}\n{").split('\n'); | |
for (let line = 0, len = buffered.length - 1; line < len; line++) { | |
parseEvents(buffered[line]); | |
totalEvents++; | |
} | |
jsonLine = buffered[buffered.length - 1]; | |
}); | |
jsonStream.on('close', function () { | |
console.log("\ntotal events parsed: ", totalEvents); | |
process.exit(0); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment