Skip to content

Instantly share code, notes, and snippets.

@bdkosher
Created January 16, 2014 23:01
Show Gist options
  • Save bdkosher/8465180 to your computer and use it in GitHub Desktop.
Save bdkosher/8465180 to your computer and use it in GitHub Desktop.
Command line script for parsing an access log and outputting some subset of data, e.g. groovy parseAccessLog.groovy "$path,$status,$elapsedTime" lists all the request paths, response codes, and elapsed time in a CSV format.
/**
* Breaks apart a line localhost_access_logs and provides properties to access the various components of that line
*
* Assumes that the format is
* Time Taken: 0.006 10.113.110.28 - - [12/Jan/2014:00:02:29 -0500] GET /path/ HTTP/1.1 http-bsd-dfw-1.xyz.com%2F10.113.110.25-8443-7 302 - E1518C5CABA9A32394BE209727824D3D.profile
* 0 1 2=et 3=ip 4 5 6=timestamp 7=to 8=h 9=up 10=pr 11=hostname%2Fhostip-port-thread 12=rc 13 14=???
*/
class Entry {
def elapsedTimeIdx = 2
def ipIdx = 3
def userIdx = 5
def timestampIdx = 6..7
def methodIdx = 8
def pathIdx = 9
def protocolIdx = 10
def statusIdx = 12
def parts
Entry(String line) {
parts = line.split(/\s/)
}
/**
* Logs use a "-" to represent a blank value. This class should return "" for blank values'
*/
def blankify(String part) {
part != '-' ? part : ''
}
def propertyMissing(String name) {
def returnVal
this.metaClass.properties.each { idxProp ->
if (idxProp.name == name + 'Idx') {
returnVal = idxPropValue(idxProp.name)
}
}
if (!returnVal) {
throw new MissingPropertyException("$name is not a property of Entry")
}
blankify(returnVal)
}
def idxPropValue(def idxPropName) {
def idx = this[idxPropName]
if (idx.class == IntRange) {
parts[idx].join(' ')
} else {
parts[idx]
}
}
Map toMap() {
def map = [:]
this.metaClass.properties.each { prop ->
if (prop.name.endsWith('Idx')) {
map[prop.name - 'Idx'] = blankify(idxPropValue(prop.name))
}
}
map
}
}
def template = new groovy.text.GStringTemplateEngine().createTemplate(new StringReader(args.length > 0 ? args[0] : '$ip = $user'))
System.in.eachLine { line ->
StringWriter sw = new StringWriter()
template.make(new Entry(line).toMap()).writeTo(sw)
println sw.toString()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment