Skip to content

Instantly share code, notes, and snippets.

@gswallow
Created May 6, 2013 22:17
Show Gist options
  • Save gswallow/5528682 to your computer and use it in GitHub Desktop.
Save gswallow/5528682 to your computer and use it in GitHub Desktop.
class WindowsLogWatcher < Scout::Plugin
needs 'win32/file'
OPTIONS = <<-EOS
log_path
name: Log path
notes: Full path to the log file
term:
default: "[Ee]rror"
name: Search Term
notes: Returns the number of matches for this term. Use Regex formatting.
grep_options:
name: Grep Options
notes: Provide any options to pass to grep when running. For example, to count non-matching lines, enter 'v'. Use the abbeviated format and not 'invert-match'.
send_error_if_no_log:
attributes: advanced
default: 1
notes: 1=yes
EOS
def init
@log_file_path = option("log_path").to_s.strip
if @log_file_path.empty?
return error("Please provide a path to the log file.")
end
unless File.exists?(@log_file_path)
if option("send_error_if_no_log").to_s == "1"
return error("Log file does not exist.", "The log file could not be found at #{@log_file_path}.")
end
end
@term = option("term").to_s.strip
if @term.empty?
return error("The term cannot be empty.")
end
nil
end
def build_report
return if init()
last_length = memory(:last_bytes) || 0
current_length = File.size(@log_file_path)
matches = 0
elapsed_seconds = 0
if last_length > 0
read_length = current_length - last_length
# Check to see if this file was rotated. This occurs when the +current_length+ is less than the
# +last_length+. Don't return a count if this occurs.
if read_length >= 0
IO.read(@log_file_path, last_length).split("\n").each do |line|
error(line)
if line.match(@term)
matches.succ
end
end
elapsed_seconds = Time.now - @last_run
matches = matches / (elapsed_seconds / 60)
else
matches = nil
end
end
report(:log_path => @log_file_path, :read_length => read_length, :current_length => current_length, :last_length => last_length, :term => @term, :send_error_if_no_log => option("send_error_if_no_log").to_s, :matches => matches)
remember(:last_bytes, current_length)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment