Class: SensuHandler

Inherits:
Chef::Handler
  • Object
show all
Defined in:
lib/chef-sensu-handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(status = :warning) ⇒ SensuHandler



13
14
15
# File 'lib/chef-sensu-handler.rb', line 13

def initialize(status=:warning)
  @return_status = STATUS[status]
end

Instance Method Details

#reportObject



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

def report
  msg = { 'name' => 'check_chef_run' }
  if run_status.success?
    msg['status'] = 0
    msg['output'] = 'OK: Chef-client ran successfully.'
  elsif run_status.failed?
    msg['status'] = @return_status
    msg['output'] = 'WARNING: Chef-client run failed!'
  else
    msg['status'] = 3
    msg['output'] = 'UNKNOWN: Not sure what happened.'
  end
  send_to_sensu msg
end

#send_to_sensu(msg) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/chef-sensu-handler.rb', line 17

def send_to_sensu(msg)
  begin
    Timeout.timeout(10) do
      s = TCPSocket.new 'localhost', SENSU_CLIENT_PORT
      s.puts msg.to_json
      s.close
    end
    Chef::Log.info("Updated Sensu with #{msg}")
  rescue Timeout::Error
    Chef::Log.error("Timed out while attempting to update Sensu.")
  rescue => error
    Chef::Log.error("Unexpected error while attemping to update Sensu: #{error}")
  end
end