]> git.openstreetmap.org Git - chef.git/blob - cookbooks/web/templates/default/api-statistics.erb
Redirect all names to canonical site
[chef.git] / cookbooks / web / templates / default / api-statistics.erb
1 #!/usr/bin/ruby
2
3 require "apache_log_regex"
4 require "file-tail"
5 require "json"
6
7 def categorise_uri(line)
8   uri = line.split(" ")[1]
9
10   case uri
11   when %r{api/0\.6/map} then :map
12   when %r{api/0\.6/changeset/[0-9]*/upload} then :upload
13   when %r{api/0\.6/amf} then :amf
14   when %r{api/0\.6/(node|way|relation)/[0-9]*/history} then :history
15   when %r{api/0\.6/(node|way|relation)/[0-9]*/full} then :full
16   when %r{api/0\.6/trackpoints} then :trkpts
17   when %r{api/0\.6/} then :other
18   else :web
19   end
20 end
21
22 parser = ApacheLogRegex.new('%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\" %Dus %{UNIQUE_ID}e %{SSL_PROTOCOL}x %{SSL_CIPHER}x')
23 last_write = Time.now
24 statistics = { :status => Hash.new(0), :uri => Hash.new(0) }
25
26 File::Tail::Logfile.tail("/var/log/apache2/access.log") do |line|
27   begin
28     hash = parser.parse(line)
29     status = hash["%>s"]
30     uri = categorise_uri(hash["%r"])
31
32     statistics[:status][status] += 1
33     statistics[:uri][uri] += 1
34   rescue ApacheLogRegex::ParseError
35     # nil
36   end
37
38   if Time.now - last_write > 10
39     File.write("/srv/www.openstreetmap.org/rails/tmp/statistics.json", statistics.to_json)
40     last_write = Time.now
41   end
42 end