Class: MuninManager::Plugins::HaproxyAppResponseTime

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

Constant Summary collapse

EXTRACTORS =
{
  :client_connect => lambda {|line| line.split(/\s+/)[9].split("/")[0].to_f},
  :waiting_in_queue => lambda {|line| line.split(/\s+/)[9].split("/")[1].to_f},
  :server_connect => lambda {|line| line.split(/\s+/)[9].split("/")[2].to_f},
  :server_response => lambda {|line| line.split(/\s+/)[9].split("/")[3].to_f},
  :rails_action => lambda {|line| (line.match(/\{([0-9.]+)\}/).captures[0].to_f * 1000) rescue 0},
  :total => lambda {|line| line.split(/\s+/)[9].split("/")[4].to_f},
}

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!, #load_saved_state, #save_state

Constructor Details

#initialize(logfile, options) ⇒ HaproxyAppResponseTime

Returns a new instance of HaproxyAppResponseTime.

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
# File 'lib/munin_manager/plugins/haproxy_app_response_time.rb', line 14

def initialize(logfile, options)
  @measure = options[:measure].to_sym
  raise ArgumentError, 
    "I do not know how to measure `%s`" % options[:measure] unless EXTRACTORS.key?(@measure)
    
  super(logfile)
end

Class Method Details

.help_text(options = {}) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/munin_manager/plugins/haproxy_app_response_time.rb', line 85

def self.help_text(options = {})
  %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

[#{options[:symlink]}]
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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/munin_manager/plugins/haproxy_app_response_time.rb', line 66

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

  measure = ENV['measure']
  # Try to figure out what we're trying to measure from the symlink name
  measure ||= File.basename($0).split(".", 2).last
  measure = nil unless EXTRACTORS.key?(measure.to_sym)
  
  haproxy = new(log_file, :measure => measure || 'total')

  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



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

def config
  collect!(:save_state => false)
              
  config_text = <<-LABEL                     
graph_title HAProxy App Server #{@measure}
graph_vlabel time (secs)
graph_category Haproxy
  LABEL
  
  data.keys.sort.each do |server|
    config_text << "#{server}_#{@measure}.label #{server}_#{@measure}\n"
  end
  config_text      
end

#dataObject



22
23
24
# File 'lib/munin_manager/plugins/haproxy_app_response_time.rb', line 22

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

#process!Object



38
39
40
41
42
43
44
45
# File 'lib/munin_manager/plugins/haproxy_app_response_time.rb', line 38

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

#scan(log_file) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/munin_manager/plugins/haproxy_app_response_time.rb', line 26

def scan(log_file)
  loop do
    line = log_file.readline
    chunks = line.split(/\s+/)
    server, port = chunks[8].split(":") rescue []
    server_name = server.split("/")[1] rescue nil
    next if server_name.nil?
    
    data[server_name][@measure] << EXTRACTORS[@measure].call(line)
  end
end

#valuesObject



62
63
64
# File 'lib/munin_manager/plugins/haproxy_app_response_time.rb', line 62

def values
  data.inject([]){|datas, (server, values)| (datas + values.map{|k,v| "#{server}_#{k}.value #{v}"})}.join("\n")
end