class DansguardianDeniedAction::AccessLog

Main interface to the gem

Public Class Methods

new( format:, path: '/var/log/dansguardian/access.log', action: 'DENIED' ) click to toggle source

@param format [Integer] The log file format specified on the “logfileformat” property

in /etc/dansguardian/dansguardian.conf. Options include:  1 = Dansguardian format
(space delimited), 2 = CSV-style format, 3 = Squid Log File Format, 4 = Tab delimited,
5 = Protex format, 6 = Protex format with server field blanked.

@param path [String] File path to access.log @param action [String] Action string to trigger notifications of observers @raise [ArgumentError] if an unsupported format is passed in @note Only format 2 is currently supported!

# File lib/dansguardian_denied_action/access_log.rb, line 19
def initialize( format:, path: '/var/log/dansguardian/access.log', action: 'DENIED' )
  raise ArgumentError, "Unsupported format: #{format}" unless SUPPORTED_FORMATS.include?( format )
  @format = format
  @path = path
  @action = action
  @log_class = LOG_CLASSES[format]
end

Public Instance Methods

monitor( timeout: nil ) click to toggle source

Monitors the access.log by tailing the file watching for blocked pages accessed

@param timeout [Integer, NilClass] Specify a timeout for monitoring the access

logs.  If not specified, it will monitor the logs indefinitely.
# File lib/dansguardian_denied_action/access_log.rb, line 31
def monitor( timeout: nil )
  if timeout
    begin
      Timeout::timeout( timeout ) { tail_file }
    rescue Timeout::Error
      # Do nothing
    end
  else
    tail_file
  end
end

Private Instance Methods

process_line( line ) click to toggle source

Notifies the observers if a line in the log matches the action

@param line [String] Log line

# File lib/dansguardian_denied_action/access_log.rb, line 57
def process_line( line )
  if line =~ /\*#{@action}\*/
    changed
    log = @log_class.new( line ) 
    notify_observers( log )
  end
end
tail_file() click to toggle source

Tail the log files

# File lib/dansguardian_denied_action/access_log.rb, line 46
def tail_file
  file_watch = FileWatch::Tail.new
  file_watch.tail( "#{@path}*" )
  file_watch.subscribe do |_, line|
    process_line( line )
  end
end