lib/splash/logs.rb in prometheus-splash-0.8.0 vs lib/splash/logs.rb in prometheus-splash-0.8.1

- old
+ new

@@ -4,10 +4,83 @@ module Splash # Logs namespace module Logs + class LogsNotifier + + @@registry = Prometheus::Client::Registry::new + @@metric_missing = Prometheus::Client::Gauge.new(:logmissing, docstring: 'SPLASH metric log missing', labels: [:log ]) + @@metric_count = Prometheus::Client::Gauge.new(:logerrors, docstring: 'SPLASH metric log error', labels: [:log ]) + @@metric_lines = Prometheus::Client::Gauge.new(:loglines, docstring: 'SPLASH metric log lines numbers', labels: [:log ]) + @@registry.register(@@metric_count) + @@registry.register(@@metric_missing) + @@registry.register(@@metric_lines) + + def initialize(options={}) + @config = get_config + @url = @config.prometheus_pushgateway_url + @name = options[:name] + @missing = options[:missing] + @lines = options[:lines] + @errors = options[:errors] + end + + # send metrics to Prometheus PushGateway + # @return [Bool] + def notify + unless verify_service url: @url then + return { :case => :service_dependence_missing, :more => "Prometheus Notification not send."} + end + @@metric_missing.set(@missing, labels: { log: @name }) + @@metric_count.set(@errors, labels: { log: @name }) + @@metric_lines.set(@lines, labels: { log: @name }) + hostname = Socket.gethostname + return Prometheus::Client::Push.new("Splash", hostname, @url).add(@@registry) + end + + end + + class LogsRecords + include Splash::Backends + include Splash::Constants + def initialize(name) + @name = name + @backend = get_backend :logs_trace + end + + def purge(retention) + retention = {} if retention.nil? + if retention.include? :hours then + adjusted_datetime = DateTime.now - retention[:hours].to_f / 24 + elsif retention.include? :hours then + adjusted_datetime = DateTime.now - retention[:days].to_i + else + adjusted_datetime = DateTime.now - DEFAULT_RETENTION + end + + data = get_all_records + + data.delete_if { |item| + DateTime.parse(item.keys.first) <= (adjusted_datetime)} + @backend.put key: @name, value: data.to_yaml + end + + def add_record(record) + data = get_all_records + data.push({ DateTime.now.to_s => record }) + @backend.put key: @name, value: data.to_yaml + end + + def get_all_records(options={}) + return (@backend.exist?({key: @name}))? YAML::load(@backend.get({key: @name})) : [] + end + + end + + + # Log scanner and notifier class LogScanner include Splash::Constants include Splash::Config @@ -15,17 +88,11 @@ # LogScanner Constructor : initialize prometheus metrics # return [Splash::Logs::LogScanner] def initialize @logs_target = Marshal.load(Marshal.dump(get_config.logs)) @config = get_config - @registry = Prometheus::Client::Registry::new - @metric_count = Prometheus::Client::Gauge.new(:logerrors, docstring: 'SPLASH metric log error', labels: [:log ]) - @metric_missing = Prometheus::Client::Gauge.new(:logmissing, docstring: 'SPLASH metric log missing', labels: [:log ]) - @metric_lines = Prometheus::Client::Gauge.new(:loglines, docstring: 'SPLASH metric log lines numbers', labels: [:log ]) - @registry.register(@metric_count) - @registry.register(@metric_missing) - @registry.register(@metric_lines) + end # start log analyse for log target in config # @return [Hash] Exiter case :quiet_exit @@ -60,20 +127,27 @@ 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| - missing = (item[:status] == :missing)? 1 : 0 - log.item "Sending metrics for #{item[:log]}", session - @metric_count.set(item[:count], labels: { log: item[:log] }) - @metric_missing.set(missing, labels: { log: item[:log] }) + logsrec = LogsRecords::new item[:label] + errors = (item[:count])? item[:count] : 0 lines = (item[:lines])? item[:lines] : 0 - @metric_lines.set(lines, labels: { log: item[:log] }) + 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 - hostname = Socket.gethostname - url = @config.prometheus_pushgateway_url - Prometheus::Client::Push.new('Splash',hostname, url).add(@registry) - log.ok "Sending to Prometheus PushGateway done.", session return {:case => :quiet_exit } end end end