Class: BindLogAnalyzer::Base
- Inherits:
-
Object
- Object
- BindLogAnalyzer::Base
- Defined in:
- lib/bind_log_analyzer/base.rb
Overview
The main class of the BindLogAnalyzer module
Instance Attribute Summary collapse
-
#log_filename ⇒ String
readonly
The file containing the logs to be analyzed.
Instance Method Summary collapse
-
#analyze ⇒ true, false
The main method used to manage the analysis operations.
-
#initialize(database_params = nil, logfile = nil, setup_database = false, log_level = 0, check_uniq = false) ⇒ Base
constructor
The constructor of BindLogAnalyzer::Base sets some vars and manages the setup of the database.
-
#logfile ⇒ String
Returns the path to the Bind’s log file.
-
#logfile=(logfile) ⇒ Object
Sets the path to the log file checking if exists.
-
#parse_line(line) ⇒ Hash, false
Parses a log line and creates a hash with the informations.
-
#store_query(query) ⇒ Object
Stores the parsed log line into the database and increments @stored_queries if successful.
Methods included from Connector
connect, #connected?, establish_connection, #load_environment, #migrate_tables, #setup_db, setup_db_confs
Methods included from LogUtils
Constructor Details
#initialize(database_params = nil, logfile = nil, setup_database = false, log_level = 0, check_uniq = false) ⇒ Base
The constructor of BindLogAnalyzer::Base sets some vars and manages the setup of the database
22 23 24 25 26 27 28 29 30 |
# File 'lib/bind_log_analyzer/base.rb', line 22 def initialize(database_params = nil, logfile = nil, setup_database = false, log_level = 0, check_uniq = false) @stored_queries = 0 @check_uniq = check_uniq @log = BindLogAnalyzer::LogUtils.set_log_level(log_level) self.logfile = logfile if logfile setup_db(database_params, setup_database) end |
Instance Attribute Details
#log_filename ⇒ String (readonly)
Returns The file containing the logs to be analyzed.
14 15 16 |
# File 'lib/bind_log_analyzer/base.rb', line 14 def log_filename @log_filename end |
Instance Method Details
#analyze ⇒ true, false
The main method used to manage the analysis operations. Opens the log file and passes wvery line to #parse_line and #store_query
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/bind_log_analyzer/base.rb', line 104 def analyze return false unless @log_filename lines = 0 File.new(@log_filename).each do |line| @log.debug "Got line: \"#{line}\"" query = self.parse_line(line) if query @log.debug "Storing line: \"#{query}\"" self.store_query(query) lines += 1 end end puts "Analyzed #{lines} lines and correctly stored #{@stored_queries} logs" return true end |
#logfile ⇒ String
Returns the path to the Bind’s log file
44 45 46 |
# File 'lib/bind_log_analyzer/base.rb', line 44 def logfile @log_filename end |
#logfile=(logfile) ⇒ Object
Sets the path to the log file checking if exists
34 35 36 37 38 39 40 |
# File 'lib/bind_log_analyzer/base.rb', line 34 def logfile=(logfile) if FileTest.exists?(logfile) @log_filename = logfile else @log.error("The provided log file doesn't exist") end end |
#parse_line(line) ⇒ Hash, false
Parses a log line and creates a hash with the informations
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/bind_log_analyzer/base.rb', line 51 def parse_line(line) query = {} regexp = /^(\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+\(.+\):\s+query:\s+(.*)\s+IN\s+(\w+)\s+\+\s+\((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\)$/ old_regexp = /^(\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})\)$/ parsed_line = line.scan(regexp) # Try the old version if parsed_line.size == 0 parsed_line = line.scan(old_regexp) end if parsed_line.size > 0 # Parse timestamp = Date._strptime(parsed_line[0][0], "%d-%b-%Y %H:%M:%S.%L") query_time = Time.local([:year], [:mon], [:mday], [:hour], [:min], [:sec], [:sec_fraction], [:zone]) query[:date] = query_time query[:client] = parsed_line[0][1] query[:query] = parsed_line[0][2] query[:q_type] = parsed_line[0][3] query[:server] = parsed_line[0][4] query else @log.error "Can't parse the line: \"#{line}\"" false end end |
#store_query(query) ⇒ Object
Stores the parsed log line into the database and increments @stored_queries if successful. It checks the uniqueness of a record if the @check_uniq flag is set
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/bind_log_analyzer/base.rb', line 83 def store_query(query) if @check_uniq unless Log.where(query) log = Log.new(:date => query[:date]) @stored_queries += 1 if log.save else @log.warn "Skipping duplicate entry: #{query}" end else log = Log.new(query) if log.save @stored_queries += 1 else @log.error "Error saving the log #{query}" end end end |