logtrend 0.6.20101204165035 → 0.7.20101204170528

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/lib/logtrend.rb +66 -69
  2. data/samples/example.rb +4 -1
  3. metadata +4 -4
data/lib/logtrend.rb CHANGED
@@ -4,56 +4,55 @@ require 'eventmachine-tail'
4
4
  require 'rrd'
5
5
  require 'logger'
6
6
 
7
- class LogTrend
8
- attr_accessor :graphs
9
- attr_accessor :graphs_dir, :rrd_dir
7
+ module LogTrend
8
+ class Base
9
+ attr_accessor :graphs_dir, :rrd_dir
10
10
 
11
- def initialize
12
- @graphs_dir = '.'
13
- @rrd_dir = '.'
14
- @trends = {}
15
- @graphs = []
16
- @logger = Logger.new(STDERR)
17
- @logger.level = ($DEBUG and Logger::DEBUG or Logger::WARN)
18
- end
11
+ def initialize
12
+ @graphs_dir = '.'
13
+ @rrd_dir = '.'
14
+ @trends = {}
15
+ @graphs = []
16
+ @logger = Logger.new(STDERR)
17
+ @logger.level = ($DEBUG and Logger::DEBUG or Logger::WARN)
18
+ end
19
19
 
20
- def reset_counters
21
- counters = {}
22
- @trends.keys.each do |k|
23
- counters[k] = 0
20
+ def reset_counters
21
+ counters = {}
22
+ @trends.keys.each do |k|
23
+ counters[k] = 0
24
+ end
25
+ counters
24
26
  end
25
- counters
26
- end
27
27
 
28
- def update_rrd(name, value)
29
- file_name = File.join(@rrd_dir,"#{name}.rrd")
30
- rrd = RRD::Base.new(file_name)
31
- if !File.exists?(file_name)
32
- rrd.create :start => Time.now - 10.seconds, :step => 1.minutes do
33
- datasource "#{name}_count", :type => :gauge, :heartbeat => 5.minutes, :min => 0, :max => :unlimited
34
- archive :average, :every => 5.minutes, :during => 1.year
28
+ def update_rrd(name, value)
29
+ file_name = File.join(@rrd_dir,"#{name}.rrd")
30
+ rrd = RRD::Base.new(file_name)
31
+ if !File.exists?(file_name)
32
+ rrd.create :start => Time.now - 10.seconds, :step => 1.minutes do
33
+ datasource "#{name}_count", :type => :gauge, :heartbeat => 5.minutes, :min => 0, :max => :unlimited
34
+ archive :average, :every => 5.minutes, :during => 1.year
35
+ end
35
36
  end
37
+ rrd.update Time.now, value
36
38
  end
37
- rrd.update Time.now, value
38
- end
39
39
 
40
- def build_graph(graph)
41
- rrd_dir = @rrd_dir
42
- RRD.graph File.join(@graphs_dir,"#{graph.name}.png"), :title => graph.name, :width => 800, :height => 250, :color => ["FONT#000000", "BACK#FFFFFF"] do
43
- graph.points.each do |point|
44
- if point.style == :line
45
- line File.join(rrd_dir,"#{point.name}.rrd"), "#{point.name}_count" => :average, :color => point.color, :label => point.name.to_s
46
- elsif point.style == :area
47
- area File.join(rrd_dir,"#{point.name}.rrd"), "#{point.name}_count" => :average, :color => point.color, :label => point.name.to_s
40
+ def build_graph(graph)
41
+ rrd_dir = @rrd_dir
42
+ RRD.graph File.join(@graphs_dir,"#{graph.name}.png"), :title => graph.name, :width => 800, :height => 250, :color => ["FONT#000000", "BACK#FFFFFF"] do
43
+ graph.points.each do |point|
44
+ if point.style == :line
45
+ line File.join(rrd_dir,"#{point.name}.rrd"), "#{point.name}_count" => :average, :color => point.color, :label => point.name.to_s
46
+ elsif point.style == :area
47
+ area File.join(rrd_dir,"#{point.name}.rrd"), "#{point.name}_count" => :average, :color => point.color, :label => point.name.to_s
48
+ end
48
49
  end
49
50
  end
50
51
  end
51
- end
52
52
 
53
- def start(logfile)
54
- begin
53
+ def run(logfile)
55
54
  counters = reset_counters
