Created
February 10, 2021 19:34
-
-
Save kadamwhite/041efa7905014e666ed281d703afec0c to your computer and use it in GitHub Desktop.
Use like `docker logs docker_proxy_1 --follow | summarize-altis-traefik-logs
This file contains hidden or 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 { match } = require('assert'); | |
const { parse } = require('path'); | |
const readline = require('readline'); | |
const { getContainers, matchContainerByURL } = require( '/home/kadam/bin/get-docker-ips' ); | |
// Figure out if we asked for a certain number of events, or else show the 20 most recent. | |
const [ , , flag, val ] = process.argv; | |
let eventCount = 20; | |
if ( flag === '-n' && val ) { | |
eventCount = +val; | |
} | |
const rl = readline.createInterface( { | |
input: process.stdin, | |
output: process.stdout, | |
terminal: false | |
} ); | |
const output = []; | |
rl.on( 'line', ( line ) => { | |
if ( /^time/.test( line ) ) { | |
console.log( line ); | |
return; | |
} | |
if ( /favicon|wp-personal-data-exports/.test( line ) ) { | |
return; | |
} | |
output.push( line ); | |
} ); | |
const containers = []; | |
getContainers().then( c => c.forEach( ( container ) => { | |
containers.push( container ); | |
} ) ); | |
const getContainerName = ( url ) => { | |
const container = matchContainerByURL( containers, url ); | |
if ( container ) { | |
return container.name; | |
} | |
return '(unknown)'; | |
}; | |
const parseLogLine = ( line ) => { | |
const bits = []; | |
let currentBit = ''; | |
let acceptSpaces = false; | |
for ( let i = 0; i < line.length; i++ ) { | |
const c = line[i]; | |
if ( acceptSpaces === false && ! currentBit && /\[|"/.test( c ) ) { | |
acceptSpaces = true; | |
continue; | |
} | |
const bitIsOver = acceptSpaces | |
? /\]|"/.test( c ) | |
: /\s/.test( c ); | |
if ( bitIsOver ) { | |
if ( currentBit ) { | |
bits.push( currentBit ); | |
} | |
acceptSpaces = false; | |
currentBit = ''; | |
} else { | |
currentBit += c; | |
} | |
} | |
const [ sourceIp, , , date, req, status, , , uagent, , re, url ] = bits; | |
return { | |
date, | |
sourceIp, | |
req, | |
uagent, | |
status, | |
re, | |
url, | |
container: getContainerName( url ), | |
}; | |
}; | |
const summarizeRecentEvents = () => { | |
if ( ! output.length ) { | |
return; | |
} | |
const logEvents = output | |
.map( ( line ) => { | |
if ( /^time/.test( line ) || /favicon|wp-personal-data-exports/.test( line ) ) { | |
return null; | |
} | |
return parseLogLine( line ); | |
} ) | |
.filter( Boolean ); | |
const eventsByDate = []; | |
logEvents.forEach( ( event ) => { | |
const latestEvent = eventsByDate[ eventsByDate.length - 1 ] || null; | |
if ( latestEvent && latestEvent.date === event.date ) { | |
latestEvent.events.push( event ); | |
} else { | |
eventsByDate.push( { | |
date: event.date, | |
events: [ event ], | |
} ); | |
} | |
} ); | |
eventsByDate.forEach( ( event ) => { | |
const { date, events } = event; | |
console.log( | |
`\n${ date }:\n${ | |
events | |
.map( ( event ) => { | |
const { req, status, re, uagent, container } = event; | |
return ` - ${ req }\n ${ re }; ${ uagent }\n ${ status }: ${ container }`; | |
} ) | |
.join( '\n' ) | |
}` | |
); | |
} ); | |
// Reset output | |
output.length = 0; | |
}; | |
const interval = setInterval( summarizeRecentEvents, 1000 ); | |
process.on( 'exit', () => { | |
clearInterval( interval ); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Designed to be used in conjunction with this script to get the IPs of running containers