Skip to content

Commit 3238d55

Browse files
committed
Make it possible to work without Nokogiri
Nokogiri is not a mandatory dependency for this plugin, so this should work without it. Currently, we need to install Nokogiri manually. Otherwise, the following LoadError occurs: <internal:/path/to/rubygems/core_ext/kernel_require.rb>:88:in `require': cannot load such file -- nokogiri (LoadError) from <internal:/path/to/rubygems/core_ext/kernel_require.rb>:88:in `require' from /path/to/gems/fluent-plugin-windows-eventlog-0.8.3/lib/fluent/plugin/bookmark_sax_parser.rb:1:in `<top (required)>' ... Signed-off-by: Daijiro Fukuda <[email protected]>
1 parent 5a294c5 commit 3238d55

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
@@ -227,11 +233,16 @@ def clear_subscritpions
227233
end
228234

229235
def subscription(ch, read_existing_events, remote_session)
230-
bookmarkXml = @bookmarks_storage.get(ch) || ""
231236
bookmark = nil
232-
if bookmark_validator(bookmarkXml, ch)
233-
bookmark = Winevt::EventLog::Bookmark.new(bookmarkXml)
237+
bookmarkXml = @bookmarks_storage.get(ch) || ""
238+
unless bookmarkXml.empty?
239+
if bookmark_valid?(bookmarkXml, ch)
240+
bookmark = Winevt::EventLog::Bookmark.new(bookmarkXml)
241+
else
242+
log.warn "This stored bookmark is incomplete for using. Referring `read_existing_events` parameter to subscribe: #{bookmarkXml}, channel: #{ch}"
243+
end
234244
end
245+
235246
subscribe = Winevt::EventLog::Subscribe.new
236247
subscribe.read_existing_events = read_existing_events
237248
begin
@@ -258,19 +269,26 @@ def subscribe_channels(subscriptions)
258269
end
259270
end
260271

261-
def bookmark_validator(bookmarkXml, channel)
262-
return false if bookmarkXml.empty?
272+
def bookmark_valid?(bookmarkXml, channel)
273+
if @@bookmark_parser_avaiable
274+
bookmark_valid_strictly?(bookmarkXml, channel)
275+
else
276+
bookmarklist_is_not_empty?(bookmarkXml, channel)
277+
end
278+
end
263279

280+
def bookmark_valid_strictly?(bookmarkXml, channel)
264281
evtxml = WinevtBookmarkDocument.new
265282
parser = Nokogiri::XML::SAX::Parser.new(evtxml)
266283
parser.parse(bookmarkXml)
267284
result = evtxml.result
268-
if !result.empty? && (result[:channel].downcase == channel.downcase) && result[:is_current]
269-
true
270-
else
271-
log.warn "This stored bookmark is incomplete for using. Referring `read_existing_events` parameter to subscribe: #{bookmarkXml}, channel: #{channel}"
272-
false
273-
end
285+
!result.empty? && (result[:channel].downcase == channel.downcase) && result[:is_current]
286+
end
287+
288+
def bookmarklist_is_not_empty?(bookmarkXml, channel)
289+
# Empty example: "<BookmarkList>\r\n</BookmarkList>"
290+
# Not empty example: "<BookmarkList>\r\n <Bookmark Channel='Setup' RecordId='777' IsCurrent='true'/>\r\n</BookmarkList>"
291+
bookmarkXml.include?("Channel")
274292
end
275293

276294
def escape_channel(ch)

0 commit comments

Comments
 (0)