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:



37
38
39
40
41
42
# File 'lib/sensu/client/utils.rb', line 37

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.



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

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.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sensu/client/utils.rb', line 10

def validate_check_result(check)
  unless check[:name] =~ /\A[\w\.-]+\z/
    raise DataError, "check name must be a string and cannot contain spaces or special characters"
  end
  unless check[:source].nil? || check[:source] =~ /\A[\w\.-]+\z/
    raise DataError, "check source must be a string and cannot contain spaces or special characters"
  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
  unless check[:ttl].nil? || (check[:ttl].is_a?(Integer) && check[:ttl] > 0)
    raise DataError, "check ttl must be an integer greater than 0"
  end
end