Module: Sensu::Utilities

Included in:
Client::HTTPSocket, Daemon
Defined in:
lib/sensu/utilities.rb

Instance Method Summary collapse

Instance Method Details

#check_subdued?(check) ⇒ TrueClass, FalseClass

Determine if a check is subdued, by conditions set in the check definition. If any of the conditions are true, without an exception, the check is subdued.

Parameters:

  • check (Hash)

    definition.

Returns:

  • (TrueClass, FalseClass)


178
179
180
181
182
183
184
# File 'lib/sensu/utilities.rb', line 178

def check_subdued?(check)
  if check[:subdue]
    in_time_windows?(check[:subdue])
  else
    false
  end
end

#deep_merge(hash_one, hash_two) ⇒ Hash

Deep merge two hashes. Nested hashes are deep merged, arrays are concatenated and duplicate array items are removed.

Parameters:

  • hash_one (Hash)
  • hash_two (Hash)

Returns:

  • (Hash)

    deep merged hash.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sensu/utilities.rb', line 32

def deep_merge(hash_one, hash_two)
  merged = hash_one.dup
  hash_two.each do |key, value|
    merged[key] = case
    when hash_one[key].is_a?(Hash) && value.is_a?(Hash)
      deep_merge(hash_one[key], value)
    when hash_one[key].is_a?(Array) && value.is_a?(Array)
      hash_one[key].concat(value).uniq
    else
      value
    end
  end
  merged
end

#find_attribute_value(tree, path, default) ⇒ Object

Traverse a hash for an attribute value, with a fallback default value if nil.

Parameters:

  • tree (Hash)

    to traverse.

  • path (Array)

    of attribute keys.

  • default (Object)

    value if attribute value is nil.

Returns:

  • (Object)

    attribute or fallback default value.



91
92
93
94
95
96
97
98
# File 'lib/sensu/utilities.rb', line 91

def find_attribute_value(tree, path, default)
  attribute = tree[path.shift]
  if attribute.is_a?(Hash)
    find_attribute_value(attribute, path, default)
  else
    attribute.nil? ? default : attribute
  end
end

#in_time_window?(condition) ⇒ TrueClass, FalseClass

Determine if the current time falls within a time window. The provided condition must have a ‘:begin` and `:end` time, eg. “11:30:00 PM”, or `false` will be returned.

Parameters:

  • condition (Hash)

Options Hash (condition):

  • :begin (String)

    time.

  • :end (String)

    time.

Returns:

  • (TrueClass, FalseClass)


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/sensu/utilities.rb', line 130

def in_time_window?(condition)
  if condition.has_key?(:begin) && condition.has_key?(:end)
    begin_time = Time.parse(condition[:begin])
    end_time = Time.parse(condition[:end])
    if end_time < begin_time
      if Time.now < end_time
        begin_time = Time.parse("12:00:00 AM")
      else
        end_time = Time.parse("11:59:59 PM")
      end
    end
    Time.now >= begin_time && Time.now <= end_time
  else
    false
  end
end

#in_time_windows?(conditions) ⇒ TrueClass, FalseClass

Determine if time window conditions for one or more days of the week are met. If a day of the week is provided, it can provide one or more conditions, each with a ‘:begin` and `:end` time, eg. “11:30:00 PM”, or `false` will be returned.

Parameters:

  • conditions (Hash)

Options Hash (conditions):

  • :days (String)

    of the week.

Returns:

  • (TrueClass, FalseClass)


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

def in_time_windows?(conditions)
  in_window = false
  window_days = conditions[:days] || {}
  if window_days[:all]
    in_window = window_days[:all].any? do |condition|
      in_time_window?(condition)
    end
  end
  current_day = Time.now.strftime("%A").downcase.to_sym
  if !in_window && window_days[current_day]
    in_window = window_days[current_day].any? do |condition|
      in_time_window?(condition)
    end
  end
  in_window
end

#random_uuidString

Generate a random universally unique identifier.

Returns:

  • (String)

    random UUID.



50
51
52
# File 'lib/sensu/utilities.rb', line 50

def random_uuid
  ::SecureRandom.uuid
end

#redact_sensitive(hash, keys = nil) ⇒ Hash

Remove sensitive information from a hash (eg. passwords). By default, hash values will be redacted for the following keys: password, passwd, pass, api_key, api_token, access_key, secret_key, private_key, secret

Parameters:

  • hash (Hash)

    to redact sensitive value from.

  • keys (Array) (defaults to: nil)

    that indicate sensitive values.

Returns:

  • (Hash)

    hash with redacted sensitive values.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sensu/utilities.rb', line 62

def redact_sensitive(hash, keys=nil)
  keys ||= %w[
    password passwd pass
    api_key api_token
    access_key secret_key private_key
    secret
  ]
  hash = hash.dup
  hash.each do |key, value|
    if keys.include?(key.to_s)
      hash[key] = "REDACTED"
    elsif value.is_a?(Hash)
      hash[key] = redact_sensitive(value, keys)
    elsif value.is_a?(Array)
      hash[key] = value.map do |item|
        item.is_a?(Hash) ? redact_sensitive(item, keys) : item
      end
    end
  end
  hash
end

#retry_until_true(wait = 0.5, &block) ⇒ Object

Retry a code block until it retures true. The first attempt and following retries are delayed.

Parameters:

  • wait (Numeric) (defaults to: 0.5)

    time to delay block calls.

  • block (Proc)

    to call that needs to return true.



18
19
20
21
22
23
24
# File 'lib/sensu/utilities.rb', line 18

def retry_until_true(wait=0.5, &block)
  EM::Timer.new(wait) do
    unless block.call
      retry_until_true(wait, &block)
    end
  end
end

#substitute_tokens(tokens, attributes) ⇒ Array

Substitute dot notation tokens (eg. :::db.name|production:::) with the associated definition attribute value. Tokens can provide a fallback default value, following a pipe.

Parameters:

  • tokens (String)
  • attributes (Hash)

Returns:

  • (Array)

    containing the string with tokens substituted and an array of unmatched tokens.



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/sensu/utilities.rb', line 108

def substitute_tokens(tokens, attributes)
  unmatched_tokens = []
  substituted = tokens.gsub(/:::([^:].*?):::/) do
    token, default = $1.to_s.split("|", -1)
    path = token.split(".").map(&:to_sym)
    matched = find_attribute_value(attributes, path, default)
    if matched.nil?
      unmatched_tokens << token
    end
    matched
  end
  [substituted, unmatched_tokens]
end

#testing?TrueClass, FalseClass

Determine if Sensu is being tested, using the process name. Sensu is being test if the process name is “rspec”,

Returns:

  • (TrueClass, FalseClass)


9
10
11
# File 'lib/sensu/utilities.rb', line 9

def testing?
  File.basename($0) == "rspec"
end