-
Notifications
You must be signed in to change notification settings - Fork 2
/
apache.rb
56 lines (47 loc) · 1.42 KB
/
apache.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
require 'open-uri'
module Dash::Sensor::Plugins
class Apache
include SensorPlugin
ONE_KB = 1024
register :apache, :url => 'http://dash.fiveruns.com' do |recipe|
recipe.counter :requests, 'Requests' do
total = Integer(stats['Total Accesses'])
last = Integer(old_stats['Total Accesses'])
this_minute = total - last
this_minute > 0 && last > 0 ? this_minute : 0
end
recipe.counter :mbytes, 'Transferred', :unit => 'MB' do
total = Integer(stats['Total kBytes'])
last = Integer(old_stats['Total kBytes'])
this_minute = Float(total - last) / ONE_KB
this_minute > 0 && last > 0 ? this_minute : 0
end
end
def configure(options)
@url = options.fetch(:url, 'http://localhost/server-status?auto')
end
private
def self.stats
if !@time || @time < Time.now - 55
@old_stats = @stats || Hash.new(0)
logger.debug "Fetching status at #{Time.now}"
@stats = instance.send(:stats_data)
@time = Time.now
end
@stats
end
def self.old_stats
@old_stats
end
def stats_data
begin
lines = open(@url).read.split("\n")
Hash[*lines.map {|line| line.split(':') }.flatten]
rescue => e
logger.error "Error contacting #{@url}"
logger.error "#{e.class.name}: #{e.message}"
Hash.new(0)
end
end
end
end