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.



89
90
91
# File 'lib/ostrichpoll/ostrich_validator.rb', line 89

def exit_code
  @exit_code
end

#host_instanceObject

Returns the value of attribute host_instance.



83
84
85
# File 'lib/ostrichpoll/ostrich_validator.rb', line 83

def host_instance
  @host_instance
end

#metricObject

Returns the value of attribute metric.



85
86
87
# File 'lib/ostrichpoll/ostrich_validator.rb', line 85

def metric
  @metric
end

#missingObject

Returns the value of attribute missing.



88
89
90
# File 'lib/ostrichpoll/ostrich_validator.rb', line 88

def missing
  @missing
end

#normal_rangeObject

Returns the value of attribute normal_range.



87
88
89
# File 'lib/ostrichpoll/ostrich_validator.rb', line 87

def normal_range
  @normal_range
end

#rateObject

Returns the value of attribute rate.



86
87
88
# File 'lib/ostrichpoll/ostrich_validator.rb', line 86

def rate
  @rate
end

Instance Method Details

#check(value) ⇒ Object



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
164
165
166
167
168
169
170
171
172
173
# File 'lib/ostrichpoll/ostrich_validator.rb', line 106

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

  # error on missing value unless we ignore missing
  unless value
    unless missing == :ignore
      Log.warn "#{metric}: value missing, treating as error; exit code #{exit_code}"
      return 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 false
    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 false
    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}"
      return exit_code
    end

    if hi && value > hi
      Log.warn "#{metric}: read value #{value} is above normal range maximum #{hi}; exit code #{exit_code}"
      return exit_code
    end

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

  false
end

#initObject



91
92
93
94
95
# File 'lib/ostrichpoll/ostrich_validator.rb', line 91

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

#verify!Object



97
98
99
100
101
102
103
104
# File 'lib/ostrichpoll/ostrich_validator.rb', line 97

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 normal_range
    Log.warn "Invalid normal range: #{normal_range.inspect}" unless normal_range.is_a? Array
  end
end