-
Notifications
You must be signed in to change notification settings - Fork 0
/
globi.rb
executable file
·273 lines (219 loc) · 5.42 KB
/
globi.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/usr/env ruby
require 'rubygems'
require 'geoip'
require 'date'
require 'optparse'
class Globi
attr_writer :formatter
def formatter
@formatter ||= Globi::PrintFormater.new
end
def geo_ip
@geo_ip ||= default_geo_ip
end
def scan(io)
formatter.open do |fmt|
io.each_line do |line|
if geo_info = scan_line(line)
fmt.process_entry(geo_info)
end
end
end
end
protected
COMMON_FORMAT = /
(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+
.*\s+
\[(\d{1,2}\/\w+\/\d{2,4}:\d{2}:\d{2}:\d{2}\s[+-]\d{1,4})\]\s+
"(\w+)\s+(.+)\s+([\w\/\.\\]+)\s*"\s+
(\d{3})\s+(\d+|-)\s+
"(.*)"\s+
"(.*)"
/ixm
# For now assume scan parses just Apache Common Log Format logs
def scan_line(line)
if m = COMMON_FORMAT.match(line)
ip = m[1]
GeoInfo.new( geo_ip.city(ip), parse_date(m[2]) )
else
nil
end
end
def default_geo_ip
geo_db = File.join(File.dirname(__FILE__), "db", 'GeoLiteCity.dat')
GeoIP.new(base)
end
DATE_FORMAT = '%d/%b/%Y:%H:%M:%S %Z'
def parse_date(date)
DateTime.strptime(date, DATE_FORMAT) #.to_time
end
end
class Globi::GeoInfo
attr_reader :country, :region, :city, :latitude, :longitude, :date, :country_code
def initialize(city_data, date)
@country_code = city_data[2]
@country = city_data[4]
@region = city_data[6]
@city = city_data[7]
@latitude = city_data[9]
@longitude = city_data[10]
@date = date
end
def to_s
"Country: #{country} Region: #{region} City: #{city} Lat: #{latitude} Long: #{longitude}"
end
end
class Globi::Formatter
attr_writer :output_stream
def output_stream
@output_stream ||= $stdout
end
def open
open_scan
yield self
ensure
close_scan
end
def process_entry(geo_info)
output_stream.puts process_geo_entry(geo_info)
end
def open_scan
output_stream.puts process_open
end
def close_scan
output_stream.puts process_close
end
protected
# overwrite the following methods
def process_open
end
def process_close
end
def process_geo_entry(geo_info)
end
end
class Globi::GroupedFormatter < Globi::Formatter
def initialize
@formatters = []
end
def <<(formatrtr)
@formatters << formatter
end
def open_scan
@formatters.each { |f| f.open_scan }
end
def close_scan
@formatters.each { |f| f.close_scan }
end
def process_entry(geo_info)
@formatters.each { |f| f.process_entry(geo_info) }
end
end
# Simple debug formatter that outputs each geo_info data point
class Globi::PrintFormatter < Globi::Formatter
def process_geo_entry(geo_info)
output_stream.puts geo_info
end
end
# Create a KML XML file suitable for GoogleEarth
class Globi::KMLFormatter < Globi::Formatter
attr_accessor :destination_longitude, :destination_latitude, :destination_name
def process_geo_entry(geo)
<<-KML
<Placemark>
<name>#{geo.country} : #{geo.region} - #{geo.city}</name>
<description>#{geo.country} : #{geo.region} - #{geo.city}</description>
<TimeStamp>#{geo.date}</TimeStamp>
<styleUrl>#yellowLineGreenPoly</styleUrl>
<LineString>
<extrude>1</extrude>
<tessellate>1</tessellate>
<coordinates>#{geo.longitude},#{geo.latitude}
139.701204,35.655614
</coordinates>
</LineString>
</Placemark>
KML
end
def process_open
<<KML
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<name>Web Tokyo</name>
<description>Web Requests to Tokyo around the world</description>
<Style id="yellowLineGreenPoly">
<LineStyle>
<color>7f00ffff</color>
<width>4</width>
</LineStyle>
<PolyStyle>
<color>7f00ff00</color>
</PolyStyle>
</Style>
<Placemark>
<name>iKnow!</name>
<description>Smar.fm home</description>
<Point>
<coordinates>139.701204,35.655614,0</coordinates>
</Point>
</Placemark>
KML
end
def process_close
<<KML
</Document>
</kml>
KML
end
end
# Creates a URL to a google chart with a map of the world where each country
# is colored based on the number of hits it gets
class Globi::GoogleChartFormatter < Globi::Formatter
SYMBOLS = ("A".."Z").to_a + ("a".."z").to_a + ('0'..'9').to_a
def initialize
@hits = Hash.new(0)
end
def process_geo_entry(geo_info)
@hits[geo_info.country_code] += 1
nil # don't want output yet
end
# note could do something fun like on close just read the link and save an image
def process_close
max = @hits.values.max
min = @hits.values.min
chld = ""
data = ""
@hits.each do |country, val|
chld << country
data << SYMBOLS[(val - min) / max * SYMBOLS.size]
end
query = [
"cht=t",
"chs=440x220",
"chtm=world",
"chd=s:#{data}",
"chco=ffffff,f4ed28,f11414",
"chld=#{chld}",
"chf=bg,s,EAF7FE"
]
"http://chart.apis.google.com/chart?" + query.join("&")
end
end
if __FILE__ == $0
io = $stdin
globi = Globi.new
aggregate = Globi::GroupedFormatter.new
globi.formatter = aggregate
OptionParser.new do |opts|
opts.banner = "Usage: #{File.basename(__FILE__)} [options]"
opts.separator ""
opts.on("-f", "--formatter Formatter") do |formatter|
aggregate << const_get("Globi::#{formatter}").new
end
opts.on("-i", "--input input_file") do |input|
io = input == "-" ? $stdin : File.new(input, "r")
end
end.parse!(ARGV)
globi.scan(io)
end