Class: LogTrend::Base

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/logtrend.rb', line 15

def initialize
  @graphs_dir = '.'
  @rrd_dir = '.'
  @trends = {}
  @graphs = []
  @logger = Logger.new(STDERR)
  @logger.level = ($DEBUG and Logger::DEBUG or Logger::WARN)
  
  @template = ERB.new <<-EOF
  <html>
    <head>
      <title>logtrend</title>
    </head>
    <body>
      <% @graphs.each do |graph| %>
        <img src='<%=graph.name%>.png' />
      <% end %>
    </body>
  </html>
  EOF
end

Instance Attribute Details

#graphs_dirObject

This sets the directory where graphs should be stored.



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

def graphs_dir
  @graphs_dir
end

#rrd_dirObject

This sets the directory where your RRD files will rest.



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

def rrd_dir
  @rrd_dir
end

Class Method Details

.run(logfile) {|l| ... } ⇒ Object

This is the preferred entry point.

Yields:

  • (l)


50
51
52
53
54
55
# File 'lib/logtrend.rb', line 50

def self.run(logfile, &block)
  throw "D'oh! No block." unless block_given?
  l = Base.new
  yield l
  l.run logfile
end

Instance Method Details

#add_graph(name) {|graph| ... } ⇒ Object

Yields:

  • (graph)


42
43
44
45
46
47
# File 'lib/logtrend.rb', line 42

def add_graph(name, &block)
  throw "D'oh! No block." unless block_given?
  graph = Graph.new(name)
  yield graph
  @graphs << graph
end

#add_trend(name, &block) ⇒ Object



37
38
39
40
# File 'lib/logtrend.rb', line 37

def add_trend(name, &block)
  throw "D'oh! No block." unless block_given?
  @trends[name] = block
end

#run(logfile) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/logtrend.rb', line 57

def run(logfile)
  counters = reset_counters

  EventMachine.run do       
    EventMachine::add_periodic_timer(1.minute) do
      @logger.debug "#{Time.now} #{counters.inspect}"
      counters.each {|name, value| update_rrd(name, value)}            
      @graphs.each {|graph| build_graph(graph)}
      build_page
      counters = reset_counters
    end
  
    EventMachine::file_tail(logfile) do |filetail, line|
      @trends.each do |name, block|
        counters[name] += 1 if block.call(line)
      end
      @logger.debug counters.inspect
    end
  end
end