blackdog 0.0.2

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.
@@ -0,0 +1,105 @@
1
+ # blackdog
2
+
3
+ A Ruby program that watches lines added to log files (or any text file) for
4
+ patterns and invokes specific actions when these patterns match.
5
+
6
+ ## Install
7
+
8
+ To install, change directory to where you have extracted the blackdog source
9
+ files and issue:
10
+
11
+ gem build blackdog.gemspec
12
+ gem install blackdog-*.gem
13
+
14
+ Alternatively, you can download the gem remotely from [RubyGems.org][]:
15
+
16
+ gem install -r blackdog
17
+
18
+ [RubyGems.org]: http://rubygems.org/
19
+
20
+ ## Use
21
+
22
+ blackdog comes with a binary of the same name that you use to run configuration
23
+ files.
24
+
25
+ A blackdog configuration file is a pure Ruby script that contains a simple and
26
+ pretty intuitive [DSL][] for specifying which files you want to watch, for which
27
+ patterns and the actions to take when each of these patterns matches.
28
+
29
+ [DSL]: http://en.wikipedia.org/wiki/Domain-specific_language
30
+
31
+ A typical example of what you'd do to watch the access log file of a
32
+ [lighttpd][] instance follows:
33
+
34
+ [lighttpd]: http://www.lighttpd.net/
35
+
36
+ file '/var/log/lighttpd/access.log' do |f|
37
+ f.watch {|w|
38
+ w.for /^([^ ]+)/
39
+
40
+ w.limit 1, :every => MINUTE
41
+
42
+ w.do {|line, matches|
43
+ from = 'blackdog@localhost'
44
+ to = 'user@example.com'
45
+ subject = "#{f.path}: remote connection from #{matches[1]}"
46
+ body = line
47
+ mail(from, to, subject, body)
48
+ }
49
+ }
50
+ end
51
+
52
+ The above example reads:
53
+
54
+ * Watch file _/var/log/lighttpd/access.log_
55
+ * For any line, capturing the first word (text until the first space character)
56
+ * Limit the number of matches to one per minute
57
+ * Send an email whenever there's a match
58
+
59
+ Some things to note:
60
+
61
+ * [Capturing groups][groups] in the pattern are available in the actions block
62
+ along with the matching line through the `matches` and `line` variables
63
+ respectively.
64
+ * It's possible to throttle the number of matches in a watch (M matches in N
65
+ seconds). Time constants for seconds, minutes, hours and days (both singular
66
+ and plural) are defined for this purpose. In the above example, `MINUTE` could
67
+ also have been written as `1 * MINUTE`, `1 * MINUTES`, `60 * SECONDS`,
68
+ `60 * SECOND` or simply `60`.
69
+ * The following additional data is available inside the actions block:
70
+ * `f.path` -- path of file being watched
71
+ * `w.pattern` -- matching pattern
72
+ * `w.throttle` -- throttling parameters (optional)
73
+ * What you do inside the actions block is up to you (e.g. send an email message,
74
+ post to a private [Twitter][] account, etc.)
75
+
76
+ [groups]: http://www.regular-expressions.info/brackets.html
77
+ [Twitter]: http://twitter.com/
78
+
79
+ ## License
80
+
81
+ (The MIT License)
82
+
83
+ Copyright (c) 2010 Aggelos Orfanakos
84
+
85
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
86
+ this software and associated documentation files (the "Software"), to deal in
87
+ the Software without restriction, including without limitation the rights to
88
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
89
+ the Software, and to permit persons to whom the Software is furnished to do so,
90
+ subject to the following conditions:
91
+
92
+ The above copyright notice and this permission notice shall be included in all
93
+ copies or substantial portions of the Software.
94
+
95
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
96
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
97
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
98
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
99
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
100
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
101
+
102
+ ## Author
103
+
104
+ [Aggelos Orfanakos](http://agorf.gr/), with suggestions and help from [Christos
105
+ Trochalakis](http://ctrochalakis.org/).
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'eventmachine'
5
+ require 'blackdog/blackdog'
6
+
7
+ if ARGV.size != 1
8
+ $stderr.puts "usage: #$0 <config_file>"
9
+ exit 1
10
+ end
11
+
12
+ conf = ARGV[0]
13
+
14
+ if not File.exist?(conf)
15
+ $stderr.puts "#{conf}: file does not exist"
16
+ exit 2
17
+ end
18
+
19
+ begin
20
+ EM.run do
21
+ conf_data = open(conf).read
22
+ BlackDog.module_eval(conf_data)
23
+ end
24
+ rescue Interrupt
25
+ puts
26
+ puts 'Bye!'
27
+ end
@@ -0,0 +1,121 @@
1
+ require 'logger'
2
+ require 'eventmachine-tail'
3
+
4
+ module BlackDog
5
+ extend self
6
+
7
+ SECOND = SECONDS = 1
8
+ MINUTE = MINUTES = 60 * SECONDS
9
+ HOUR = HOURS = 60 * MINUTES
10
+ DAY = DAYS = 24 * HOURS
11
+
12
+ LOGGER = Logger.new(STDERR)
13
+ LOGGER.level = Logger::DEBUG
14
+
15
+ def self.log; LOGGER end
16
+
17
+ class Watch
18
+ attr_reader :pattern, :throttle, :actions
19
+
20
+ def initialize
21
+ @pattern = nil
22
+ @throttle = nil
23
+ @actions = nil
24
+ @window = []
25
+ end
26
+
27
+ def for(pattern)
28
+ @pattern = pattern
29
+ end
30
+
31
+ def limit(times, opts)
32
+ every = opts[:every]
33
+
34
+ if times.is_a?(Integer) and times > 0 &&
35
+ every.is_a?(Integer) and every > 0
36
+ @throttle = {:times => times, :every => every}
37
+ else
38
+ @throttle = false
39
+ end
40
+ end
41
+
42
+ def do(&block)
43
+ @actions = block
44
+ end
45
+
46
+ def throttled?
47
+ return false if @throttle.nil?
48
+
49
+ now = Time.new
50
+
51
+ if @window.size == @throttle[:times]
52
+ if now - @window[0] <= @throttle[:every]
53
+ return true
54
+ end
55
+
56
+ @window.shift
57
+ end
58
+
59
+ @window << now
60
+
61
+ return false
62
+ end
63
+
64
+ def match(line)
65
+ @pattern && line.match(@pattern)
66
+ end
67
+ end
68
+
69
+ class LogFile
70
+ attr_reader :path, :watches
71
+
72
+ def initialize(path)
73
+ @path = path
74
+ @watches = []
75
+ end
76
+
77
+ def watch
78
+ yield w = Watch.new
79
+ @watches << w
80
+
81
+ # {{{ logging
82
+ if w.throttle
83
+ BlackDog.log.debug(@path) {
84
+ "watching for #{w.pattern.inspect} with throttle \
85
+ #{w.throttle[:times]}/#{w.throttle[:every]} secs"
86
+ }
87
+ else
88
+ BlackDog.log.debug(@path) { "watching for #{w.pattern.inspect}" }
89
+
90
+ if w.throttle == false
91
+ BlackDog.log.warn(@path) {
92
+ "ignoring invalid throttle for watch #{w.pattern.inspect}"
93
+ }
94
+ end
95
+ end
96
+ # }}}
97
+ end
98
+
99
+ def each_watch
100
+ @watches.each {|w| yield w }
101
+ end
102
+ end
103
+
104
+ def self.file(path)
105
+ yield f = LogFile.new(path)
106
+
107
+ EventMachine.file_tail(path) {|ft, line|
108
+ f.each_watch {|w|
109
+ if matches = w.match(line) and not w.throttled?
110
+ # {{{ logging
111
+ BlackDog.log.debug(f.path) {
112
+ "#{w.pattern.inspect} matched #{line.inspect} \
113
+ with matches #{matches.to_a.inspect}"
114
+ }
115
+ # }}}
116
+ w.actions.call(line, matches) if not w.actions.nil?
117
+ end
118
+ }
119
+ }
120
+ end
121
+ end
@@ -0,0 +1,3 @@
1
+ module BlackDog
2
+ VERSION = '0.0.2'
3
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: blackdog
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
11
+ platform: ruby
12
+ authors:
13
+ - Aggelos Orfanakos
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-20 00:00:00 +03: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: Watch log files for patterns and react
50
+ email: agorf@agorf.gr
51
+ executables:
52
+ - blackdog
53
+ extensions: []
54
+
55
+ extra_rdoc_files: []
56
+
57
+ files:
58
+ - README.md
59
+ - bin/blackdog
60
+ - lib/blackdog/blackdog.rb
61
+ - lib/blackdog/version.rb
62
+ has_rdoc: true
63
+ homepage: http://github.com/agorf/blackdog
64
+ licenses: []
65
+
66
+ post_install_message:
67
+ rdoc_options: []
68
+
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ hash: 3
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ hash: 3
86
+ segments:
87
+ - 0
88
+ version: "0"
89
+ requirements: []
90
+
91
+ rubyforge_project:
92
+ rubygems_version: 1.3.7
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: Watch log files for patterns and react
96
+ test_files: []
97
+