Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / other / apache_request_rate @ f2710f48

Historique | Voir | Annoter | Télécharger (1,32 ko)

1
#!/usr/bin/env ruby
2
require 'rubygems'
3
require 'json'
4
require 'open-uri'
5

    
6
if ARGV.first == 'config'
7
  puts 'graph_category Apache'
8
  puts 'graph_title Requests per second'
9
  puts 'graph_vlabel reqs/sec'
10
  puts 'graph_scale no'
11
  puts 'requests_per_second.label Apache reqs/sec'
12
  exit 0
13
end
14

    
15
STATUS_URL = 'http://localhost/server-status?auto'
16
TMPFILE = '/tmp/apache-status-monitor'
17
SECONDS_BEFORE_STALE = 900
18

    
19
begin
20
  previous_sample = File.exists?(TMPFILE) ? JSON.parse(File.open(TMPFILE, 'r').read) : {}
21
  output = {}
22

    
23
  apache_status = open(STATUS_URL).read
24
  new_total_requests = apache_status.match(/Total Accesses: (\d+)$/)[1].to_i  #subtract one to account for request we just made!
25
  sample_duration = previous_sample['updated_at'] ? Time.now - Time.at(previous_sample['updated_at'].to_i) : nil
26

    
27
  if sample_duration && sample_duration < SECONDS_BEFORE_STALE
28
      output['requests_per_second'] = "%0.2f" % ((new_total_requests - 1 - previous_sample['total_requests'].to_i) / sample_duration.to_f)
29
  else
30
      output = {}
31
  end
32

    
33
  # Write back to tmpfile
34
  new_tmp_data = {'total_requests' => new_total_requests, 'updated_at' => Time.now.to_i}
35
  File.open(TMPFILE, 'w') { |f| f.puts(new_tmp_data.to_json)}
36

    
37
  puts "requests_per_second.value #{output['requests_per_second']}"
38
rescue Exception => e
39
  puts "Exception: #{e.message}"
40
  exit -1
41
end