Skip to content

Instantly share code, notes, and snippets.

@elrikdante
Last active August 29, 2015 14:18
Show Gist options
  • Save elrikdante/46c0ebf615f72d76d4b2 to your computer and use it in GitHub Desktop.
Save elrikdante/46c0ebf615f72d76d4b2 to your computer and use it in GitHub Desktop.
NGINX log scraper for time/route based data aggregation
#!/usr/bin/env ruby
require 'json'
require 'time'
data = []
counts = Hash.new(0)
uniq_paths = []
time_series = Hash.new do |h,k|
h[k] = Hash.new(0)
end
triggers = {
path: ->(path,map) do
counts[path] += 1
uniq_paths.push(path)
path
end,
date: ->(date,map) do
Date.parse(date).strftime("%d/%m/%Y").tap do |index|
time_series[index][map[:path]] += 1
end
end
}
while line = $stdin.gets do
next if line =~ /socket/
columns = [:path, :date]
data << line.scan(/\[(.+)\]."([^"]+)"/).flatten.reverse.inject({}) do |map, s|
c = columns.shift
map.update c => triggers[c].call(s,map)
end
end
uniq_paths.uniq!
$stdout.puts time_series.to_json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment