bind_log_analyzer 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,8 @@
1
1
  require "bind_log_analyzer/version"
2
2
  require "bind_log_analyzer/base"
3
3
 
4
+ # BindLogAnalyzer analyzes a Bind query log file, parses the logs and store them in
5
+ # a database. The database choise is "limited" to the databases supported by ActiveRecord.
4
6
  module BindLogAnalyzer
5
7
 
6
8
  end
@@ -3,11 +3,22 @@ require 'bind_log_analyzer/connector'
3
3
  require 'models/log'
4
4
 
5
5
  module BindLogAnalyzer
6
+ # The main class of the BindLogAnalyzer module
6
7
  class Base
7
8
  include BindLogAnalyzer::Connector
8
9
 
9
- attr_reader :log_filename, :database_confs
10
+ # @attribute [r]
11
+ # @return [String] The file containing the logs to be analyzed
12
+ attr_reader :log_filename
13
+ # @attribute [r]
14
+ # @return [Hash] The hash containing the database params and credentials
15
+ attr_reader :database_confs
10
16
 
17
+ # The constructor of BindLogAnalyzer::Base sets some vars and manages the setup of the database
18
+ # @param [Hash, String] database_confs The path to the database configurations file or a hash containing such informations
19
+ # @param [String] logfile The path to the file containing the Bind's logs to analyze
20
+ # @param [true, false] setup_database A flag which indicates whether to launch the database migration
21
+ # @param [Integer] log_level The level of the log requested by the user
11
22
  def initialize(database_confs = nil, logfile = nil, setup_database = false, log_level = 0)
12
23
  if database_confs
13
24
  if database_confs.instance_of?(Hash)
@@ -35,14 +46,21 @@ module BindLogAnalyzer
35
46
  setup_db(@database_confs, setup_database, log_level)
36
47
  end
37
48
 
49
+ # Sets the path to the log file checking if exists
50
+ # @param [String] logfile The path to the file containing the Bind's logs to analyze
38
51
  def logfile=(logfile)
39
52
  @log_filename = logfile if FileTest.exists?(logfile)
40
53
  end
41
54
 
55
+ # Returns the path to the Bind's log file
56
+ # @return [String] The path to the Bind's log file
42
57
  def logfile
43
58
  @log_filename
44
59
  end
45
60
 
61
+ # Parses a log line and creates a hash with the informations
62
+ # @param [String] line The query log line to parse
63
+ # @return [Hash, false] The hash containing the parsed line or false if the line couldn't be parsed
46
64
  def parse_line(line)
47
65
  query = {}
48
66
  regexp = %r{^(\d{2}-\w{3}-\d{4}\s+\d{2}:\d{2}:\d{2}\.\d{3})\s+client\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})#\d+:\s+query:\s+(.*)\s+IN\s+(\w+)\s+\+\s+\((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\)$}
@@ -65,11 +83,16 @@ module BindLogAnalyzer
65
83
  end
66
84
  end
67
85
 
86
+ # Stores the parsed log line into the database. Increments @stored_queries if successful
87
+ # @param [Hash] query The log line parsed by #parse_line
68
88
  def store_query(query)
69
89
  log = Log.new(query)
70
90
  @stored_queries += 1 if log.save
71
91
  end
72
92
 
93
+ # The main method used to manage the analysis operations.
94
+ # Opens the log file and passes wvery line to #parse_line and #store_query
95
+ # @return [true, false] False if there's a problem with the log file. True elsewhere.
73
96
  def analyze
74
97
  return false unless @log_filename
75
98
 
@@ -82,6 +105,7 @@ module BindLogAnalyzer
82
105
  end
83
106
  end
84
107
  puts "Analyzed #{lines} lines and correctly stored #{@stored_queries} logs"
108
+ return true
85
109
  end
86
110
  end
87
111
  end
@@ -2,8 +2,14 @@ require 'active_record'
2
2
  require 'logger'
3
3
 
4
4
  module BindLogAnalyzer
5
+ # The module which provides connection facility
5
6
  module Connector
6
7
 
8
+ # Main connection method which connects to the database, launches migrations if requested,
9
+ # loads the Log ActiveRecord model and setups the logger.
10
+ # @param [Hash] database_params The database params and credentials
11
+ # @param [true, false] setup_database If true launches the migrations of the database
12
+ # @param [Integer] log_level The log level to setup the Logger
7
13
  def setup_db(database_params, setup_database = false, log_level = 0)
