Skip to content

Instantly share code, notes, and snippets.

@eiryu
Last active August 29, 2015 14:18
Show Gist options
  • Save eiryu/841bbaabc701e6591a85 to your computer and use it in GitHub Desktop.
Save eiryu/841bbaabc701e6591a85 to your computer and use it in GitHub Desktop.
LTSV集計
// Groovy Version: 2.2.1 JVM: 1.7.0_51 Vendor: Oracle Corporation OS: Mac OS X
// 実行時に引数としてログファイル名を絶対パスで指定すること
// 特定の間隔でアクセス数を算出
@Grab('org.apache.commons:commons-lang3:3.2.1')
import org.apache.commons.lang3.time.FastDateFormat
@Grab('am.ik.ltsv4j:ltsv4j:0.9.0')
import am.ik.ltsv4j.LTSV
def logFile = new File(args[0])
def map = [:].withDefault { 0 }
def interval = 5
println "STARTED: $logFile"
logFile.eachLine { row ->
def parsed = LTSV.parser().parseLine(row)
def date = FastDateFormat.getInstance('[dd/MMM/yyyy:HH:mm:ss Z]', Locale.US).parse(parsed['time'])
def day = FastDateFormat.getInstance('dd').format(date)
def hour = FastDateFormat.getInstance('HH').format(date)
def min = FastDateFormat.getInstance('mm').format(date)
def responseTime = parsed['reqtime_microsec'].toBigDecimal()
def req = parsed['req']
// 集計対象時間を指定
if (req =~ /\.php/) {
if ('01'.equals(day)) {
// if (16 <= hour.toInteger() && hour.toInteger() <= 23) {
// mapのキー名作成
def minutes = sprintf '%02d', (min.toInteger() / interval).toInteger() * interval
def key = "${hour}_${minutes}"
m = map[key]
if (m) {
m['rt'] += responseTime
m['cnt']++
} else {
map[key] = [:].withDefault { 0 }
map[key]['cnt']++
map[key]['rt'] = responseTime
}
// }
}
}
}
println "FINISHED: $logFile"
println map.each {
def average = it.value['rt'] / it.value['cnt']
println "${it.key},${average}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment