lib/nagira/background_parse.rb in nagira-0.5.0 vs lib/nagira/background_parse.rb in nagira-0.5.1
- old
+ new
@@ -1,31 +1,123 @@
-require 'nagios'
-
-module Nagios
+class Nagira < Sinatra::Base
##
# Background parsing of status.dat file in separate thread. Runs on
- # regular intervals slightly shorter than :ttl
+ # regular intervals defined by :ttl
#
class BackgroundParser
+ include Singleton
- ##
- #
- # If :ttl is not defined set to 0 and do not run
- # background parsing.
- #
def initialize
- interval = [::DEFAULT[:ttl],1].max || nil
- $use_inflight_status = false
- $use_inflight_objects = false
- if interval && ::DEFAULT[:start_background_parser]
- puts "[#{Time.now}] Starting background parser thread with interval #{interval} sec"
- $bg = Thread.new {
- loop {
- $use_inflight_status ? $nagios[:status].parse : $nagios[:status_inflight].parse
- $use_inflight_status = !$use_inflight_status
- sleep interval
- } #loop
- } # thread
+ @use_inflight_flag = false
+ end
+
+ # For large Nagios files there's a significant time required for
+ # the parsing, if HTTP request comes during the parsing, data
+ # could be missing. To prevent this from happening flag variable
+ # defines two sets of the parsed data, which are parsed at
+ # different sequential runs of the parser.
+ attr_accessor :use_inflight_flag
+
+ class << self
+
+ ##
+ # Target data structure (i.e. $nagios hash for example) which is
+ # updated by BackgroundParser.
+ #
+ def target
+ @target ||= Parser.state
end
+
+ ##
+ # \@ttl (Fixint, seconds) defines re-parsing interval for the
+ # BackgroundParser.
+ #
+ # Set @@ttl after initialization, to be able to pass
+ # configuration variables.
+ #
+ # @see start=
+ #
+ # Example:
+ # Nagios::BackgroundParser.ttl = ::DEFAULT[:ttl].to_i
+ # Nagios::BackgroundParser.start = ::DEFAULT[:start_background_parser]
+ # Nagios::BackgroundParser.run
+ #
+ def ttl= ttl
+ @ttl = ttl
+ end
+
+ ##
+ # \@start (Boolean) defines whether BackgroundParser should be
+ # started.
+ #
+ # Set :start variable after initialization, to be able to pass
+ # configuration values.
+ #
+ # @see ttl=
+ #
+ # Example:
+ # Nagios::BackgroundParser.ttl = ::DEFAULT[:ttl].to_i
+ # Nagios::BackgroundParser.start = ::DEFAULT[:start_background_parser]
+ # Nagios::BackgroundParser.run
+ #
+ def start= start
+ @start = start
+ end
+
+ ##
+ # Is BackgroundParser configured to run?
+ def configured?
+ @ttl > 0 && @start
+ end
+
+ ##
+ # Is BG parser thread running
+ #
+ def alive?
+ !@bg.nil? && @bg.alive?
+ end
+
+ ##
+ # See alive?
+ def dead?
+ !alive?
+ end
+
+
+ ##
+ # Start BG Parser if it's configured to run and TTL is defined
+ def run
+ if configured? && dead?
+
+ Logger.log "Starting background parser thread with interval #{@ttl} sec"
+
+ target.status_inflight = Nagios::Status.new(target[:status].path)
+ target.objects_inflight = Nagios::Objects.new(target[:objects].path)
+
+ Parser.parse [:status_inflight,:objects_inflight]
+
+ @bg = Thread.new {
+ loop {
+ target[with_inflight?(:status)].parse
+ target[with_inflight?(:objects)].parse
+ sleep @ttl
+ @use_inflight_flag = !@use_inflight_flag
+ }
+ }
+ end
+ end
+
+ def inflight?
+ @use_inflight_flag
+ end
+ ##
+ # Construct file symbol, based on in flight status.
+ # @see run
+ def with_inflight?(file)
+ (inflight? ? "#{file}_inflight" : file).to_sym
+ end
+
end
end
end
+
+# LocalWords: ttl BackgroundParser config param Fixint