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:



34
35
36
37
38
39
# File 'lib/sensu/client/utils.rb', line 34

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.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sensu/client/utils.rb', line 44

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
18
19
20
21
22
23
24
25
26
# 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
  unless check[:output].is_a?(String)
    raise DataError, "check output must be a string"
  end
  unless check[:status].is_a?(Integer)
    raise DataError, "check status must be an integer"
  end
  unless check[:executed].is_a?(Integer)
    raise DataError, "check executed timestamp must be an integer"
  end
end