blooper 1.5

Sign up to get free protection for your applications and to get access to all the features.
data/COPYING ADDED
@@ -0,0 +1,30 @@
1
+ Copyright (c) 2012 paranormal <mbsd@isgroup.com.ua>
2
+
3
+ All rights reserved.
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are
7
+ met:
8
+
9
+ 1. Redistributions of source code must retain the above copyright
10
+ notice, this list of conditions and the following disclaimer.
11
+
12
+ 2. Redistributions in binary form must reproduce the above copyright
13
+ notice, this list of conditions and the following disclaimer in the
14
+ documentation and/or other materials provided with the distribution.
15
+
16
+ 3. Neither the name of the organizations nor the names of its
17
+ contributors may be used to endorse or promote products derived
18
+ from this software without specific prior written permission.
19
+
20
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
+ ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,32 @@
1
+ == Welcome to blooper
2
+
3
+ {Blooper}[http://www.mariowiki.com/Blooper] is squid-like creatures that are found in the ocean.
4
+ It can also write squid access log into a database (I believe any sequel-support database).
5
+ Should you write a squid access log into a data base, you won't have to catch a blooper in the ocean of errors for that.
6
+ I have already tamed one, and you are free to use it.
7
+
8
+ The idea is shamelessly stolen from {logmysqldaemon}[http://sourceforge.net/projects/logmysqldaemon/] script.
9
+
10
+ == Installation
11
+
12
+ gem install (pg|mysql|oracle|sqlite|etc sequel database adapter)
13
+ gem install blooper
14
+
15
+ == Squid configuration
16
+
17
+ logformat squid_log time %ts.%03tu response_time %tr src_ip %>a squid_request_status %Ss http_status_code %03>Hs reply_size %<st request_method %rm request_url %ru username %un squid_hier_code %Sh dst_ip %<a mime_type %mt
18
+ access_log daemon:{adapter:postgres,database:squid,username:squid,password:squid,host:db} squid_log
19
+ logfile_daemon /usr/local/bin/blooper
20
+
21
+ 1. The column names are dynamic, here time is a column name, %ts.%03tu is a value that will have been written into that column.
22
+
23
+ 2. The second string contains database access information, the credential depends on {adapter}[http://sequel.rubyforge.org/rdoc/files/doc/opening_databases_rdoc.html], tested only with postgres.
24
+
25
+ 3. The third string is a full path to the executable ($ which blooper).
26
+
27
+ == Database configuration
28
+
29
+ createdb squid
30
+ psql squid < pg.schema
31
+
32
+ Or use your own schema integration, just specify the proper format into the logformat variable in squid.conf.
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # $VERBOSE = true
4
+ # require 'ap'
5
+
6
+ %w{logger singleton yaml sequel}.each {|lib| require lib}
7
+ require_relative '../lib/blooper'
8
+
9
+ Blooper.application.start
@@ -0,0 +1,64 @@
1
+ require_relative 'blooper/version.rb'
2
+ BLOOPER_VERSION = Blooper::VERSION
3
+
4
+ require_relative 'blooper/db.rb'
5
+ require_relative 'blooper/input.rb'
6
+ require_relative 'blooper/line.rb'
7
+ require_relative 'blooper/rows.rb'
8
+
9
+ module Blooper
10
+
11
+ class << self
12
+ # Current Blooper Application
13
+ def application
14
+ @application ||= Application.new
15
+ end
16
+ end
17
+
18
+
19
+ class Application < Logger::Application
20
+
21
+ DATE_FORMAT = '%Y/%m/%d %H:%M:%S'
22
+ LOGGER_LEVEL = Logger::INFO
23
+
24
+ def initialize
25
+ super('Blooper')
26
+ self.level = $VERBOSE && Logger::DEBUG || LOGGER_LEVEL
27
+ self.logger.formatter = formatter
28
+ @input = Input.new
29
+ end
30
+
31
+ def run
32
+ @log.info('Establishing the database connection')
33
+ DB.instance
34
+ @log.info('A database connection has been established')
35
+ @input.each do |rows|
36
+ begin
37
+ rows.save
38
+ @log.debug('Data was saved')
39
+ rescue Sequel::DatabaseDisconnectError
40
+ @log.warn('A database connection has been lost, reconnecting...')
41
+ DB.instance.connect
42
+ retry
43
+ rescue Sequel::DatabaseError
44
+ @log.error('Probably data doesn\'t fit for database')
45
+ next
46
+ rescue Sequel::Error => error
47
+ @log.error(error.message)
48
+ next
49
+ end
50
+ end
51
+ end
52
+
53
+ private
54
+
55
+ def formatter
56
+ -> severity, datetime, appname, msg do
57
+ "#{datetime.strftime(DATE_FORMAT)} " +
58
+ "#{appname}(#{severity[0]})| " +
59
+ "#{msg}\n"
60
+ end
61
+ end
62
+
63
+ end
64
+ end
@@ -0,0 +1,26 @@
1
+ module Blooper
2
+ class DB
3
+
4
+ include Singleton
5
+
6
+ def initialize
7
+ @credentials = credentials
8
+ @accesses = connect
9
+ end
10
+
11
+ def connect
12
+ Sequel.connect(@credentials)[:accesses]
13
+ end
14
+
15
+ def insert(params)
16
+ @accesses.insert(params)
17
+ end
18
+
19
+ private
20
+
21
+ def credentials
22
+ YAML.load(ARGV.join(" ").gsub(/:/, ': '))
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,16 @@
1
+ module Blooper
2
+ class Input
3
+
4
+ def initialize(input = STDIN)
5
+ @input = input
6
+ end
7
+
8
+ def each
9
+ @input.each do |line|
10
+ line = Line.new(line)
11
+ yield Rows.new(line.clean) if line.valid?
12
+ end
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,19 @@
1
+ module Blooper
2
+ class Line
3
+
4
+ REG = Regexp.new('^L')
5
+
6
+ def initialize(line)
7
+ @line = line
8
+ end
9
+
10
+ def valid?
11
+ @line.match(REG) && @line.split.size.even?
12
+ end
13
+
14
+ def clean
15
+ @line.sub(REG, '')
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ module Blooper
2
+ class Rows
3
+
4
+ def initialize(line = STDIN)
5
+ @line = line
6
+ end
7
+
8
+ def rows
9
+ Hash[*@line.split]
10
+ end
11
+
12
+ def save
13
+ DB.instance.insert(rows)
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module Blooper
2
+ VERSION = "1.5" unless defined?(Blooper::VERSION)
3
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: blooper
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.5'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - paranormal
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '2.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: sequel
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 3.38.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 3.38.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: work through squid access log daemon
63
+ email: mbsd@isgroup.com.ua
64
+ executables:
65
+ - blooper
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - bin/blooper
70
+ - lib/blooper.rb
71
+ - lib/blooper/input.rb
72
+ - lib/blooper/line.rb
73
+ - lib/blooper/rows.rb
74
+ - lib/blooper/db.rb
75
+ - lib/blooper/version.rb
76
+ - COPYING
77
+ - README.rdoc
78
+ homepage: https://github.com/paranormal/blooper
79
+ licenses:
80
+ - bsd
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: 1.9.0
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: 1.3.6
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 1.8.24
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: squid's access log collector — Read more
103
+ test_files: []