Created
November 2, 2016 11:30
-
-
Save aleung/297b84fb38e6eedd3d1148ab04cf5a5b to your computer and use it in GitHub Desktop.
Merge consul agent logs which are fetched by Ansible
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 | |
"use strict"; | |
const fs = require('fs'); | |
const readline = require('readline'); | |
const logLineRegexp = /\s+(.+?)\[(.+?)\](.*)/; | |
function processFile(file, host) { | |
const lineReader = readline.createInterface({ | |
input: fs.createReadStream(file) | |
}); | |
lineReader.on('line', (line) => { | |
const matches = line.match(logLineRegexp); | |
if (matches) { | |
console.log(matches[1], '|', host, '|', matches[2], '|', matches[3]); | |
} | |
}); | |
} | |
const filenameRegexp = /agent-\d+\.\d+\.\d+\.(\d+)\.log/; | |
fs.readdir('.', (err, files) => { | |
for (let file of files) { | |
const matches = file.match(filenameRegexp); | |
if (matches) { | |
const host = matches[1]; | |
processFile(file, host); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment