Class: Nagira::BackgroundParser

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
app/parsers/background_parse.rb

Overview

Background parsing of status.dat file in separate thread. Runs on regular intervals defined by :ttl

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBackgroundParser

Returns a new instance of BackgroundParser.



9
10
11
# File 'app/parsers/background_parse.rb', line 9

def initialize
  @use_inflight_flag = false
end

Instance Attribute Details

#use_inflight_flagObject

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.


18
19
20
# File 'app/parsers/background_parse.rb', line 18

def use_inflight_flag
  @use_inflight_flag
end

Class Method Details

.alive?Boolean

Is BG parser thread running

Returns:

  • (Boolean)


75
76
77
# File 'app/parsers/background_parse.rb', line 75

def alive?
  !@bg.nil? && @bg.alive?
end

.configured?Boolean

Is BackgroundParser configured to run?

Returns:

  • (Boolean)


68
69
70
# File 'app/parsers/background_parse.rb', line 68

def configured?
  @ttl > 0 && @start
end

.dead?Boolean

See alive?

Returns:

  • (Boolean)


81
82
83
# File 'app/parsers/background_parse.rb', line 81

def dead?
  !alive?
end

.inflight?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'app/parsers/background_parse.rb', line 109

def inflight?
  @use_inflight_flag
end

.runObject

Start BG Parser if it’s configured to run and TTL is defined



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/parsers/background_parse.rb', line 88

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

.start=(start) ⇒ Object

@start (Boolean) defines whether BackgroundParser should be started.

Set :start variable after initialization, to be able to pass

configuration values.

Example:

Nagios::BackgroundParser.ttl = ::DEFAULT[:ttl].to_i
Nagios::BackgroundParser.start = ::DEFAULT[:start_background_parser]
Nagios::BackgroundParser.run

See Also:



62
63
64
# File 'app/parsers/background_parse.rb', line 62

def start= start
  @start = start
end

.targetObject

Target data structure (i.e. $nagios hash for example) which is updated by BackgroundParser.



26
27
28
# File 'app/parsers/background_parse.rb', line 26

def target
  @target ||= Parser.state
end

.ttl=(ttl) ⇒ Object

@ttl (Fixint, seconds) defines re-parsing interval for the BackgroundParser.

Set @@ttl after initialization, to be able to pass

configuration variables.

Example:

Nagios::BackgroundParser.ttl = ::DEFAULT[:ttl].to_i
Nagios::BackgroundParser.start = ::DEFAULT[:start_background_parser]
Nagios::BackgroundParser.run

See Also:



44
45
46
# File 'app/parsers/background_parse.rb', line 44

def ttl= ttl
  @ttl = ttl
end

.with_inflight?(file) ⇒ Boolean

Construct file symbol, based on in flight status.

Returns:

  • (Boolean)

See Also:



115
116
117
# File 'app/parsers/background_parse.rb', line 115

def with_inflight?(file)
  (inflight? ? "#{file}_inflight" : file).to_sym
end