Class: Fluent::Plugin::WindowsEventLog2Input

Inherits:
Input
  • Object
show all
Defined in:
lib/fluent/plugin/in_windows_eventlog2.rb

Constant Summary collapse

DEFAULT_STORAGE_TYPE =
'local'
KEY_MAP =
{"ProviderName"      => ["ProviderName",          :string],
"ProviderGUID"      => ["ProviderGUID",          :string],
"EventID"           => ["EventID",               :string],
"Qualifiers"        => ["Qualifiers",            :string],
"Level"             => ["Level",                 :string],
"Task"              => ["Task",                  :string],
"Opcode"            => ["Opcode",                :string],
"Keywords"          => ["Keywords",              :string],
"TimeCreated"       => ["TimeCreated",           :string],
"EventRecordID"     => ["EventRecordID",         :string],
"ActivityID"        => ["ActivityID",            :string],
"RelatedActivityID" => ["RelatedActivityID",     :string],
"ProcessID"         => ["ProcessID",             :string],
"ThreadID"          => ["ThreadID",              :string],
"Channel"           => ["Channel",               :string],
"Computer"          => ["Computer",              :string],
"UserID"            => ["UserID",                :string],
"Version"           => ["Version",               :string],
"Description"       => ["Description",           :string],
"EventData"         => ["EventData",             :array]}
GROUP_DELIMITER =
"\r\n\r\n".freeze
RECORD_DELIMITER =
"\r\n\t".freeze
FIELD_DELIMITER =
"\t\t".freeze
NONE_FIELD_DELIMITER =
"\t".freeze

Instance Method Summary collapse

Instance Method Details

#bookmark_validator(bookmarkXml, channel) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/fluent/plugin/in_windows_eventlog2.rb', line 169

def bookmark_validator(bookmarkXml, channel)
  return false if bookmarkXml.empty?

  evtxml = WinevtBookmarkDocument.new
  parser = Nokogiri::XML::SAX::Parser.new(evtxml)
  parser.parse(bookmarkXml)
  result = evtxml.result
  if !result.empty? && (result[:channel].downcase == channel.downcase) && result[:is_current]
    true
  else
    log.warn "This stored bookmark is incomplete for using. Referring `read_existing_events` parameter to subscribe: #{bookmarkXml}, channel: #{channel}"
    false
  end
end

#configure(conf) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
# File 'lib/fluent/plugin/in_windows_eventlog2.rb', line 69

def configure(conf)
  super
  @chs = []
  @all_chs = Winevt::EventLog::Channel.new
  @all_chs.force_enumerate = false

  if @read_all_channels
    @all_chs.each do |ch|
      uch = ch.strip.downcase
      @chs.push([uch, @read_existing_events])
    end
  end

  @read_existing_events = @read_from_head || @read_existing_events
  if @channels.empty? && @subscribe_configs.empty? && !@read_all_channels
    @chs.push(['application', @read_existing_events])
  else
    @channels.map {|ch| ch.strip.downcase }.uniq.each do |uch|
      @chs.push([uch, @read_existing_events])
    end
    @subscribe_configs.each do |subscribe|
      subscribe.channels.map {|ch| ch.strip.downcase }.uniq.each do |uch|
        @chs.push([uch, subscribe.read_existing_events])
      end
    end
  end
  @chs.uniq!
  @keynames = @keys.map {|k| k.strip }.uniq
  if @keynames.empty?
    @keynames = KEY_MAP.keys
  end

  @tag = tag
  @bookmarks_storage = storage_create(usage: "bookmarks")
  @winevt_xml = false
  @parser = nil
  if @render_as_xml
    @parser = parser_create
    @winevt_xml = @parser.respond_to?(:winevt_xml?) && @parser.winevt_xml?
    class << self
      alias_method :on_notify, :on_notify_xml
    end
  else
    class << self
      alias_method :on_notify, :on_notify_hash
    end
  end

  if @render_as_xml && @preserve_qualifiers_on_hash
    raise Fluent::ConfigError, "preserve_qualifiers_on_hash must be used with Hash object rendering(render_as_xml as false)."
  end
  if !@render_as_xml && !@preserve_qualifiers_on_hash
    @keynames.delete('Qualifiers')
  elsif @parser.respond_to?(:preserve_qualifiers?) && !@parser.preserve_qualifiers?
    @keynames.delete('Qualifiers')
  end
  @keynames.delete('EventData') if @parse_description

  locale = Winevt::EventLog::Locale.new
  if @description_locale && unsupported_locale?(locale, @description_locale)
    raise Fluent::ConfigError, "'#{@description_locale}' is not supported. Supported locales are: #{locale.each.map{|code, _desc| code}.join(" ")}"
  end
end

#escape_channel(ch) ⇒ Object



184
185
186
# File 'lib/fluent/plugin/in_windows_eventlog2.rb', line 184

def escape_channel(ch)
  ch.gsub(/[^a-zA-Z0-9\s]/, '_')
end

#initalizeObject



63
64
65
66
67
# File 'lib/fluent/plugin/in_windows_eventlog2.rb', line 63

def initalize
  super
  @chs = []
  @keynames = []
end

#on_notify(ch, subscribe) ⇒ Object



188
189
190
# File 'lib/fluent/plugin/in_windows_eventlog2.rb', line 188

def on_notify(ch, subscribe)
  # for safety.

end

#on_notify_hash(ch, subscribe) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/fluent/plugin/in_windows_eventlog2.rb', line 233

