Skip to content

Commit

Permalink
Add support for utf16 character encoding (#26)
Browse files Browse the repository at this point in the history
* add utf16 line encoding
* making utf16 encoding as option
* adding comments
* fixing inspections
  • Loading branch information
avihay-av authored and majormoses committed Jul 4, 2017
1 parent d715513 commit dc75801
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
This CHANGELOG follows the format listed at [Keep A Changelog](http://keepachangelog.com/)

## [Unreleased]
### Added
- Support of UTF16 encoded characters

## [1.1.2] - 2017-05-25
### Fixed
- Write the state file atomically by using flock where available (@nlopes)
Expand Down
13 changes: 13 additions & 0 deletions bin/check-log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ class CheckLog < Sensu::Plugin::Check::CLI
proc: proc(&:to_i),
default: 250

# Use this option when you expecting your log lines to contain invalid byte sequence in utf-8.
# https://github.com/sensu-plugins/sensu-plugins-logs/pull/26
option :encode_utf16,
description: 'Encode line with utf16 before matching',
short: '-eu',
long: '--encode-utf16',
boolean: true,
default: false

def run
unknown 'No log file specified' unless config[:log_file] || config[:file_pattern]
unknown 'No pattern specified' unless config[:pattern]
Expand Down Expand Up @@ -208,6 +217,10 @@ def search_log
line = get_log_entry(line)
end

if config[:encode_utf16]
line = line.encode('UTF-16', invalid: :replace, replace: '')
end

line = line.encode('UTF-8', invalid: :replace, replace: '')
bytes_read += line.bytesize
if config[:case_insensitive]
Expand Down

0 comments on commit dc75801

Please sign in to comment.