Created
May 17, 2012 15:57
-
-
Save kurtsiegfried/2719798 to your computer and use it in GitHub Desktop.
A quick script to parse a rails log and do some geolocation work. Tested against a Rails 3.2.3 development.log file.
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 ruby | |
#This script requires a MaxMind GeoIP database. | |
#I tested with the database from: http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz | |
require 'geoip' | |
require 'json' | |
geo = GeoIP.new('GeoLiteCity.dat') | |
ips = Hash.new() | |
visitors = Array.new() | |
ARGF.each do |line| | |
if line.start_with?("Start") | |
ip = line.split(' ')[4] | |
if ips.has_key?ip | |
ips[ip]+= 1 | |
else | |
ips[ip] = 1 | |
end | |
end | |
end | |
ips.keys.each do |ip| | |
city = geo.city(ip).to_hash | |
puts "IP: "+ip | |
puts "Requests: "+ips[ip].to_s | |
city[:requests] = ips[ip] | |
puts "Country: "+city[:country_name].to_s | |
puts "Region: "+city[:region_name].to_s | |
puts "City: "+city[:city_name].to_s | |
visitors.push city | |
puts | |
end | |
File.open("visitors.json", 'w') {|f| f.write(visitors.to_json) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment