Class: MuninManager::Plugins::HaproxyResponseTime

Inherits:
LogReader
  • Object
show all
Includes:
ActsAsMuninPlugin
Defined in:
lib/munin_manager/plugins/haproxy_response_time.rb

Instance Attribute Summary

Attributes inherited from LogReader

#file_name, #me, #state_dir, #state_file

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActsAsMuninPlugin

included

Methods inherited from LogReader

#collect!, #initialize, #load_saved_state, #save_state

Constructor Details

This class inherits a constructor from MuninManager::LogReader

Class Method Details

.help_textObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/munin_manager/plugins/haproxy_response_time.rb', line 63

def self.help_text
  %Q{
#{plugin_name.capitalize} Munin Plugin
===========================

Please remember to add something like the lines below to /etc/munin/plugin-conf.d/munin-node
if the haproxy log file is not at /var/log/haproxy.log

[#{plugin_name}]
env.log_file /var/log/custom/haproxy.log

Also, make sure that the '/var/lib/munin/plugin-state' is writable by munin.

$ sudo chmod 777 /var/lib/munin/plugin-state

}
end

.runObject



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/munin_manager/plugins/haproxy_response_time.rb', line 49

def self.run
  log_file = ENV['log_file'] || "/var/log/haproxy.log"
  allowed_commands = ['config']

  haproxy = new(log_file)

  if cmd = ARGV[0] and allowed_commands.include? cmd then
    puts haproxy.send(cmd.to_sym)
  else
    haproxy.collect!
    puts haproxy.values
  end
end

Instance Method Details

#configObject



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/munin_manager/plugins/haproxy_response_time.rb', line 31

def config
  <<-LABEL                     
graph_title HAProxy Response Breakdown
graph_vlabel time (secs)
graph_category Haproxy                
client_connect.label Client Connect
waiting_in_queue.label  Waiting In Queue
server_connect.label  Server Connect
server_response.label  Server Response
rails_action.label  Rails Controller Action
total.label Total Response Time
  LABEL
end

#dataObject



5
6
7
# File 'lib/munin_manager/plugins/haproxy_response_time.rb', line 5

def data
  @data ||= Hash.new {|h, k| h[k] = Array.new}
end

#process!Object



24
25
26
27
28
29
# File 'lib/munin_manager/plugins/haproxy_response_time.rb', line 24

def process!
  data.each do |k, v|
    data[k] = data[k].inject(0) {|sum, i| sum + i} / data[k].length rescue 0
    data[k] = formatted(data[k] / 1000)
  end
end

#scan(log_file) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/munin_manager/plugins/haproxy_response_time.rb', line 9

def scan(log_file)
  loop do
    line = log_file.readline
    chunks = line.split(/\s+/)

    timers = chunks[9].split("/") rescue []
    data[:client_connect] << timers[0].to_f
    data[:waiting_in_queue] << timers[1].to_f
    data[:server_connect] << timers[2].to_f
    data[:server_response] << timers[3].to_f
    data[:rails_action] << line.match(/\{([0-9.]+)\}/).captures[0].to_f rescue 0
    data[:total] << timers[4].to_f
  end
end

#valuesObject



45
46
47
# File 'lib/munin_manager/plugins/haproxy_response_time.rb', line 45

def values
  data.map {|k, v| "#{k}.value #{v}"}.join("\n")
end