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

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



47
48
49
50
51
52
53
# File 'lib/logtrend.rb', line 47

def initialize(options={})
  set_defaults

  options.each do |key, val|
    send("#{key}=", val)
  end
end

Instance Attribute Details

#graphs_dirObject

This sets the directory where graphs should be stored.



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

def graphs_dir
  @graphs_dir
end

#heartbeatObject

Defines the amount of time between updates that will mark a value unknown.

From the rrdcreate(2) manual page:

heartbeat defines the maximum number of seconds that may pass between two updates of this data source before the value of the data source is assumed to be *UNKNOWN*.


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

def heartbeat
  @heartbeat
end

#loggerObject

This sets the logger to use. Must be something that behaves like a Logger object.



20
21
22
# File 'lib/logtrend.rb', line 20

def logger
  @logger
end

#rrd_dirObject

This sets the directory where your RRD files will rest.



17
18
19
# File 'lib/logtrend.rb', line 17

def rrd_dir
  @rrd_dir
end

#stepObject

Defines the amount of time between each updates, given in seconds. Default value is 60 seconds.

From the rrdcreate(2) manual page:

Specifies the base interval in seconds with which data will be fed into the RRD.


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

def step
  @step
end

#templateObject

This sets the HTML file template for the generated index.html file. The String here will pass through ERB, with self being set as the binding.

The default generates a simple HTML file that loads one IMG tag per graph.



26
27
28
# File 'lib/logtrend.rb', line 26

def template
  @template
end

Class Method Details

.run(logfile, options = {}) {|l| ... } ⇒ Object

This is the preferred entry point.

Yields:

  • (l)


68
69
70
71
72
73
# File 'lib/logtrend.rb', line 68

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

Instance Method Details

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

Yields:

  • (graph)

Raises:

  • (ArgumentError)


60
61
62
63
64
65
# File 'lib/logtrend.rb', line 60

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

#add_trend(name, &block) ⇒ Object

Raises:

  • (ArgumentError)


55
56
57
58
# File 'lib/logtrend.rb', line 55

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

#run(logfile) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/logtrend.rb', line 75

def run(logfile)
  counters = reset_counters

  EventMachine.run do
    EventMachine::add_periodic_timer(step) 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