]> git.openstreetmap.org Git - chef.git/blob - cookbooks/munin/files/default/plugins/api_calls_num
Standardise on double quoted strings
[chef.git] / cookbooks / munin / files / default / plugins / api_calls_num
1 #!/usr/bin/ruby
2
3 require "rubygems"
4 require "date"
5 gem "home_run", ">= 0"
6 require "apache_log_regex"
7
8 NUM_LINES = 10000
9
10 def uris_from_logs
11   lines = []
12   max_time = nil
13   min_time = nil
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|
16     begin
17       hash = parser.parse(line)
18       uri = hash["%r"]
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
22       lines << uri
23     rescue ApacheLogRegex::ParseError
24       # nil
25     end
26   end
27   [min_time, max_time, lines]
28 end
29
30 CALL_TYPES = {
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"
39 }
40
41 def categorise_uri(line)
42   uri = line.split(" ")[1]
43
44   case uri
45   when %r{api/0\.6/map} then :map
46   when %r{api/0\.6/changeset/[0-9]*/upload} then :upload
47   when %r{api/0\.6/amf} then :amf
48   when %r{api/0\.6/(node|way|relation)/[0-9]*/history} then :history
49   when %r{api/0\.6/(node|way|relation)/[0-9]*/full} then :full
50   when %r{api/0\.6/trackpoints} then :trkpts
51   when %r{api/0\.6/} then :other
52   else :web
53   end
54 end
55
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}" }
61
62 else
63   min_time, max_time, lines = uris_from_logs
64   delta_t = (max_time - min_time).to_f * 24 * 60
65   counts = lines
66            .collect { |x| categorise_uri(x) }
67            .each_with_object({}) do |h, e|
68     if h.key? e
69       h[e] += 1
70     else
71       h[e] = 1
72     end
73   end
74
75   CALL_TYPES.keys.each do |type|
76     count = counts[type] || 0
77     puts "#{type}.value #{count / delta_t}"
78   end
79 end