logtrend 0.7.20101204171005 → 0.7.20101205083841
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/logtrend.rb +41 -39
- data/samples/example.rb +43 -14
- data/samples/test.log +1 -0
- metadata +4 -4
data/lib/logtrend.rb
CHANGED
@@ -16,7 +16,48 @@ module LogTrend
|
|
16
16
|
@logger = Logger.new(STDERR)
|
17
17
|
@logger.level = ($DEBUG and Logger::DEBUG or Logger::WARN)
|
18
18
|
end
|
19
|
+
|
20
|
+
def add_trend(name, &block)
|
21
|
+
throw "D'oh! No block." unless block_given?
|
22
|
+
@trends[name] = block
|
23
|
+
end
|
24
|
+
|
25
|
+
def add_graph(name, &block)
|
26
|
+
throw "D'oh! No block." unless block_given?
|
27
|
+
graph = Graph.new(name)
|
28
|
+
yield graph
|
29
|
+
@graphs << graph
|
30
|
+
end
|
19
31
|
|
32
|
+
def self.run(logfile, &block)
|
33
|
+
throw "D'oh! No block." unless block_given?
|
34
|
+
l = Base.new
|
35
|
+
yield l
|
36
|
+
l.run logfile
|
37
|
+
end
|
38
|
+
|
39
|
+
def run(logfile)
|
40
|
+
counters = reset_counters
|
41
|
+
|
42
|
+
EventMachine.run do
|
43
|
+
EventMachine::add_periodic_timer(1.minute) do
|
44
|
+
@logger.debug "#{Time.now} #{counters.inspect}"
|
45
|
+
counters.each {|name, value| update_rrd(name, value)}
|
46
|
+
@graphs.each {|graph| build_graph(graph)}
|
47
|
+
counters = reset_counters
|
48
|
+
end
|
49
|
+
|
50
|
+
EventMachine::file_tail(logfile) do |filetail, line|
|
51
|
+
@trends.each do |name, block|
|
52
|
+
counters[name] += 1 if block.call(line)
|
53
|
+
end
|
54
|
+
@logger.debug counters.inspect
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
20
61
|
def reset_counters
|
21
62
|
counters = {}
|
22
63
|
@trends.keys.each do |k|
|
@@ -49,45 +90,6 @@ module LogTrend
|
|
49
90
|
end
|
50
91
|
end
|
51
92
|
end
|
52
|
-
|
53
|
-
def run(logfile)
|
54
|
-
counters = reset_counters
|
55
|
-
|
56
|
-
EventMachine.run do
|
57
|
-
EventMachine::add_periodic_timer(1.minute) do
|
58
|
-
@logger.debug "#{Time.now} #{counters.inspect}"
|
59
|
-
counters.each {|name, value| update_rrd(name, value)}
|
60
|
-
@graphs.each {|graph| build_graph(graph)}
|
61
|
-
counters = reset_counters
|
62
|
-
end
|
63
|
-
|
64
|
-
EventMachine::file_tail(logfile) do |filetail, line|
|
65
|
-
@trends.each do |name, block|
|
66
|
-
counters[name] += 1 if block.call(line)
|
67
|
-
end
|
68
|
-
@logger.debug counters.inspect
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
def add_trend(name, &block)
|
74
|
-
throw "D'oh! No block." unless block_given?
|
75
|
-
@trends[name] = block
|
76
|
-
end
|
77
|
-
|
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
|
84
|
-
|
85
|
-
def self.run(logfile, &block)
|
86
|
-
throw "D'oh! No block." unless block_given?
|
87
|
-
l = Base.new
|
88
|
-
yield l
|
89
|
-
l.run logfile
|
90
|
-
end
|
91
93
|
end
|
92
94
|
|
93
95
|
class Graph
|
data/samples/example.rb
CHANGED
@@ -1,29 +1,58 @@
|
|
1
|
-
|
1
|
+
# The story: you have all sorts of logs,
|
2
|
+
# and you want to get an idea how often certain
|
3
|
+
# events occur each minute
|
4
|
+
|
5
|
+
# Perhaps you just want to know how many HTTP transactions occurred.
|
6
|
+
# Maybe you are worried about unauthorized access attempts.
|
7
|
+
# How about how many customers are flogging your api endpoint?
|
8
|
+
|
9
|
+
# You need to see the outliers. Grepping through log files
|
10
|
+
# in the midst of a crisis is very hard to do and quite unproductive.
|
11
|
+
# This is especially true in environments with lots of transactions.
|
12
|
+
|
13
|
+
# With this tool, you can begin categorizing events
|
14
|
+
# in your logs and trend them. Then, when crisis hits, you can
|
15
|
+
# take a look at the graphs to see if anything suspicious has occurred
|
16
|
+
# over the last few minutes.
|
2
17
|
|
3
18
|
$:.unshift "#{File.dirname(__FILE__)}/../lib"
|
4
19
|
require 'logtrend'
|
5
20
|
require 'fileutils'
|
6
21
|
|
7
22
|
FileUtils.touch('test.log')
|
8
|
-
Dir.mkdir('/tmp/rrd') if !File.exists?('/tmp/rrd')
|
9
|
-
Dir.mkdir('/tmp/graphs') if !File.exists?('/tmp/graphs')
|
10
23
|
|
11
|
-
|
24
|
+
# Invoke this to begin trending your data...
|
25
|
+
LogTrend::Base.run("test.log") do |lt|
|
12
26
|
|
13
|
-
#
|
14
|
-
|
15
|
-
|
27
|
+
# Set new locations for our graphs and rrds. defaults to '.'
|
28
|
+
lt.rrd_dir = '/tmp/rrd'
|
29
|
+
lt.graphs_dir = '/tmp/graphs'
|
16
30
|
|
17
|
-
#
|
18
|
-
|
19
|
-
|
20
|
-
|
31
|
+
# Add some things to trend. An RRD is built for each one of these items.
|
32
|
+
# Each time we read a line from the log file, we pass it to the block.
|
33
|
+
# If your block returns true, we count that as a hit.
|
34
|
+
# Every minute, the RRD is updated with the hits for the previous period.
|
35
|
+
lt.add_trend(:total) {|line| line.match /.*/}
|
36
|
+
lt.add_trend(:fbod) {|line| line.match /fogbugz.com/}
|
37
|
+
lt.add_trend(:kod) {|line| line.match /kilnhg.com/}
|
38
|
+
lt.add_trend(:long) do |line|
|
39
|
+
# Let us pretend that request time is in seconds
|
40
|
+
# and is the last item on the log line
|
41
|
+
request_time = line.split.last.to_i
|
42
|
+
request_time > 10
|
43
|
+
end
|
21
44
|
|
22
|
-
#
|
23
|
-
|
45
|
+
# Build a graph displaying some of the items we are trending
|
46
|
+
# Label it as :requests_per_minute
|
47
|
+
lt.add_graph(:requests_per_minute) do |g|
|
24
48
|
g.add_point :area, :total, "#333333"
|
25
49
|
g.add_point :line, :fbod, "#0066cc"
|
26
50
|
g.add_point :line, :kod, "#993333"
|
27
51
|
end
|
52
|
+
|
53
|
+
# Build a second graph for our long running queries
|
54
|
+
lt.add_graph(:long_requests) do |g|
|
55
|
+
g.add_point :area, :long, '#000000'
|
56
|
+
end
|
28
57
|
|
29
|
-
end
|
58
|
+
end
|
data/samples/test.log
CHANGED
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:
|
4
|
+
hash: 40202410167681
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 7
|
9
|
-
-
|
10
|
-
version: 0.7.
|
9
|
+
- 20101205083841
|
10
|
+
version: 0.7.20101205083841
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Michael Gorsuch
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-12-
|
18
|
+
date: 2010-12-05 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|