8
14
  @database_params = database_params
9
15
 
@@ -12,6 +18,7 @@ module BindLogAnalyzer
12
18
  migrate_tables if setup_database
13
19
 
14
20
  self.load_environment
21
+
15
22
  if log_level > 0
16
23
 
17
24
  log_level_class = {
@@ -26,20 +33,25 @@ module BindLogAnalyzer
26
33
  end
27
34
  end
28
35
 
36
+ # Launches ActiveRecord migrations
29
37
  def migrate_tables
30
38
  ActiveRecord::Migration.verbose = true
31
39
  migrations_dir = File.join(File.dirname(__FILE__), '..', '..', 'db/migrate')
32
40
  ActiveRecord::Migrator.migrate migrations_dir
33
41
  end
34
42
 
43
+ # Establishes the connection to the database
35
44
  def connect
36
45
  ActiveRecord::Base.establish_connection(@database_params)
37
46
  end
38
47
 
48
+ # Shows the status of the connection to the database
49
+ # @return [true, false] The status of the connection to the database
39
50
  def connected?
40
51
  ActiveRecord::Base.connected?
41
52
  end
42
53
 
54
+ # Loads the ActiveRecord models
43
55
  def load_environment
44
56
  Dir.glob('./lib/models/*').each { |r| require r }
45
57
  end
@@ -1,4 +1,5 @@
1
1
  module BindLogAnalyzer
2
+ # A custom exception to be raise when database configurations are wrong
2
3
  class DatabaseConfsNotValid < Exception
3
4
  end
4
5
  end
@@ -1,3 +1,4 @@
1
1
  module BindLogAnalyzer
2
- VERSION = "0.1.0"
2
+ # @return [String] The version of the gem
3
+ VERSION = "0.1.1"
3
4
  end
data/lib/models/log.rb CHANGED
@@ -1,2 +1,3 @@
1
+ # The Log object represents a log line of a Bind's query
1
2
  class Log < ActiveRecord::Base
2
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bind_log_analyzer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-04-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &8286660 !ruby/object:Gem::Requirement
16
+ requirement: &11066420 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *8286660
24
+ version_requirements: *11066420
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &8286020 !ruby/object:Gem::Requirement
27
+ requirement: &11065980 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *8286020
35
+ version_requirements: *11065980
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: simplecov
38
- requirement: &8285380 !ruby/object:Gem::Requirement
38
+ requirement: &11065540 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *8285380
46
+ version_requirements: *11065540
47
47
  description: BindLogAnalyzer analyzes a Bind query log file and stores it's data into
48
48
  a SQL database (ActiveRecord is used for this feature)
49
49
  email:
@@ -60,7 +60,26 @@ files:
60
60
  - bin/bind_log_analyzer
61
61
  - bind_log_analyzer.gemspec
62
62
  - db/migrate/20120329151738_create_logs.rb
63
+ - doc/BindLogAnalyzer.html
64
+ - doc/BindLogAnalyzer/Base.html
65
+ - doc/BindLogAnalyzer/Connector.html
66
+ - doc/BindLogAnalyzer/DatabaseConfsNotValid.html
67
+ - doc/Log.html
68
+ - doc/_index.html
69
+ - doc/class_list.html
70
+ - doc/css/common.css
71
+ - doc/css/full_list.css
72
+ - doc/css/style.css
63
73
  - doc/database.yml
74
+ - doc/file.README.html
75
+ - doc/file_list.html
76
+ - doc/frames.html
77
+ - doc/index.html
78
+ - doc/js/app.js
79
+ - doc/js/full_list.js
80
+ - doc/js/jquery.js
81
+ - doc/method_list.html
82
+ - doc/top-level-namespace.html
64
83
  - doc/update_bind_log_analyzer.sh
65
84
  - lib/bind_log_analyzer.rb
66
85
  - lib/bind_log_analyzer/base.rb
@@ -99,3 +118,4 @@ test_files:
99
118
  - spec/bind_log_analyzer_spec.rb
100
119
  - spec/spec.opts
101
120
  - spec/spec_helper.rb
121
+ has_rdoc: