3 require "apache_log_regex"
9 REQUESTS_PER_SECOND = <%= node[:tile][:ratelimit][:requests_per_second] %>
10 BLOCK_AT = <%= node[:tile][:ratelimit][:maximum_backlog] %>
11 UNBLOCK_AT = BLOCK_AT / 2
13 parser = ApacheLogRegex.new('%a %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"')
14 clients = LruRedux::Cache.new(1000000)
16 def decay_count(client, time)
17 decay = (time.to_i - client[:last_update]) * REQUESTS_PER_SECOND
19 client[:request_count] = [client[:request_count] - decay, 0].max
20 client[:last_update] = time.to_i
23 def write_blocked_ips(clients)
26 File.open("/srv/tile.openstreetmap.org/conf/ip.map.new", "w") do |file|
27 clients.each do |address, client|
28 decay_count(client, time)
30 if client[:request_count] >= UNBLOCK_AT
31 file.puts "#{address} blocked"
32 elsif client.has_key?(:blocked_at)
33 puts "Unblocked #{address}"
35 client.delete(:blocked_at)
40 File.rename("/srv/tile.openstreetmap.org/conf/ip.map.new",
41 "/srv/tile.openstreetmap.org/conf/ip.map")
46 next_check = write_blocked_ips(clients)
48 File::Tail::Logfile.tail("/var/log/apache2/access.log") do |line|
50 hash = parser.parse!(line)
54 next if address == "127.0.0.1" || address == "::1"
58 client = clients.getset(address) do
59 { :request_count => 0, :last_update => 0 }
62 decay_count(client, time)
64 client[:request_count] = client[:request_count] + 1
66 if client[:request_count] > BLOCK_AT && !client.has_key?(:blocked_at)
67 puts "Blocked #{address}"
69 client[:blocked_at] = time
72 elsif client[:request_count] < UNBLOCK_AT && client.has_key?(:blocked_at)
73 puts "Unblocked #{address}"
75 client.delete(:blocked_at)
81 next_check = write_blocked_ips(clients)
83 rescue ApacheLogRegex::ParseError