6 require 'apache_log_regex'
14 parser = ApacheLogRegex.new('%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %x')
15 IO.popen("tail -n #{NUM_LINES} /var/log/apache2/access.log").each_line do |line|
17 hash = parser.parse(line)
19 t = DateTime.strptime(hash["%t"], "[%d/%b/%Y:%H:%M:%S %z]")
20 min_time = [min_time, t].compact.min
21 max_time = [max_time, t].compact.max
23 rescue ApacheLogRegex::ParseError => e
27 [min_time, max_time, lines]
31 :map => "Map API calls",
32 :upload => "Changeset diff uploads",
33 :amf => "AMF API calls",
34 :history => "Element history fetches",
35 :full => "Full element fetches",
36 :trkpts => "GPX trackpoints calls",
37 :web => "Web site traffic",
38 :other => "Other API calls"
41 def categorise_uri(line)
42 uri = line.split(" ")[1]
45 when /api\/0\.6\/map/ then :map
46 when /api\/0\.6\/changeset\/[0-9]*\/upload/ then :upload
47 when /api\/0\.6\/amf/ then :amf
48 when /api\/0\.6\/(node|way|relation)\/[0-9]*\/history/ then :history
49 when /api\/0\.6\/(node|way|relation)\/[0-9]*\/full/ then :full
50 when /api\/0\.6\/trackpoints/ then :trkpts
51 when /api\/0\.6\// then :other
56 if ARGV[0] == 'config'
57 puts "graph_title Requests processed"
58 puts "graph_vlabel Number of requests per minute"
59 puts "graph_category api"
60 CALL_TYPES.each { |k, v| puts "#{k}.label #{v}" }
63 min_time, max_time, lines = uris_from_logs
64 delta_t = (max_time - min_time).to_f * 24 * 60
66 collect {|x| categorise_uri(x)}.
67 inject(Hash.new) do |h, e|
76 CALL_TYPES.keys.each do |type|
77 count = counts[type] || 0
78 puts "#{type}.value #{count / delta_t}"