]> git.openstreetmap.org Git - chef.git/blob - cookbooks/munin/files/default/plugins/api_waits_
Fix more rubocop detected style issues
[chef.git] / cookbooks / munin / files / default / plugins / api_waits_
1 #!/usr/bin/ruby
2
3 require 'rubygems'
4 require 'date'
5 require 'hpricot'
6 require 'open-uri'
7
8 def uri_and_times_from_status(server)
9   file = open("http://#{server}/server-status").read
10   doc = Hpricot.parse(file)
11   tables = doc / 'table'
12   rows = (tables[0] / 'tr')[1..-1]
13   data = rows.collect { |r| (r  / 'td').collect(&:inner_html) }
14   # filter where the PID is numeric, status is 'W' and host matches the server
15   matching_data = data.select { |r| (r[1].to_i > 0) && r[3].match(/W/) && r[11].match(server) }
16   # return URI and number of seconds processing for each request
17   matching_data.collect { |r| [r[12], r[5].to_i] }
18 end
19
20 CALL_TYPES = {
21   :map => "Map API calls",
22   :upload => "Changeset diff uploads",
23   :amf => "AMF API calls",
24   :history => "Element history fetches",
25   :full => "Full element fetches",
26   :trkpts => "GPX trackpoints calls",
27   :web => "Web site traffic",
28   :other => "Other API calls"
29 }
30
31 def categorise_uri(line)
32   uri = line.split(" ")[1]
33
34   case uri
35   when /api\/0\.6\/map/ then :map
36   when /api\/0\.6\/changeset\/[0-9]*\/upload/ then :upload
37   when /api\/0\.6\/amf/ then :amf
38   when /api\/0\.6\/(node|way|relation)\/[0-9]*\/history/ then :history
39   when /api\/0\.6\/(node|way|relation)\/[0-9]*\/full/ then :full
40   when /api\/0\.6\/trackpoints/ then :trkpts
41   when /api\/0\.6\// then :other
42   else :web
43   end
44 end
45
46 server = $0.match("api_waits_(.*)")[1]
47
48 if ARGV[0] == 'config'
49   puts "graph_title Wait times for active requests"
50   puts "graph_vlabel Average time of requests"
51   puts "graph_category api"
52   CALL_TYPES.each { |k, v| puts "#{k}.label #{v}" }
53
54 else
55   counts = uri_and_times_from_status(server)
56            .collect { |x, y| [categorise_uri(x), y] }
57            .each_with_object(Hash.new) do |h, e|
58     category, time = e
59     if h.key? category
60       h[category] += [time]
61     else
62       h[category] = [time]
63     end
64   end
65
66   CALL_TYPES.keys.each do |type|
67     count = counts[type] || [0]
68     avg = count.inject(0) { |a, e| a + e } / (1.0 * count.length)
69     puts "#{type}.value #{avg}"
70   end
71 end