Class: Bizside::LogAnalyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/bizside/log_analyzer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(add_on_name, files) ⇒ LogAnalyzer

Returns a new instance of LogAnalyzer.



6
7
8
9
# File 'lib/bizside/log_analyzer.rb', line 6

def initialize(add_on_name, files)
  @add_on_name = add_on_name
  @files = files
end

Instance Attribute Details

#add_on_nameObject (readonly)

Returns the value of attribute add_on_name.



3
4
5
# File 'lib/bizside/log_analyzer.rb', line 3

def add_on_name
  @add_on_name
end

#error_contentsObject (readonly)

Returns the value of attribute error_contents.



4
5
6
# File 'lib/bizside/log_analyzer.rb', line 4

def error_contents
  @error_contents
end

Instance Method Details

#analyze(output_file_name) ⇒ Object



11
12
13
# File 'lib/bizside/log_analyzer.rb', line 11

def analyze(output_file_name)
  @success =  system("request-log-analyzer --format rails3 --parse-strategy cautious --file #{output_file_name}.html --output HTML #{@files.join(' ')}")
end

#divide_into_pid(content) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bizside/log_analyzer.rb', line 48

def divide_into_pid(content)
  pid = nil
  ret = {}
  content.each_line do |c|
    s = c.scan(/#[0-9]+\]/)
    pid = s.first.scan(/[0-9]+/).first unless s.empty?
    ret[pid] = "" unless ret[pid]
    ret[pid] << c
  end

  ret
end

#duplicate_error_log?(extract_error_logs, duplicate_check_lines) ⇒ Boolean

TODO 重複したエラーログを除外する

Returns:

  • (Boolean)


107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/bizside/log_analyzer.rb', line 107

def duplicate_error_log?(extract_error_logs, duplicate_check_lines)
  res = false
  extract_error_logs.each do |eer|
    tmp = false
    duplicate_check_lines.each do |dcl|
      tmp = eer.include?(dcl)
      break unless tmp
    end
    res = tmp
    break if res
  end

  res
end

#exclude_error_log_line(line) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/bizside/log_analyzer.rb', line 87

def exclude_error_log_line(line)
  ret = nil
  ret = line.match("INFO -- : ジョブ.*登録します。")

  if ret.nil?
    line
  else
    ""
  end
end

#extract_error_log(partition_contents) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/bizside/log_analyzer.rb', line 61

def extract_error_log(partition_contents)
  ret = {}
  partition_contents.each do |pid, pc|
    tmp = ""
    pc.each_line do |line|
      unless line.scan(/: Started /).empty?
        if !tmp.scan(/Completed 500/).empty? or !tmp.scan(/\] FATAL/).empty? or !tmp.scan(/\[FATAL\]/).empty?
          ret[pid] = "" if ret[pid].nil? or ret[pid].empty?
          ret[pid] << tmp
        end
        tmp.clear
      end
      tmp << exclude_error_log_line(line)
    end

    # Startedの前にログが終了した場合、その時点までにエラーが含まれていれば抽出に含める
    if !tmp.empty? and !tmp.scan(/Completed 500/).empty? or !tmp.scan(/\] FATAL/).empty? or !tmp.scan(/\[FATAL\]/).empty?
      ret[pid] = "" if ret[pid].nil? or ret[pid].empty?
      ret[pid] << tmp
    end
    tmp.clear
  end

  ret
end

#get_duplicate_check_line(line) ⇒ Object

TODO 重複したエラーログを除外する



99
100
101
102
103
104
# File 'lib/bizside/log_analyzer.rb', line 99

def get_duplicate_check_line(line)
  ret = line.scan(/(INFO -- :.*|ERROR -- :.*|WARN -- :.*|FATAL -- :.*)/).first
  ret = ret.gsub(/\" for .*/, "\"") unless ret.nil or ret.empty?

  ret
end

#scrape_errorsObject



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
46
# File 'lib/bizside/log_analyzer.rb', line 19

def scrape_errors
  @error_content = String.new
  @error_contents = {}

  # TODO Okozeを解析できるようにする
  return if add_on_name == 'okoze'

  # TODO ログ内で重複したエラーメッセージを除外する
  return if ['amadai', 'webcam_api', 'seri', 'sushioke'].include?(add_on_name)

  @files.each do |file|
    content = File.read(file, encoding: 'utf-8')
    content = content.encode('UTF-16BE', 'UTF-8', :invalid => :replace, :undef => :replace, :replace => '?').encode('UTF-8')
    next unless content.include?('Completed 500') or content.include?('FATAL')

    # ログをプロセス単位に分割
    partition_contents = divide_into_pid(content)
    # プロセス単位に分割したログからエラーを抽出
    partition_content_errors = extract_error_log(partition_contents)
    # 抽出したエラーを出力用にまとめる
    partition_content_errors.each do |pid, pc|
      @error_contents[pid] = "#{file}のエラー抽出結果\n\n\n" if @error_contents[pid].nil? or @error_contents[pid].empty?
      @error_contents[pid] << pc
    end

    @error_contents
  end
end

#success?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/bizside/log_analyzer.rb', line 15

def success?
  @success
end