Module: Sensu::Plugin::Utils

Included in:
Handler, Mutator
Defined in:
lib/sensu-plugin/utils.rb

Instance Method Summary collapse

Instance Method Details

#cast_bool_values_int(value) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/sensu-plugin/utils.rb', line 63

def cast_bool_values_int(value)
  case value
  when 'true', true
    1
  when 'false', false
    0
  else
    value
  end
end

#config_filesObject



6
7
8
9
10
11
12
13
14
# File 'lib/sensu-plugin/utils.rb', line 6

def config_files
  if ENV['SENSU_LOADED_TEMPFILE'] && File.file?(ENV['SENSU_LOADED_TEMPFILE'])
    IO.read(ENV['SENSU_LOADED_TEMPFILE']).split(':')
  elsif ENV['SENSU_CONFIG_FILES']
    ENV['SENSU_CONFIG_FILES'].split(':')
  else
    ['/etc/sensu/config.json'] + Dir['/etc/sensu/conf.d/**/*.json']
  end
end

#deep_merge(hash_one, hash_two) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sensu-plugin/utils.rb', line 49

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

#load_config(filename) ⇒ Object



16
17
18
19
20
# File 'lib/sensu-plugin/utils.rb', line 16

def load_config(filename)
  JSON.parse(File.open(filename, 'r').read)
rescue
  {}
end

#net_http_req_class(method) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sensu-plugin/utils.rb', line 36

def net_http_req_class(method)
  case method.to_s.upcase
  when 'GET'
    Net::HTTP::Get
  when 'POST'
    Net::HTTP::Post
  when 'DELETE'
    Net::HTTP::Delete
  when 'PUT'
    Net::HTTP::Put
  end
end

#read_event(file) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/sensu-plugin/utils.rb', line 26

def read_event(file)
  @event = ::JSON.parse(file.read)
  @event['occurrences'] ||= 1
  @event['check']       ||= {}
  @event['client']      ||= {}
rescue => e
  puts 'error reading event: ' + e.message
  exit 0
end

#settingsObject



22
23
24
# File 'lib/sensu-plugin/utils.rb', line 22

def settings
  @settings ||= config_files.map { |f| load_config(f) }.reduce { |a, b| deep_merge(a, b) }
end