Module: Nagios::MkLiveStatus::Parser

Includes:
Nagios::MkLiveStatus, QueryHelper
Defined in:
lib/nagios_mklivestatus/parser.rb

Overview

This module can be used to parse a nagios string query in order to convert it into a Nagios::MkLiveStatus::Query

Author

Esco-lan Team ([email protected])

Copyright

Copyright © 2012 GIP RECIA

License

General Public Licence

Constant Summary

Constants included from QueryHelper::Trigger

QueryHelper::Trigger::ALL, QueryHelper::Trigger::CHECK, QueryHelper::Trigger::COMMAND, QueryHelper::Trigger::COMMENT, QueryHelper::Trigger::DOWNTIME, QueryHelper::Trigger::LOG, QueryHelper::Trigger::STATE

Constants included from QueryHelper::Deviation

QueryHelper::Deviation::AVG, QueryHelper::Deviation::AVGINV, QueryHelper::Deviation::MAX, QueryHelper::Deviation::MIN, QueryHelper::Deviation::STD, QueryHelper::Deviation::SUM, QueryHelper::Deviation::SUMINV

Constants included from QueryHelper::Comparator

QueryHelper::Comparator::EQUAL, QueryHelper::Comparator::EQUAL_IGNCASE, QueryHelper::Comparator::GREATER, QueryHelper::Comparator::GREATER_EQUAL, QueryHelper::Comparator::LESSER, QueryHelper::Comparator::LESSER_EQUAL, QueryHelper::Comparator::NOT_EQUAL, QueryHelper::Comparator::NOT_EQUAL_IGNCASE, QueryHelper::Comparator::NOT_SUBSTR, QueryHelper::Comparator::NOT_SUBSTR_IGNCASE, QueryHelper::Comparator::SUBSTR, QueryHelper::Comparator::SUBSTR_IGNCASE

Instance Method Summary collapse

Methods included from QueryHelper

#nagmk_and, #nagmk_filter, #nagmk_filter_from_str, #nagmk_negate, #nagmk_or, #nagmk_query, #nagmk_stats, #nagmk_stats_and, #nagmk_stats_from_str, #nagmk_stats_or, #nagmk_wait_condition, #nagmk_wait_condition_and, #nagmk_wait_condition_from_str, #nagmk_wait_condition_negate, #nagmk_wait_condition_object, #nagmk_wait_condition_or, #nagmk_wait_condition_timeout, #nagmk_wait_condition_trigger

Methods included from QueryHelper::Trigger

#get_all_triggers

Methods included from QueryHelper::Deviation

#get_all_deviations

Methods included from QueryHelper::Comparator

#get_all_comparators

Methods included from Nagios::MkLiveStatus

init, #logger

Instance Method Details

#nagmk_parse(query_str, with_opts = false) ⇒ Object

Parser method, takes the string and, optionally, a boolean to add opts And return a Nagios::MkLiveStatus::Query and query options (if asked)



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
156
157
158
159
160
161
162
163
# File 'lib/nagios_mklivestatus/parser.rb', line 16

def nagmk_parse(query_str, with_opts=false)
  
  if not query_str.is_a? String or query_str.empty?
    raise QueryException.new("The query is not valid. You must provide a valid string.")
  end
  
  query = nagmk_query()
  nagios_opts = {}
  
  #split all the lines in order to create a table of string commands
  commands = query_str.split("\n")
  
  # the first line is the GET of the query
  get = commands.shift.strip.match(/^GET (.*)$/)
  if get == nil
    raise QueryException.new("The query has no GET. The first line must match /^GET (.*)$/.")
  else
    query.get get[1]
  end
  
  columns_parsed=false
  query_parsed=false
  filter_parsed=false
  wait_before=false
  stats = []
  columns = []
  filters = []
  waits = []
  
  commands.each do |command|
    command.strip!
    logger.debug("> processing command \"#{command}\"")
    
    #if end query found, columns must be parsed
    if query_parsed and not columns_parsed
      columns_parsed = true
    end
    
    if command.match(/^Columns: /)
      
      if query_parsed
        raise QueryException.new("The query options must be defined after columns : #{command}")
      end
      
      if not columns_parsed
        logger.debug(">> columns found \"#{command.match(/^Columns: (.*)$/)[1]}\"")
        columns = command.match(/^Columns: (.*)$/)[1].split(" ")
        columns_parsed = true
      else
        raise QueryException.new("The definitions of columns are encountered twice. Or after the filter or waits predicates.")
      end
    elsif command.match(/^Stats(And|Or)?: /)
      
      if columns_parsed
        raise QueryException.new("The stats predicates must be defined before columns, filters and waits : #{command}")
      end
      
      if query_parsed
        raise QueryException.new("The query options must be defined after stats : #{command}")
      end
      
      stats_dispatching(command, stats)
      
    elsif command.match(/^(Filter: |And: |Or: |Negate:)/)
      
      if not columns_parsed
        columns_parsed = true
      end
      
      if query_parsed
        raise QueryException.new("The query options must be defined after filters : #{command}")
      end
      
      filter_dispatching(command, filters)
      filter_parsed = true
      
    elsif command.match(/^Wait(.*): /)
      
      if not columns_parsed
        columns_parsed = true
      end
      
      if query_parsed
        raise QueryException.new("The query options must be defined after waits : #{command}")
      end
      
      if filter_parsed and wait_before
        raise QueryException.new("The Query cannot have wait expression before and after filters : #{command}")
      elsif not filter_parsed
        wait_before=true
      end
      
      wait_dispatching(command, waits)
      
    elsif command.match(/^AuthUser: (.+)$/)
      query_parsed = true
      nagios_opts[:user] = command.match(/^AuthUser: (.+)$/)[1]
      
    elsif command.match(/^ColumnHeaders: (on|off)$/)
      query_parsed = true
      nagios_opts[:column_headers] = command.match(/^ColumnHeaders: (on|off)$/)[1] == "on"
      
    elsif command.match(/^Limit: (\d+)$/)
      query_parsed = true
      nagios_opts[:limit] = command.match(/^Limit: (\d+)$/)[1].to_i
    
    elsif command.match(/^Localtime: (\d+)$/)
      query_parsed = true
      nagios_opts[:local_time] = command.match(/^Localtime: (\d+)$/)[1].to_i
      
    elsif command.match(/^OutputFormat: (json|python)$/)
      query_parsed = true
      nagios_opts[:output] = command.match(/^OutputFormat: (json|python)$/)[1]
      
    elsif not command.empty?
      raise QueryException.new("The current query is not valid due to : #{command}")
    end
    
  end
  
  columns.each do |column|
    query.addColumn column
  end
  
  filters.each do |filter|
    query.addFilter filter
  end
  
  stats.each do |stat|
    query.addStats stat
  end
  
  if wait_before
    waits.each do |wait|
      query.addWaitBefore wait
    end
  else
    waits.each do |wait|
      query.addWaitAfter wait
    end
  end
  
  if with_opts
    return query, nagios_opts
  else
    return query
  end
end