Class: MuninManager::Plugins::RailsResponseTime

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

Instance Attribute Summary collapse

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(log_file, options = {}) ⇒ RailsResponseTime

Returns a new instance of RailsResponseTime.



6
7
8
9
10
# File 'lib/munin_manager/plugins/rails_response_time.rb', line 6

def initialize(log_file, options = {})
  super(log_file)
  @search_pattern = options[:search_pattern] || /.*/
  @search_pattern = Regexp.new(@search_pattern) if @search_pattern.is_a?(String)
end

Instance Attribute Details

#search_patternObject

Returns the value of attribute search_pattern.



4
5
6
# File 'lib/munin_manager/plugins/rails_response_time.rb', line 4

def search_pattern
  @search_pattern
end

Class Method Details

.help_text(options = {}) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/munin_manager/plugins/rails_response_time.rb', line 78

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 rails log file is not at /var/log/rails.log

[#{options[:symlink] || plugin_name}]
env.log_file /var/log/custom/rails.log
env.search_pattern UsersController.*

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

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

}
end

.runObject



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

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

  rails = new(log_file, :search_pattern => ENV['search_pattern'])

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

Instance Method Details

#configObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/munin_manager/plugins/rails_response_time.rb', line 40

def config
  configs = {
    "graph_title" => "Rails Response Times (by Controller->Action)",
    "graph_vlabel" => "time (secs)",
    "graph_category" => "Performance",
    "graph_args" => "--upper-limit 1.0 --lower-limit 0.100 --rigid"
  }
  self.collect!(:save_state => false)
  self.data.each do |action_name, respose_time|
    configs["#{format_for_munin(action_name)}.label"] = action_name.sub("#", "->")
    configs["#{format_for_munin(action_name)}.draw"] = "LINE2"
  end

  configs["graph_order"] = self.data.to_a.
    sort {|lhs, rhs| rhs[1] <=> lhs[1]}.
    collect {|tuple| format_for_munin(tuple[0])}

  configs.map {|key_value_pair| key_value_pair.join(" ")}.join("\n")
end

#dataObject



12
13
14
# File 'lib/munin_manager/plugins/rails_response_time.rb', line 12

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

#process!Object



34
35
36
37
38
# File 'lib/munin_manager/plugins/rails_response_time.rb', line 34

def process!
  data.each_pair do |action_name, response_times|
    data[action_name] = response_times.inject(0) {|sum, i| sum + i} / data[action_name].length rescue 0
  end
end

#scan(log_file) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/munin_manager/plugins/rails_response_time.rb', line 16

def scan(log_file)
  current_action = nil
  loop do
    line = log_file.readline

    if line.starts_with?("Processing ")
      cols = line.split(/\s+/)
      current_action = cols[1] if self.search_pattern.match(cols[1])

    elsif line.starts_with?("Completed in ") && !current_action.nil?
      cols = line.split(/\s+/)
      data[current_action] << cols[2].to_f
      current_action = nil
    end

  end
end

#valuesObject



60
61
62
# File 'lib/munin_manager/plugins/rails_response_time.rb', line 60

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