Class: OstrichPoll::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/ostrichpoll/ostrich_validator.rb

Overview

this is a pretty weak and limiting definition of a validator, but it’s quick to develop and clear how to extend

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#exit_codeObject

Returns the value of attribute exit_code.



138
139
140
# File 'lib/ostrichpoll/ostrich_validator.rb', line 138

def exit_code
  @exit_code
end

#exit_messageObject

Returns the value of attribute exit_message.



139
140
141
# File 'lib/ostrichpoll/ostrich_validator.rb', line 139

def exit_message
  @exit_message
end

#host_instanceObject

Returns the value of attribute host_instance.



131
132
133
# File 'lib/ostrichpoll/ostrich_validator.rb', line 131

def host_instance
  @host_instance
end

#metricObject

Returns the value of attribute metric.



133
134
135
# File 'lib/ostrichpoll/ostrich_validator.rb', line 133

def metric
  @metric
end

#missingObject

Returns the value of attribute missing.



137
138
139
# File 'lib/ostrichpoll/ostrich_validator.rb', line 137

def missing
  @missing
end

#normal_rangeObject

Returns the value of attribute normal_range.



136
137
138
# File 'lib/ostrichpoll/ostrich_validator.rb', line 136

def normal_range
  @normal_range
end

#rateObject

Returns the value of attribute rate.



134
135
136
# File 'lib/ostrichpoll/ostrich_validator.rb', line 134

def rate
  @rate
end

#regexObject

Returns the value of attribute regex.



135
136
137
# File 'lib/ostrichpoll/ostrich_validator.rb', line 135

def regex
  @regex
end

Instance Method Details

#check(value) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/ostrichpoll/ostrich_validator.rb', line 162

def check (value)
  Log.debug "#{host_instance.url} | Given: #{metric}=#{value}"

  if !value.is_a? Float
    value = Float(value) rescue nil
  end

  # error on missing value unless we ignore missing
  unless value
    unless missing == :ignore
      error_msg = "#{metric}: value missing"
      Log.warn "#{error_msg}, treating as error; exit code #{exit_code}"
      return ExitStatus.new(error_msg, exit_code)
    else
      Log.debug "#{host_instance.url} |   missing value, but set to ignore"
      # not an error, but you can't check anything else
      return nil
    end
  end

  # compute rate
  if rate
    timestamp, previous = host_instance.previous_reading(metric)

    if previous
      Log.debug "#{host_instance.url} |   last seen: #{previous} @ #{timestamp}"

      # change since last measure
      value -= previous

      # divide by seconds elapsed
      value /= Time.now.to_i - timestamp

      Log.debug "#{host_instance.url} |   computed rate: #{value}"

    else
      # let it pass
      Log.info "#{metric}: no previous reading for rate"
      return nil
    end
  end

  # ensure value is within normal range
  if normal_range
    Log.debug "#{host_instance.url} |   normal range: #{normal_range.inspect}"
    case normal_range.size
      when 1 # max
        hi = normal_range.first

      when 2 # min
        lo = normal_range.first
        hi = normal_range.last

      else
        # whatever, ignore
        # the yaml deserializer shouldn't let this happen
    end

    if lo && value < lo
      Log.warn "#{metric}: read value #{value} is below normal range minimum #{lo}; exit code #{exit_code}; exit message '#{exit_message}'"
      return ExitStatus.new(exit_message, exit_code)
    end

    if hi && value > hi
      Log.warn "#{metric}: read value #{value} is above normal range maximum #{hi}; exit code #{exit_code}; exit message '#{exit_message}'"
      return ExitStatus.new(exit_message, exit_code)
    end

    Log.debug "#{host_instance.url} |   within normal range"
  end

  nil
end

#initObject



141
142
143
144
145
146
147
# File 'lib/ostrichpoll/ostrich_validator.rb', line 141

def init
  @rate = false
  @regex = false
  @exit_code = 1
  @exit_message = ""
  @missing = :ignore
end

#verify!Object



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/ostrichpoll/ostrich_validator.rb', line 149

def verify!
  Log.warn "Invalid metric #{metric.inspect}" unless metric.is_a? String
  Log.warn "Invalid exit code: #{exit_code.inspect}" unless exit_code.is_a? Integer

  if exit_message
    Log.warn "Invalid exit message: #{exit_message.inspect}" unless exit_message.is_a? String
  end

  if normal_range
    Log.warn "Invalid normal range: #{normal_range.inspect}" unless normal_range.is_a? Array
  end
end