3 require "apache_log_regex"
7 def categorise_uri(line)
8 uri = line.split(" ")[1]
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
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')
26 :status => Hash.new(0),
28 :count => Hash.new(0),
29 :bytes => Hash.new(0),
30 :seconds => Hash.new(0.0),
34 File::Tail::Logfile.tail("/var/log/apache2/access.log") do |line|
36 hash = parser.parse(line)
38 uri = categorise_uri(hash["%r"])
40 bytes = hash["%O"].to_i
41 seconds = hash["%Dus"].to_f / 1000000
42 protocol = hash["%{SSL_PROTOCOL}x"]
43 cipher = hash["%{SSL_CIPHER}x"]
45 statistics[:status][status] += 1
46 statistics[:uri][uri] += 1
47 statistics[:count][[uri, status]] += 1
48 statistics[:bytes][[uri, status]] += bytes
49 statistics[:seconds][[uri, status]] += seconds
50 statistics[:ssl][[protocol, cipher]] += 1 unless protocol == "-"
51 rescue ApacheLogRegex::ParseError
55 if Time.now - last_write > 10
56 File.write("/srv/www.openstreetmap.org/rails/tmp/statistics.json", statistics.slice(:status, :uri).to_json)
58 File.open("/var/lib/prometheus/node-exporter/api.tmp", "w") do |file|
59 file.puts "# HELP api_call_count_total Number of calls by type and status"
60 file.puts "# TYPE api_call_count_total counter"
62 statistics[:count].each do |key, value|
65 file.puts "api_call_count_total{uri=\"#{uri}\",status=\"#{status}\"} #{value}"
68 file.puts "# HELP api_call_bytes_total Number of bytes returned by type and status"
69 file.puts "# TYPE api_call_bytes_total counter"
71 statistics[:bytes].each do |key, value|
74 file.puts "api_call_bytes_total{uri=\"#{uri}\",status=\"#{status}\"} #{value}"
77 file.puts "# HELP api_call_seconds_total Number of seconds returned by type and status"
78 file.puts "# TYPE api_call_seconds_total counter"
80 statistics[:seconds].each do |key, value|
83 file.puts "api_call_seconds_total{uri=\"#{uri}\",status=\"#{status}\"} #{value}"
86 file.puts "# HELP api_call_ssl_total Number of calls by SSL protocol and cipher"
87 file.puts "# TYPE api_call_ssl_total counter"
89 statistics[:ssl].each do |key, value|
90 protocol, cipher = key
92 file.puts "api_call_ssl_total{protocol=\"#{protocol}\",cipher=\"#{cipher}\"} #{value}"
96 File.rename("/var/lib/prometheus/node-exporter/api.tmp", "/var/lib/prometheus/node-exporter/api.prom")