Class: Fluent::FortigateSyslogParseOutput

Inherits:
Output
  • Object
show all
Defined in:
lib/fluent/plugin/out_fortigate_log_parser.rb

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object



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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fluent/plugin/out_fortigate_log_parser.rb', line 15

def configure(conf)
  super

  @prev_time_str = nil
  @prev_time = nil
  @country_map = nil

  if @message_key == ''
    fail ConfigError, 'message_key required a value'
  end

  if @remove_prefix
    @removed_prefix_string = @remove_prefix + '.'
    @removed_length = @removed_prefix_string.length
  end
  if @add_prefix
    @added_prefix_string = @add_prefix + '.'
  end

  if @keys
    if @remove_keys
      fail ConfigError, "fortigate_log_parser: 'keys' and 'remove_keys'" \
                         ' parameters are exclusive'
    end
    @keys = Hash[@keys.split(',').map { |x| [x, 1] }]
  end
  if @remove_keys
    @remove_keys = Hash[@remove_keys.split(',').map { |x| [x, 1] }]
  end

  if @country_map_file
    unless File.exist?(@country_map_file)
      fail ConfigError, "#{@country_map_file} does not exist"
    end
    @country_map = {}
    File.open(@country_map_file, 'r') do |f|
      f.each_line do |line|
        (country_name, country_code) = line.chomp.split(/\t/, 2)
        @country_map[country_name] = country_code
      end
    end
  end

  if @fortios_version >= 5
    @srccountry_key = 'srccountry'
    @dstcountry_key = 'dstcountry'
  else
    @srccountry_key = 'src_country'
    @dstcountry_key = 'dst_country'
  end
end

#emit(tag, es, chain) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/fluent/plugin/out_fortigate_log_parser.rb', line 67

def emit(tag, es, chain)
  _tag = tag.clone

  if @remove_prefix &&
    ((tag.start_with?(@removed_prefix_string) &&
      tag.length > @removed_length) || tag == @remove_prefix)
    tag = tag[@removed_length..-1] || ''
  end

  if @add_prefix
    tag = tag && tag.length > 0 ? @added_prefix_string + tag : @add_prefix
  end

  es.each do |time, record|
    time, record = parse(record)
    Engine.emit(tag, time, record)
  end

  chain.next
end

#parse(record) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/fluent/plugin/out_fortigate_log_parser.rb', line 88

def parse(record)
  message = record[@message_key]
  record.delete(@message_key)
  data = message.split(/\s+/, 5).pop
  data.gsub(/\G[^,=]+=(:?"[^"]*"|[^,]+)(:?,|$)/) do |kv|
    (k, v) = kv.strip.split(/=/, 2)
    if k == 'date' || k == 'time' ||
       (@keys && @keys.key?(k)) ||
       (@remove_keys && !@remove_keys.key?(k)) ||
       (!@keys && !@remove_keys)
      record[k] = v.gsub(/,$/, '').gsub(/^"(.*)"$/, '\1')
    end
  end

  # rsyslog workaround (remove the unnecessary white space)
  # rsyslog insert a space (0x20) after first colon (:)
  # (eg. time=12:34:56 -> time=12: 34:56)
  record['time'].gsub!(' ', '')

  time_str = record['date'] + ' ' + record['time']
  time = nil

  if @prev_time && time_str == @prev_time_str
    time = @prev_time
  else
    time = Time.strptime(time_str, '%Y-%m-%d %H:%M:%S').to_i
    @prev_time = time
    @prev_time_str = time_str
  end

  if @country_map
    if record.key?(@srccountry_key) &&
       @country_map.key?(record[@srccountry_key])
      record[@srccountry_key + '_code'] =
          @country_map[record[@srccountry_key]]
    end
    if record.key?(@dstcountry_key) &&
       @country_map.key?(record[@dstcountry_key])
      record[@dstcountry_key + '_code'] =
          @country_map[record[@dstcountry_key]]
    end
  end

  if record.key?('file')
    record['file'] = URI.escape(record['file'])
  end

  if record.key?('filename')
    record['filename'] = URI.escape(record['filename'])
  end

  record.delete('date')
  record.delete('time')

  [time, record]
end