def on_notify_hash(ch, subscribe)
  es = Fluent::MultiEventStream.new
  begin
    subscribe.each do |record, message, string_inserts|
      record["Description"] = message
      record["EventData"] = string_inserts
      h = {}
      @keynames.each do |k|
        type = KEY_MAP[k][1]
        value = record[KEY_MAP[k][0]]
        h[k]=case type
             when :string
               value.to_s
             when :array
               value.map {|v| v.to_s}
             else
               raise "Unknown value type: #{type}"
             end
      end
      parse_desc(h) if @parse_description
      es.add(Fluent::Engine.now, h)
    end
  rescue Winevt::EventLog::Query::Error => e
    log.warn "Invalid Hash data", error: e
    log.warn_backtrace
  end
  router.emit_stream(@tag, es)
  @bookmarks_storage.put(ch, subscribe.bookmark)
end

#on_notify_xml(ch, subscribe) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/fluent/plugin/in_windows_eventlog2.rb', line 192

def on_notify_xml(ch, subscribe)
  es = Fluent::MultiEventStream.new
  begin
    subscribe.each do |xml, message, string_inserts|
      @parser.parse(xml) do |time, record|
        # record.has_key?("EventData") for none parser checking.

        if @winevt_xml
          record["Description"] = message
          record["EventData"] = string_inserts

          h = {}
          @keynames.each do |k|
            type = KEY_MAP[k][1]
            value = record[KEY_MAP[k][0]]
            h[k]=case type
                 when :string
                   value.to_s
                 when :array
                   value.map {|v| v.to_s}
                 else
                   raise "Unknown value type: #{type}"
                 end
          end
          parse_desc(h) if @parse_description
          es.add(Fluent::Engine.now, h)
        else
          record["Description"] = message
          record["EventData"] = string_inserts
          # for none parser

          es.add(Fluent::Engine.now, record)
        end
      end
    end
  rescue Winevt::EventLog::Query::Error => e
    log.warn "Invalid XML data", error: e
    log.warn_backtrace
  end
  router.emit_stream(@tag, es)
  @bookmarks_storage.put(ch, subscribe.bookmark)
end

#parse_desc(record) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/fluent/plugin/in_windows_eventlog2.rb', line 270

def parse_desc(record)
  desc = record.delete("Description".freeze)
  return if desc.nil?

  elems = desc.split(GROUP_DELIMITER)
  record['DescriptionTitle'] = elems.shift
  previous_key = nil
  elems.each { |elem|
    parent_key = nil
    elem.split(RECORD_DELIMITER).each { |r|
      key, value = if r.index(FIELD_DELIMITER)
                     r.split(FIELD_DELIMITER)
                   else
                     r.split(NONE_FIELD_DELIMITER)
                   end
      key = "" if key.nil?
      key.chop!  # remove ':' from key

      if value.nil?
        parent_key = to_key(key)
      else
        # parsed value sometimes contain unexpected "\t". So remove it.

        value.strip!
        # merge empty key values into the previous non-empty key record.

        if key.empty?
          record[previous_key] = [record[previous_key], value].flatten.reject {|e| e.nil?}
        elsif parent_key.nil?
          record[to_key(key)] = value
        else
          k = "#{parent_key}.#{to_key(key)}"
          record[k] = value
        end
      end
      # XXX: This is for empty privileges record key.

      # We should investigate whether an another case exists or not.

      previous_key = to_key(key) unless key.empty?
    }
  }
end

#startObject



137
138
139
140
141
142
143
# File 'lib/fluent/plugin/in_windows_eventlog2.rb', line 137

def start
  super

  @chs.each do |ch, read_existing_events|
    subscribe_channel(ch, read_existing_events)
  end
end

#subscribe_channel(ch, read_existing_events) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/fluent/plugin/in_windows_eventlog2.rb', line 145

def subscribe_channel(ch, read_existing_events)
  bookmarkXml = @bookmarks_storage.get(ch) || ""
  bookmark = nil
  if bookmark_validator(bookmarkXml, ch)
    bookmark = Winevt::EventLog::Bookmark.new(bookmarkXml)
  end
  subscribe = Winevt::EventLog::Subscribe.new
  subscribe.read_existing_events = read_existing_events
  begin
    subscribe.subscribe(ch, "*", bookmark)
    if !@render_as_xml && @preserve_qualifiers_on_hash
      subscribe.preserve_qualifiers = @preserve_qualifiers_on_hash
    end
  rescue Winevt::EventLog::Query::Error => e
    raise Fluent::ConfigError, "Invalid Bookmark XML is loaded. #{e}"
  end
  subscribe.render_as_xml = @render_as_xml
  subscribe.rate_limit = @rate_limit
  subscribe.locale = @description_locale if @description_locale
  timer_execute("in_windows_eventlog_#{escape_channel(ch)}".to_sym, @read_interval) do
    on_notify(ch, subscribe)
  end
end

#to_key(key) ⇒ Object



309
310
311
312
313
# File 'lib/fluent/plugin/in_windows_eventlog2.rb', line 309

def to_key(key)
  key.downcase!
  key.gsub!(' '.freeze, '_'.freeze)
  key
end

#unsupported_locale?(locale, description_locale) ⇒ Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/fluent/plugin/in_windows_eventlog2.rb', line 133

def unsupported_locale?(locale, description_locale)
  locale.each.select {|c, _d| c.downcase == description_locale.downcase}.empty?
end