Class: Nagira::Parser

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

Overview

Singleton class, that handles parsing of the Nagios data. This class uses another singleton class BackgroundParser for repeating parsing of the files in background thread.

Example usage:

Parser.config   = < path to nagios.cfg file>
Parser.status   = < path to status.cfg file>
Parser.objects  = < path to object_cache file>

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



15
16
17
# File 'app/parsers/parser.rb', line 15

def initialize
  @state = OpenStruct.new
end

Instance Attribute Details

#stateObject

State structure keep all the Nagios parsed state information for :objects, :status, :config, etc. as well as “inflight” data.



21
22
23
# File 'app/parsers/parser.rb', line 21

def state
  @state
end

Class Method Details

.configObject

Return parsed configuration.



67
68
69
# File 'app/parsers/parser.rb', line 67

def config
  state.config
end

.config=(config) ⇒ Object

Configuration object of the Nagios and Nagira parser. Create new configuration and parse it at the time of creation.



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

def config=(config)
  state.config = Nagios::Config.new(config)
  state.config.parse
end

.inflight?Boolean

Detect which half of the data should be returned. There’s a pissiblility that during request data are bing parsed, which can result in incomplete or broken data. This ensures, that only data that are not being parsed now returned.

Returns:

  • (Boolean)


29
30
31
# File 'app/parsers/parser.rb', line 29

def inflight?
  BackgroundParser.inflight?
end

.objectsObject



94
95
96
97
98
# File 'app/parsers/parser.rb', line 94

def objects
  state
    .send(with_inflight?(:objects))
    .objects || []
end

.objects=(objects_file) ⇒ Object

Create new data structure for parsed object_cache file information.

Parameters:

  • objects_file (String)

    PATH to the file



90
91
92
# File 'app/parsers/parser.rb', line 90

def objects=(objects_file)
  state.objects = Nagios::Objects.new(objects_file)
end

.parse(files = %i{config objects status}) ⇒ Object

If BackgroundParser is not running, then parse files, otherwise do nothing, as the data are already parsed by the BG.



50
51
52
53
54
55
# File 'app/parsers/parser.rb', line 50

def parse(files = %i{config objects status})
  return if BackgroundParser.alive?
  files
    .map { |f| state.send(f) }
    .map(&:parse)
end

.stateObject

Return state object



42
43
44
# File 'app/parsers/parser.rb', line 42

def state
  instance.state
end

.statusObject

Return parsed hosts status. Depending on the inflight flag return either “A” or “B” set of data.



80
81
82
83
84
# File 'app/parsers/parser.rb', line 80

def status
  state
    .send(with_inflight?(:status))
    .status
end

.status=(status_file) ⇒ Object

Create new data structure for the host status data

Parameters:

  • status_file (String)

    PATH to the file



74
75
76
# File 'app/parsers/parser.rb', line 74

def status=(status_file)
  state.status = Nagios::Status.new(status_file)
end

.with_inflight?(file) ⇒ Boolean

Construct file symbol, based on in flight status.

Returns:

  • (Boolean)

See Also:



37
38
39
# File 'app/parsers/parser.rb', line 37

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