Exclude:
- 'cookbooks/trac/files/default/trac-authenticate'
- 'cookbooks/planet/files/default/replication-bin/replicate-changesets'
+ - 'cookbooks/*/templates/*/*.erb'
- 'hooks/*'
- 'roles/*.rb'
oauth_key web_passwords["oauth_key"]
piwik_configuration piwik_configuration
end
+
+gem_package "apachelogregex"
+gem_package "file-tail"
+
+template "/usr/local/bin/api-statistics" do
+ source "api-statistics.erb"
+ owner "root"
+ group "root"
+ mode 0755
+end
+
+template "/etc/init.d/api-statistics" do
+ source "api-statistics.init.erb"
+ owner "root"
+ group "root"
+ mode 0755
+end
+
+service "api-statistics" do
+ action [:enable, :start]
+ supports :restart => true
+ subscribes :restart, "template[/usr/local/bin/api-statistics]"
+ subscribes :restart, "template[/etc/init.d/api-statistics]"
+end
--- /dev/null
+#!/usr/bin/ruby
+
+require "apache_log_regex"
+require "file-tail"
+require "json"
+
+def categorise_uri(line)
+ uri = line.split(" ")[1]
+
+ case uri
+ when %r{api/0\.6/map} then :map
+ when %r{api/0\.6/changeset/[0-9]*/upload} then :upload
+ when %r{api/0\.6/amf} then :amf
+ when %r{api/0\.6/(node|way|relation)/[0-9]*/history} then :history
+ when %r{api/0\.6/(node|way|relation)/[0-9]*/full} then :full
+ when %r{api/0\.6/trackpoints} then :trkpts
+ when %r{api/0\.6/} then :other
+ else :web
+ end
+end
+
+parser = ApacheLogRegex.new('%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\" %Ts')
+last_write = Time.now
+statistics = { :status => Hash.new(0), :uri => Hash.new(0) }
+
+File::Tail::Logfile.tail("/var/log/apache2/access.log") do |line|
+ begin
+ hash = parser.parse(line)
+ status = hash["%>s"]
+ uri = categorise_uri(hash["%r"])
+
+ statistics[:status][status] += 1
+ statistics[:uri][uri] += 1
+ rescue ApacheLogRegex::ParseError
+ # nil
+ end
+
+ if Time.now - last_write > 10
+ File.write("/srv/www.openstreetmap.org/rails/tmp/statistics.json", statistics.to_json)
+ last_write = Time.now
+ end
+end
--- /dev/null
+#!/bin/bash
+
+# DO NOT EDIT - This file is being maintained by Chef
+
+start() {
+ start-stop-daemon --start --chuid rails:adm --background --make-pidfile --pidfile /var/run/api-statistics.pid --exec /usr/local/bin/api-statistics
+}
+
+stop() {
+ start-stop-daemon --stop --retry 300 --pidfile /var/run/api-statistics.pid
+}
+
+case "$1" in
+ start)
+ start
+ ;;
+ stop)
+ stop
+ ;;
+ restart)
+ stop || exit $?
+ start
+ ;;
+esac