Class: OstrichPoll::Host

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHost

Returns a new instance of Host.



16
17
18
# File 'lib/ostrichpoll/ostrich_validator.rb', line 16

def initialize
  validations = []
end

Instance Attribute Details

#rate_fileObject

Returns the value of attribute rate_file.



12
13
14
# File 'lib/ostrichpoll/ostrich_validator.rb', line 12

def rate_file
  @rate_file
end

#stored_timestampObject

Returns the value of attribute stored_timestamp.



21
22
23
# File 'lib/ostrichpoll/ostrich_validator.rb', line 21

def stored_timestamp
  @stored_timestamp
end

#stored_valuesObject

Returns the value of attribute stored_values.



20
21
22
# File 'lib/ostrichpoll/ostrich_validator.rb', line 20

def stored_values
  @stored_values
end

#urlObject

Returns the value of attribute url.



11
12
13
# File 'lib/ostrichpoll/ostrich_validator.rb', line 11

def url
  @url
end

#validationsObject

Returns the value of attribute validations.



13
14
15
# File 'lib/ostrichpoll/ostrich_validator.rb', line 13

def validations
  @validations
end

Instance Method Details

#find_value(map, key) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/ostrichpoll/ostrich_validator.rb', line 69

def find_value(map, key)
  tree = map
  key.split('/').each do |selector|
    return nil unless tree.kind_of? Hash
    tree = tree[selector]
  end

  tree
end

#previous_reading(key) ⇒ Object



65
66
67
# File 'lib/ostrichpoll/ostrich_validator.rb', line 65

def previous_reading(key)
  return stored_timestamp, find_value(stored_values, key)
end

#validateObject



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
# File 'lib/ostrichpoll/ostrich_validator.rb', line 23

def validate
  uri = URI.parse url
  response = Net::HTTP.get uri

  # parse response
  json = JSON.parse(response) rescue (
    Log.error "Invalid JSON response: #{response}"
    return EXIT_ERROR
  )

  @stored_values = {}
  if rate_file
    # read in rate-file
    @stored_values = YAML.load_file(rate_file) rescue (
      Log.warn "Could not parse rate file: #{rate_file}"
      {}
    )

    @stored_timestamp = stored_values['ostrichpoll.timestamp']
    unless @stored_timestamp
      Log.warn "No 'ostrichpoll.timestamp' found in rate file: #{rate_file}"
    end

    # write out new rate file
    json['ostrichpoll.timestamp'] = Time.now.to_i
    File.open(rate_file, 'w') do |f|
      f.puts json.to_yaml
    end
  end

  # execute each validations:
  retval = false
  if validations
    validations.each do |v|
      value = v.check(find_value(json, v.metric))
      retval = value unless retval
    end
  end

  retval
end