logtrend 0.1.20101203211234

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/logtrend.rb +74 -0
  2. data/samples/example.rb +22 -0
  3. metadata +96 -0
data/lib/logtrend.rb ADDED
@@ -0,0 +1,74 @@
1
+ require 'rubygems'
2
+ require 'eventmachine'
3
+ require 'eventmachine-tail'
4
+ require 'rrd'
5
+ require 'logger'
6
+
7
+ class LogTrend
8
+ attr_accessor :trends
9
+ attr_accessor :graphs
10
+
11
+ def initialize
12
+ trends = {}
13
+ graphs = {}
14
+ @logger = Logger.new(STDERR)
15
+ @logger.level = ($DEBUG and Logger::DEBUG or Logger::WARN)
16
+ end
17
+
18
+ def reset_counters
19
+ counters = {}
20
+ trends.keys.each do |k|
21
+ counters[k] = 0
22
+ end
23
+ counters
24
+ end
25
+
26
+ def update_rrd(name, value)
27
+ file_name = "#{name}.rrd"
28
+ rrd = RRD::Base.new(file_name)
29
+ if !File.exists?(file_name)
30
+ rrd.create :start => Time.now - 10.seconds, :step => 1.minutes do
31
+ datasource "#{name}_count", :type => :gauge, :heartbeat => 5.minutes, :min => 0, :max => :unlimited
32
+ archive :average, :every => 5.minutes, :during => 1.year
33
+ end
34
+ end
35
+ rrd.update Time.now, value
36
+ end
37
+
38
+ def build_graph(name, data)
39
+ RRD.graph "#{name}.png", :title => name, :width => 800, :height => 250, :color => ["FONT#000000", "BACK#FFFFFF"] do
40
+ data.each do |name, color|
41
+ area "#{name}.rrd", "#{name}_count" => :average, :color => color, :label => name.to_s
42
+ end
43
+ end
44
+ end
45
+
46
+ def start(logfile)
47
+ begin
48
+ counters = reset_counters
49
+
50
+ EventMachine.run do
51
+ EventMachine::add_periodic_timer(1.minute) do
52
+ @logger.debug "#{Time.now} #{counters.inspect}"
53
+ counters.each {|name, value| update_rrd(name, value)}
54
+ graphs.each {|name, data| build_graph(name, data)}
55
+ counters = reset_counters
56
+ end
57
+
58
+ EventMachine::file_tail(logfile) do |filetail, line|
59
+ trends.each do |name, regex|
60
+ counters[name] += 1 if line.match(regex)
61
+ end
62
+ end
63
+ end
64
+ rescue Interrupt
65
+ # hit ctrl-c
66
+ end
67
+ end
68
+
69
+ def self.start(logfile, &block)
70
+ l = LogTrend.new
71
+ yield l if block
72
+ l.start(logfile)
73
+ end
74
+ end
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift "#{File.dirname(__FILE__)}/../lib"
4
+ require 'logtrend'
5
+ require 'fileutils'
6
+
7
+ FileUtils.touch('test.log')
8
+
9
+ LogTrend.start('test.log') do |l|
10
+ l.trends = {
11
+ :total => /.*/,
12
+ :fbod => /fogbugz.com/,
13
+ :kod => /kilnhg.com/
14
+ }
15
+
16
+ l.graphs = {
17
+ "requests_per_minute" => {
18
+ :total => '#0066cc',
19
+ :kod => '#993333'
20
+ }
21
+ }
22
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logtrend
3
+ version: !ruby/object:Gem::Version
4
+ hash: 40202406422495
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 20101203211234
10
+ version: 0.1.20101203211234
11
+ platform: ruby
12
+ authors:
13
+ - Michael Gorsuch
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-12-03 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: eventmachine
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: eventmachine-tail
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :runtime
48
+ version_requirements: *id002
49
+ description: logtrend is an HTTP log parser built on top of event machine, generating rrd graphs of usage matching patterns you define.
50
+ email: michael.gorsuch@gmail.com
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files: []
56
+
57
+ files:
58
+ - lib/logtrend.rb
59
+ - samples/example.rb
60
+ has_rdoc: true
61
+ homepage: https://github.com/gorsuch/logtrend
62
+ licenses: []
63
+
64
+ post_install_message:
65
+ rdoc_options: []
66
+
67
+ require_paths:
68
+ - lib
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ hash: 3
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ requirements: []
89
+
90
+ rubyforge_project:
91
+ rubygems_version: 1.3.7
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: logtrend - an event-driven http log parser that generates rrd graphs
95
+ test_files: []
96
+