Class: MuninManager::LogReader

Inherits:
Object
  • Object
show all
Defined in:
lib/munin_manager/log_reader.rb

Overview

A LogReader that continues from where it left off. Ex: class RailsLogReader < LogReader

  def scan(log_file)
    @req_counter = 0
    loop do
      line = log_file.readline
      @req_counter += 1 if line =~ /Completed in/
    end
  end

  def process!
    # Do nothing
  end

  def print_data
    "num_requests.value #{@req_counter}"
  end
end

Usage:

reader = RailsLogReader.new("log/development.log")
reader.collect!
reader.print_data

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name) ⇒ LogReader

Returns a new instance of LogReader.



31
32
33
34
35
# File 'lib/munin_manager/log_reader.rb', line 31

def initialize(file_name)
  @file_name = file_name
  @me = File.basename($0)
  @state_dir = ENV['MUNIN_PLUGSTATE'] || '/var/lib/munin/plugin-state'
end

Instance Attribute Details

#file_nameObject

Returns the value of attribute file_name.



29
30
31
# File 'lib/munin_manager/log_reader.rb', line 29

def file_name
  @file_name
end

#meObject

Returns the value of attribute me.



29
30
31
# File 'lib/munin_manager/log_reader.rb', line 29

def me
  @me
end

#state_dirObject

Returns the value of attribute state_dir.



29
30
31
# File 'lib/munin_manager/log_reader.rb', line 29

def state_dir
  @state_dir
end

#state_fileObject

Returns the value of attribute state_file.



29
30
31
# File 'lib/munin_manager/log_reader.rb', line 29

def state_file
  @state_file
end

Instance Method Details

#collect!(options = {}) ⇒ Object



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

def collect!(options = {})
  options = {
    :save_state => true
  }.merge(options)

  File.open(@file_name, "r") do |f|
    load_saved_state(f)
    
    begin
      scan(f)
    rescue EOFError
    end
    
    process!
    save_state(f) if options[:save_state]
  end
end

#load_saved_state(log_file) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/munin_manager/log_reader.rb', line 59

def load_saved_state(log_file)
  return unless File.exists?(state_file) && !(state = File.read(state_file)).nil?
  pos, last_file_size = Marshal.load(state)

  # Check for log rotation
  return if File.size(@file_name) < last_file_size

  log_file.pos = pos
end

#process!Object



74
75
76
# File 'lib/munin_manager/log_reader.rb', line 74

def process!
  raise "Needs to be implemented by subclasses"
end

#save_state(log_file) ⇒ Object



78
79
80
81
82
83
# File 'lib/munin_manager/log_reader.rb', line 78

def save_state(log_file)
  File.open(state_file, "w") do |f|
    f.write(Marshal.dump([log_file.pos, File.size(log_file)]))
    f.flush
  end
end

#scan(log_file) ⇒ Object



69
70
71
72
# File 'lib/munin_manager/log_reader.rb', line 69

def scan(log_file)
  # Only subclasses know how to process each type of logfile
  raise "Needs to be implemented by subclasses"
end