Module: Sensu::Plugin::Utils

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

Instance Method Summary collapse

Instance Method Details

#config_filesObject



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

def config_files
  if 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



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

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

#load_config(filename) ⇒ Object



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

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

#net_http_req_class(method) ⇒ Object



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

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



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

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

#settingsObject



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

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