Class: Munin::RailsPlugin

Inherits:
RequestLogAnalyzerPlugin show all
Defined in:
lib/munin/rails_plugin.rb

Instance Attribute Summary collapse

Attributes inherited from RequestLogAnalyzerPlugin

#debug, #environment, #graph_category, #passenger_memory_stats, #passenger_status

Instance Method Summary collapse

Methods inherited from RequestLogAnalyzerPlugin

#autoconf, #initialize, #require_command, #require_gem, #require_passenger_gem, #require_passenger_memory_stats, #require_passenger_status, #require_request_log_analyzer_gem, #require_tail_command, #require_yaml_gem, #run_command

Constructor Details

This class inherits a constructor from Munin::RequestLogAnalyzerPlugin

Instance Attribute Details

#after_timeObject

Returns the value of attribute after_time.



8
9
10
# File 'lib/munin/rails_plugin.rb', line 8

def after_time
  @after_time
end

#floor_timeObject

Returns the value of attribute floor_time.



9
10
11
# File 'lib/munin/rails_plugin.rb', line 9

def floor_time
  @floor_time
end

#intervalObject

Returns the value of attribute interval.



4
5
6
# File 'lib/munin/rails_plugin.rb', line 4

def interval
  @interval
end

#log_fileObject

Returns the value of attribute log_file.



6
7
8
# File 'lib/munin/rails_plugin.rb', line 6

def log_file
  @log_file
end

#log_formatObject

Returns the value of attribute log_format.



7
8
9
# File 'lib/munin/rails_plugin.rb', line 7

def log_format
  @log_format
end

#number_of_linesObject

Returns the value of attribute number_of_lines.



5
6
7
# File 'lib/munin/rails_plugin.rb', line 5

def number_of_lines
  @number_of_lines
end

#request_log_analyzerObject

Returns the value of attribute request_log_analyzer.



13
14
15
# File 'lib/munin/rails_plugin.rb', line 13

def request_log_analyzer
  @request_log_analyzer
end

#temp_file_nameObject

Returns the value of attribute temp_file_name.



12
13
14
# File 'lib/munin/rails_plugin.rb', line 12

def temp_file_name
  @temp_file_name
end

#temp_folderObject

Returns the value of attribute temp_folder.



10
11
12
# File 'lib/munin/rails_plugin.rb', line 10

def temp_folder
  @temp_folder
end

#temp_prefixObject

Returns the value of attribute temp_prefix.



11
12
13
# File 'lib/munin/rails_plugin.rb', line 11

def temp_prefix
  @temp_prefix
end

Instance Method Details

#ensure_configurationObject



39
40
41
42
43
44
45
# File 'lib/munin/rails_plugin.rb', line 39

def ensure_configuration
  require_request_log_analyzer_gem
  require_yaml_gem
  require_tail_command

  super
end

#ensure_log_fileObject



47
48
49
50
51
52
# File 'lib/munin/rails_plugin.rb', line 47

def ensure_log_file
  if log_file == "" || log_file.nil?
    $stderr.puts "Filepath unspecified. Exiting"
    exit 1
  end      
end

#fetch_or_create_yaml_file(log_file, debug = false) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/munin/rails_plugin.rb', line 54

def fetch_or_create_yaml_file(log_file, debug = false)
  # Clean up any old temp files left in de temp folder
  Dir.new(temp_folder).entries.each do |file_name|
    if match = file_name.match(/^#{temp_prefix}_.*\.yml/)
      if match[0] != temp_file_name
        puts "Removing old cache file: #{file_name}" if debug
        File.delete(temp_folder + "/" + file_name)
      end
    end
  end

  temp_file = temp_folder + "/" + temp_file_name

  # Create temp file rla if needed
  unless File.exists?(temp_file)
    puts "Processing the last #{number_of_lines} lines of #{log_file} which are less then #{interval} seconds old." if debug
    status = `tail -n #{number_of_lines} #{log_file} | #{request_log_analyzer} - --after #{after_time} #{log_format} -b --dump #{temp_file}`

    unless $?.success?
      $stderr.puts "failed executing request-log-analyzer. Is the gem path correct?"
      exit 1
    end
  else
    puts "Processing cached YAML result #{temp_file}" if debug
  end

  return temp_file
end

#get_request_log_analyzer_fileObject



35
36
37
# File 'lib/munin/rails_plugin.rb', line 35

def get_request_log_analyzer_file
  fetch_or_create_yaml_file(log_file, debug)
end

#handle_arguments(args, environment) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/munin/rails_plugin.rb', line 15

def handle_arguments(args, environment)
  super
  
  self.interval        = environment['interval'] ? environment['interval'].to_i : 300
  self.number_of_lines = environment['lines'] || 50000
  self.log_file        = environment['log_file'] || args[0]
  self.log_format      = environment['log_format'] ? "--format #{environment['log_format']}" : ''
  self.after_time      = (Time.now - interval).strftime('%Y%m%d%H%M%S')
  self.floor_time      = Time.at((Time.now.to_f / interval).floor * interval)

  self.temp_folder     = '/tmp'
  self.temp_prefix     = graph_category == 'App' ? 'rla' : graph_category.downcase
  self.temp_file_name       = "#{temp_prefix}_#{floor_time.to_i}.yml"
  self.request_log_analyzer = environment['request_log_analyzer'] || '/usr/bin/request-log-analyzer'       
end

#parse_request_log_analyzer_dataObject



31
32
33
# File 'lib/munin/rails_plugin.rb', line 31

def parse_request_log_analyzer_data
  YAML::load_file( get_request_log_analyzer_file )
end