Class: Splash::Logs::LogScanner

Inherits:
Object
  • Object
show all
Includes:
Config, Constants
Defined in:
lib/splash/logs.rb

Overview

Log scanner and notifier

Constant Summary

Constants included from Constants

Constants::AUTHOR, Constants::BACKENDS_STRUCT, Constants::CONFIG_FILE, Constants::COPYRIGHT, Constants::DAEMON_LOGMON_SCHEDULING, Constants::DAEMON_METRICS_SCHEDULING, Constants::DAEMON_PID_FILE, Constants::DAEMON_PROCESS_NAME, Constants::DAEMON_PROCMON_SCHEDULING, Constants::DAEMON_STDERR_TRACE, Constants::DAEMON_STDOUT_TRACE, Constants::DEFAULT_RETENTION, Constants::EMAIL, Constants::EXECUTION_TEMPLATE, Constants::EXECUTION_TEMPLATE_TOKENS_LIST, Constants::LICENSE, Constants::LOGGERS_STRUCT, Constants::PID_PATH, Constants::PROMETHEUS_ALERTMANAGER_URL, Constants::PROMETHEUS_PUSHGATEWAY_URL, Constants::PROMETHEUS_URL, Constants::TRACE_PATH, Constants::TRANSPORTS_STRUCT, Constants::VERSION, Constants::WEBADMIN_IP, Constants::WEBADMIN_PID_FILE, Constants::WEBADMIN_PID_PATH, Constants::WEBADMIN_PORT, Constants::WEBADMIN_PROCESS_NAME, Constants::WEBADMIN_PROXY, Constants::WEBADMIN_STDERR_TRACE, Constants::WEBADMIN_STDOUT_TRACE

Instance Method Summary collapse

Methods included from Config

#get_config, #rehash_config

Methods included from ConfigUtilities

#addservice, #checkconfig, #flush_backend, #setupsplash

Methods included from Helpers

#check_unicode_term, #daemonize, #format_by_extensions, #format_response, #get_processes, #group_root, #install_file, #is_root?, #make_folder, #make_link, #run_as_root, #search_file_in_gem, #user_root, #verify_file, #verify_folder, #verify_link, #verify_service

Constructor Details

#initializeLogScanner

LogScanner Constructor : initialize prometheus metrics return [Splash::Logs::LogScanner]



94
95
96
97
98
# File 'lib/splash/logs.rb', line 94

def initialize
  @logs_target = Marshal.load(Marshal.dump(get_config.logs))
  @config = get_config

end

Instance Method Details

#analyseHash

start log analyse for log target in config

Returns:

  • (Hash)

    Exiter case :quiet_exit



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/splash/logs.rb', line 103

def analyse
  @logs_target.each do |record|
    record[:count]=0 if record[:count].nil?
    record[:status] = :clean if record[:status].nil?
    if File.exist?(record[:log]) then
      record[:count] = File.readlines(record[:log]).grep(/#{record[:pattern]}/).size
      record[:status] = :matched if record[:count] > 0
      record[:lines] = `wc -l "#{record[:log]}"`.strip.split(/\s+/)[0].to_i unless record[:status] == :missing
    else
      record[:status] = :missing
    end
  end
  return {:case => :quiet_exit }
end

#notify(options = {}) ⇒ Hash

start notification on prometheus for metric logerrors, logmissing; loglines

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :session (String)

    a session number for log daemon

Returns:

  • (Hash)

    Exiter case :quiet_exit



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/splash/logs.rb', line 128

def notify(options = {})
  log = get_logger
  unless verify_service url: @config.prometheus_pushgateway_url then
    return  { :case => :service_dependence_missing, :more => "Prometheus Notification not send." }
  end
  session = (options[:session]) ? options[:session] : log.get_session
  log.info "Sending metrics to Prometheus Pushgateway", session
  @logs_target.each do |item|
    logsrec = LogsRecords::new item[:label]
    errors = (item[:count])? item[:count] : 0
    lines = (item[:lines])? item[:lines] : 0
    missing = (item[:status] == :missing)? 1 : 0
    file = item[:log]
    logsrec.purge(item[:retention])
    logsrec.add_record :status => item[:status],
                     :errors => errors,
                     :lines => lines,
                     :file => file

    logsmonitor = LogsNotifier::new({name: item[:label], missing: missing, file: file, errors: errors, lines: lines})
    if logsmonitor.notify then
      log.ok "Sending metrics for log #{file} to Prometheus Pushgateway", session
    else
      log.ko "Failed to send metrics for log #{file} to Prometheus Pushgateway", session
    end
  end
  return {:case => :quiet_exit }
end

#outputHash

pseudo-accessor on @logs_target

Returns:

  • (Hash)

    the logs structure



120
121
122
# File 'lib/splash/logs.rb', line 120

def output
  return @logs_target
end