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



135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/fluent/plugin/in_windows_eventlog2.rb', line 135

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



66
67
68
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
# File 'lib/fluent/plugin/in_windows_eventlog2.rb', line 66

def configure(conf)
  super
  @chs = []

  @read_existing_events = @read_from_head || @read_existing_events
  if @channels.empty? && @subscribe_configs.empty?
    @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
  @keynames.delete('Qualifiers') unless @render_as_xml
  @keynames.delete('EventData') if @parse_description

  @tag = tag
  @bookmarks_storage = storage_create(usage: "bookmarks")
  @winevt_xml = false
  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
end

#escape_channel(ch) ⇒ Object



150
151
152
# File 'lib/fluent/plugin/in_windows_eventlog2.rb', line 150

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

#initalizeObject



60
61
62
63
64
# File 'lib/fluent/plugin/in_windows_eventlog2.rb', line 60

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

#on_notify(ch, subscribe) ⇒ Object



154
155
156
# File 'lib/fluent/plugin/in_windows_eventlog2.rb', line 154

def on_notify(ch, subscribe)
  # for safety.

end

#on_notify_hash(ch, subscribe) ⇒ Object



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
# File 'lib/fluent/plugin/in_windows_eventlog2.rb', line 199

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



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/fluent/plugin/in_windows_eventlog2.rb', line 158

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



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
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/fluent/plugin/in_windows_eventlog2.rb', line 236

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



107
108
109
110
111
112
113
# File 'lib/fluent/plugin/in_windows_eventlog2.rb', line 107

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



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/fluent/plugin/in_windows_eventlog2.rb', line 115

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)
  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
  timer_execute("in_windows_eventlog_#{escape_channel(ch)}".to_sym, @read_interval) do
    on_notify(ch, subscribe)
  end
end

#to_key(key) ⇒ Object



275
276
277
278
279
# File 'lib/fluent/plugin/in_windows_eventlog2.rb', line 275

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