Skip to content

Commit e233b60

Browse files
authored
Merge pull request #107 from fluent/make-work-without-nokogiri
Make it possible to work without Nokogiri
2 parents f093143 + 3238d55 commit e233b60

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

lib/fluent/plugin/in_windows_eventlog2.rb

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
require 'winevt'
22
require 'fluent/plugin/input'
33
require 'fluent/plugin'
4-
require_relative 'bookmark_sax_parser'
54

65
module Fluent::Plugin
76
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+
814
Fluent::Plugin.register_input('windows_eventlog2', self)
915

1016
class ReconnectError < Fluent::UnrecoverableError; end
@@ -246,11 +252,16 @@ def clear_subscritpions
246252
end
247253

248254
def subscription(ch, read_existing_events, remote_session)
249-
bookmarkXml = @bookmarks_storage.get(ch) || ""
250255
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
253263
end
264+
254265
subscribe = Winevt::EventLog::Subscribe.new
255266
subscribe.read_existing_events = read_existing_events
256267
begin
@@ -280,19 +291,26 @@ def subscribe_channels(subscriptions)
280291
end
281292
end
282293

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
285301

302+
def bookmark_valid_strictly?(bookmarkXml, channel)
286303
evtxml = WinevtBookmarkDocument.new
287304
parser = Nokogiri::XML::SAX::Parser.new(evtxml)
288305
parser.parse(bookmarkXml)
289306
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")
296314
end
297315

298316
def escape_channel(ch)

0 commit comments

Comments
 (0)