|
1 | 1 | require 'winevt' |
2 | 2 | require 'fluent/plugin/input' |
3 | 3 | require 'fluent/plugin' |
4 | | -require_relative 'bookmark_sax_parser' |
5 | 4 |
|
6 | 5 | module Fluent::Plugin |
7 | 6 | class WindowsEventLog2Input < Input |
| 7 | + begin |
| 8 | + require_relative 'bookmark_sax_parser' |
| 9 | + @@bookmark_parser_avaiable = true |
| 10 | + rescue LoadError |
| 11 | + @@bookmark_parser_avaiable = false |
| 12 | + end |
| 13 | + |
8 | 14 | Fluent::Plugin.register_input('windows_eventlog2', self) |
9 | 15 |
|
10 | 16 | class ReconnectError < Fluent::UnrecoverableError; end |
@@ -246,11 +252,16 @@ def clear_subscritpions |
246 | 252 | end |
247 | 253 |
|
248 | 254 | def subscription(ch, read_existing_events, remote_session) |
249 | | - bookmarkXml = @bookmarks_storage.get(ch) || "" |
250 | 255 | bookmark = nil |
251 | | - if bookmark_validator(bookmarkXml, ch) |
252 | | - bookmark = Winevt::EventLog::Bookmark.new(bookmarkXml) |
| 256 | + bookmarkXml = @bookmarks_storage.get(ch) || "" |
| 257 | + unless bookmarkXml.empty? |
| 258 | + if bookmark_valid?(bookmarkXml, ch) |
| 259 | + bookmark = Winevt::EventLog::Bookmark.new(bookmarkXml) |
| 260 | + else |
| 261 | + log.warn "This stored bookmark is incomplete for using. Referring `read_existing_events` parameter to subscribe: #{bookmarkXml}, channel: #{ch}" |
| 262 | + end |
253 | 263 | end |
| 264 | + |
254 | 265 | subscribe = Winevt::EventLog::Subscribe.new |
255 | 266 | subscribe.read_existing_events = read_existing_events |
256 | 267 | begin |
@@ -280,19 +291,26 @@ def subscribe_channels(subscriptions) |
280 | 291 | end |
281 | 292 | end |
282 | 293 |
|
283 | | - def bookmark_validator(bookmarkXml, channel) |
284 | | - return false if bookmarkXml.empty? |
| 294 | + def bookmark_valid?(bookmarkXml, channel) |
| 295 | + if @@bookmark_parser_avaiable |
| 296 | + bookmark_valid_strictly?(bookmarkXml, channel) |
| 297 | + else |
| 298 | + bookmarklist_is_not_empty?(bookmarkXml, channel) |
| 299 | + end |
| 300 | + end |
285 | 301 |
|
| 302 | + def bookmark_valid_strictly?(bookmarkXml, channel) |
286 | 303 | evtxml = WinevtBookmarkDocument.new |
287 | 304 | parser = Nokogiri::XML::SAX::Parser.new(evtxml) |
288 | 305 | parser.parse(bookmarkXml) |
289 | 306 | result = evtxml.result |
290 | | - if !result.empty? && (result[:channel].downcase == channel.downcase) && result[:is_current] |
291 | | - true |
292 | | - else |
293 | | - log.warn "This stored bookmark is incomplete for using. Referring `read_existing_events` parameter to subscribe: #{bookmarkXml}, channel: #{channel}" |
294 | | - false |
295 | | - end |
| 307 | + !result.empty? && (result[:channel].downcase == channel.downcase) && result[:is_current] |
| 308 | + end |
| 309 | + |
| 310 | + def bookmarklist_is_not_empty?(bookmarkXml, channel) |
| 311 | + # Empty example: "<BookmarkList>\r\n</BookmarkList>" |
| 312 | + # Not empty example: "<BookmarkList>\r\n <Bookmark Channel='Setup' RecordId='777' IsCurrent='true'/>\r\n</BookmarkList>" |
| 313 | + bookmarkXml.include?("Channel") |
296 | 314 | end |
297 | 315 |
|
298 | 316 | def escape_channel(ch) |
|
0 commit comments