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_validation_names_by_regex(tree, key) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ostrichpoll/ostrich_validator.rb', line 95

def find_validation_names_by_regex(tree, key)
  split_key = split_on_slash(key, 2)
  selector = split_key.first

  stat_name_matches = []
  regexp = /#{selector}/

  tree.each do |k, v|
    if regexp.match(k) do
        if v.kind_of? Hash
          find_validation_names_by_regex(v, split_key.last).each do |s|
            stat_name_matches << "#{k}/#{s}"
          end
        elsif split_key.size == 1 #This is the last match
          stat_name_matches << k
        end
      end
    end
  end
  stat_name_matches
end

#find_value(map, key) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/ostrichpoll/ostrich_validator.rb', line 117

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

  tree
end

#previous_reading(key) ⇒ Object



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

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

#split_on_slash(key, *limit) ⇒ Object

split on a slash, unless it’s escaped



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

def split_on_slash(key, *limit)
  key.split(/(?<!\\)\//, *limit).map do |str|
      str.sub('\\', '')
  end
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ostrichpoll/ostrich_validator.rb', line 23

def validate
  uri = URI.parse url
  response = Net::HTTP.get uri rescue (
    error_msg = "Unable to connect to host #{uri}"
    Log.error error_msg
    return ExitStatus.new(error_msg, 1)
  )

  # parse response
  json = JSON.parse(response) rescue (
    error_msg = "Invalid JSON response: #{response}"
    Log.error error_msg
    return ExitStatus.new(error_msg, 1)
  )

  @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
  
  retval = nil

  # execute each validation:
  if validations
    matched_validations = []
    validations.each do |v|
      if(v.regex)
        find_validation_names_by_regex(json, v.metric).each do |n|
          matched_validator = v.clone
          matched_validator.metric = n
          matched_validations << matched_validator
        end
      else
        matched_validations << v
      end
    end

    matched_validations.each do |v|
      value = v.check(find_value(json, v.metric))
      retval = value if retval.nil?
    end
  end

  retval
end