56
-
55
+
57
56
  EventMachine.run do
58
57
  EventMachine::add_periodic_timer(1.minute) do
59
58
  @logger.debug "#{Time.now} #{counters.inspect}"
@@ -61,7 +60,7 @@ class LogTrend
61
60
  @graphs.each {|graph| build_graph(graph)}
62
61
  counters = reset_counters
63
62
  end
64
-
63
+
65
64
  EventMachine::file_tail(logfile) do |filetail, line|
66
65
  @trends.each do |name, block|
67
66
  counters[name] += 1 if block.call(line)
@@ -69,43 +68,41 @@ class LogTrend
69
68
  @logger.debug counters.inspect
70
69
  end
71
70
  end
72
- rescue Interrupt
73
- # hit ctrl-c
74
- end
75
- end
71
+ end
76
72
 
77
- def add_trend(name, &block)
78
- throw "D'oh! No block." unless block_given?
79
- @trends[name] = block
80
- end
73
+ def add_trend(name, &block)
74
+ throw "D'oh! No block." unless block_given?
75
+ @trends[name] = block
76
+ end
81
77
 
82
- def add_graph(name, &block)
83
- throw "D'oh! No block." unless block_given?
84
- graph = Graph.new(name)
85
- yield graph
86
- @graphs << graph
87
- end
78
+ def add_graph(name, &block)
79
+ throw "D'oh! No block." unless block_given?
80
+ graph = Graph.new(name)
81
+ yield graph
82
+ @graphs << graph
83
+ end
88
84
 
89
- def self.start(logfile, &block)
90
- l = LogTrend.new
91
- yield l if block
92
- l.start logfile
85
+ def self.run(logfile, &block)
86
+ l = Base.new
87
+ yield l if block
88
+ l.run logfile
89
+ end
93
90
  end
94
- end
95
91
 
96
- class Graph
92
+ class Graph
97
93
 
98
- attr_reader :points
99
- attr_reader :name
94
+ attr_reader :points
95
+ attr_reader :name
100
96
 
101
- def initialize(name)
102
- @name = name
103
- @points = []
104
- end
97
+ def initialize(name)
98
+ @name = name
99
+ @points = []
100
+ end
105
101
 
106
- def add_point(style,name,color)
107
- @points << GraphPoint.new(style, name, color)
102
+ def add_point(style,name,color)
103
+ @points << GraphPoint.new(style, name, color)
104
+ end
108
105
  end
109
- end
110
106
 
111
- GraphPoint = Struct.new(:style, :name, :color)
107
+ GraphPoint = Struct.new(:style, :name, :color)
108
+ end
data/samples/example.rb CHANGED
@@ -8,15 +8,18 @@ FileUtils.touch('test.log')
8
8
  Dir.mkdir('/tmp/rrd') if !File.exists?('/tmp/rrd')
9
9
  Dir.mkdir('/tmp/graphs') if !File.exists?('/tmp/graphs')
10
10
 
11
- LogTrend.start("test.log") do |l|
11
+ LogTrend::Base.run("test.log") do |l|
12
12
 
13
+ # set new locations for our graphs and rrds. defaults to '.'
13
14
  l.rrd_dir = '/tmp/rrd'
14
15
  l.graphs_dir = '/tmp/graphs'
15
16
 
17
+ # add some things to trend. An RRD is built for each one of these items.
16
18
  l.add_trend(:total) {|line| line.match /.*/}
17
19
  l.add_trend(:fbod) {|line| line.match /fogbugz.com/}
18
20
  l.add_trend(:kod) {|line| line.match /kilnhg.com/}
19
21
 
22
+ # build a graph showing requests per minute
20
23
  l.add_graph(:requests_per_minute) do |g|
21
24
  g.add_point :area, :total, "#333333"
22
25
  g.add_point :line, :fbod, "#0066cc"
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logtrend
3
3
  version: !ruby/object:Gem::Version
4
- hash: 40202408330065
4
+ hash: 40202408341059
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 6
9
- - 20101204165035
10
- version: 0.6.20101204165035
8
+ - 7
9
+ - 20101204170528
10
+ version: 0.7.20101204170528
11
11
  platform: ruby
12
12
  authors:
13
13
  - Michael Gorsuch