Class: Nagios::MkLiveStatus::Query

Inherits:
Object
  • Object
show all
Includes:
Nagios::MkLiveStatus
Defined in:
lib/nagios_mklivestatus/query.rb

Overview

This class is used to create a nagios mklivestatus query

  • get : is the object we are looking for

  • columns : are the fields we need from the object

  • filters : are the filters applied to the query

  • stats : are the stats applied to the query

  • wait : are the wait applied to the query

By default the query get all the columns of the object without filter. If you add any objects (columns, filter, wait, …) they will take place in the query

Author

Esco-lan Team ([email protected])

Copyright

Copyright © 2011 GIP RECIA

License

General Public Licence

Instance Method Summary collapse

Methods included from Nagios::MkLiveStatus

init, #logger

Constructor Details

#initialize(base = nil) ⇒ Query

Constructor of the Query instance base is by default nil but if its filled the base if associated to the GET of the query



26
27
28
# File 'lib/nagios_mklivestatus/query.rb', line 26

def initialize(base=nil)
  get(base)
end

Instance Method Details

#addColumn(name) ⇒ Object

Add a field to get in the query for the GET object



38
39
40
41
42
43
44
45
46
# File 'lib/nagios_mklivestatus/query.rb', line 38

def addColumn(name)
  if not name == nil or name.empty?
    if @columns == nil
      @columns=Array.new
    end
    
    @columns.push(name)
  end
end

#addFilter(expression) ⇒ Object

Add a filter to the query



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/nagios_mklivestatus/query.rb', line 71

def addFilter(expression)
  if expression.is_a? Nagios::MkLiveStatus::Filter
    if @filters == nil
      @filters=Array.new
    end
    
    @filters.push(expression)
  else
    raise QueryException.new("The filter must be a filter expression.")
  end
end

#addStats(expression) ⇒ Object

Add a stat filter



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/nagios_mklivestatus/query.rb', line 84

def addStats(expression)
  if expression.is_a? Nagios::MkLiveStatus::Stats
    if @stats == nil
      @stats=Array.new
    end
    
    @stats.push(expression)
  else
    raise QueryException.new("The filter must be a stat expression.")
  end
end

#addWaitAfter(expression) ⇒ Object

add wait to query but after filter predicats



60
61
62
63
64
65
66
67
68
# File 'lib/nagios_mklivestatus/query.rb', line 60

def addWaitAfter(expression)
  if expression.is_a? Nagios::MkLiveStatus::Wait
    if @after_wait == nil
      @after_wait = []
    end
    
    wait_adding expression, @after_wait
  end
end

#addWaitBefore(expression) ⇒ Object

add wait to query but before filter predicats



49
50
51
52
53
54
55
56
57
# File 'lib/nagios_mklivestatus/query.rb', line 49

def addWaitBefore(expression)
  if expression.is_a? Nagios::MkLiveStatus::Wait
    if @before_wait == nil
      @before_wait = []
    end
    
    wait_adding expression, @before_wait
  end
end

#get(get) ⇒ Object

Get method is used to set the object to search for in nagios mklivestatus



31
32
33
34
35
# File 'lib/nagios_mklivestatus/query.rb', line 31

def get(get)
  if get != nil and not get.empty?
    @get = get
  end
end

#get_columns_nameObject

get all the column name of the query



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/nagios_mklivestatus/query.rb', line 155

def get_columns_name
  columns = []
  if @columns != nil
    @columns.each do |column|
      columns.push column
    end
  end
    
  if @stats != nil and @stats.length > 0
    @stats.each do |stat|
      columns.push stat.to_column_name
    end
  end
    
  return columns
end

#has_statsObject

check if the query have stats predicates



148
149
150
# File 'lib/nagios_mklivestatus/query.rb', line 148

def has_stats
  @stats != nil and @stats.length > 0
end

#to_sObject

short cut to the method to_socket



97
98
99
# File 'lib/nagios_mklivestatus/query.rb', line 97

def to_s
  return to_socket
end

#to_socketObject

method used to generate the query from the field if get is not set then the method return an empty string



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
# File 'lib/nagios_mklivestatus/query.rb', line 103

def to_socket
  query = String.new
  if @get != nil
    query << "GET "+@get+"\n"
    
    if @stats != nil and @stats.length > 0
      @stats.each do |stat|
        query << stat.to_s+"\n"
      end
    end
    
    if @columns != nil and @columns.length > 0
      query << "Columns:"
      @columns.each do |column|
        query << " "+column
      end
      query << "\n"
    end
    
    if @before_wait != nil and @before_wait.length > 0
      @before_wait.each do |wait|
        query << wait.to_s+"\n"
      end
    end
    
    if @filters != nil and @filters.length > 0
      @filters.each do |filter|
        query << filter.to_s+"\n"
      end
    end
    
    if @after_wait != nil and @after_wait.length > 0
      @after_wait.each do |wait|
        query << wait.to_s+"\n"
      end
    end
    
  end
  
  return query
end