Module: Sensu::Client::CheckUtils

Included in:
HTTPSocket, Socket
Defined in:
lib/sensu/client/utils.rb

Defined Under Namespace

Classes: DataError

Instance Method Summary collapse

Instance Method Details

#process_check_result(check) ⇒ Object

Process a check result. Set check result attribute defaults, validate the attributes, publish the check result to the Sensu transport, and respond to the sender with the message “ok”.

Parameters:

  • check (Hash)

    result to be validated and published.

Raises:



25
26
27
28
29
30
# File 'lib/sensu/client/utils.rb', line 25

def process_check_result(check)
  check[:status] ||= 0
  check[:executed] ||= Time.now.to_i
  validate_check_result(check)
  publish_check_result(check)
end

#publish_check_result(check) ⇒ Object

Publish a check result to the Sensu transport.

Parameters:

  • check (Hash)

    result.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sensu/client/utils.rb', line 35

def publish_check_result(check)
  payload = {
    :client => @settings[:client][:name],
    :check => check.merge(:issued => Time.now.to_i)
  }
  payload[:signature] = @settings[:client][:signature] if @settings[:client][:signature]
  @logger.info("publishing check result", :payload => payload)
  @transport.publish(:direct, "results", Sensu::JSON.dump(payload)) do |info|
    if info[:error]
      @logger.error("failed to publish check result", {
        :payload => payload,
        :error => info[:error].to_s
      })
    end
  end
end

#validate_check_result(check) ⇒ Object

Validate check result attributes.

Parameters:

  • check (Hash)

    result to validate.



12
13
14
15
16
17
# File 'lib/sensu/client/utils.rb', line 12

def validate_check_result(check)
  validator = Validators::Check.new
  unless validator.valid?(check)
    raise DataError, validator.failures.first[:message]
  end
end