Class: Apalo::Core::LogParser

Inherits:
Object
  • Object
show all
Defined in:
lib/apalo/core/log_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filter = nil) ⇒ LogParser

Returns a new instance of LogParser.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/apalo/core/log_parser.rb', line 7

def initialize(filter = nil)
  @processed_lines = 0
  @filtered_lines = 0
  @errors = 0
  @logline = LogLine.new
  if filter
    begin
      require 'oniguruma'
      @filter = Oniguruma::ORegexp.new(filter) 
    rescue Exception => e
      STDERR.puts \
        "WARNING: oniguruma gem not installed. Log analysis will be much slower."
      @filter  = /#{filter}/
    end
  end
  @regex = nil
  r = [
  '(\d+\.\d+\.\d+\.\d+)',    # ip
  '(.*?)',                   # foo
  '(.*?)',                   # bar
  '\[(.*?)\]',             # datetime
  '"(.*?)"',                 # request
  '(\d+)',                 # code
  '(-|\d+)',               # size
  '"(.*?)"',                 # referer
  '"(.*?)"',               # user-agent 
  '\[(.*?)\]'              # vhost
  ]
  logr = "^#{r.join('\s+')}$"
  begin
    require 'oniguruma'
    @regex = Oniguruma::ORegexp.new(logr) 
  rescue Exception => e
    puts e.message
    STDERR.puts \
      "WARNING: oniguruma gem not installed. Log analysis will be much slower."
    @regex  = /#{logr}/
  end
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



5
6
7
# File 'lib/apalo/core/log_parser.rb', line 5

def errors
  @errors
end

#filtered_linesObject (readonly)

Returns the value of attribute filtered_lines.



5
6
7
# File 'lib/apalo/core/log_parser.rb', line 5

def filtered_lines
  @filtered_lines
end

#processed_linesObject (readonly)

Returns the value of attribute processed_lines.



5
6
7
# File 'lib/apalo/core/log_parser.rb', line 5

def processed_lines
  @processed_lines
end

Instance Method Details

#each_lineObject



47
48
49
50
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
# File 'lib/apalo/core/log_parser.rb', line 47

def each_line
  File.open(Apalo.logfile) do |f|
    if Apalo.logfile =~ /\.gz/
      handle = Zlib::GzipReader.new(f)
    else
      handle = f
    end
    handle.each_line do |line|
      @processed_lines += 1
      if @regex.match(line)
        @logline.raw = line
        @logline.ipaddr      = $1
        @logline.ident       = $2
        @logline.userid      = $3
        @logline.time        = $4
        @logline.request     = $5
        @logline.rcode       = $6
        @logline.rsize       = $7
        @logline.referer     = $8
        @logline.user_agent  = $9
        @logline.vhost       = $10
        @logline.raw         = line
        yield @logline
      else
        Apalo.parsing_errors << line
      end
    end
  end
end