Module: Sensu::API::Utilities::PublishCheckRequest

Includes:
Utilities
Defined in:
lib/sensu/api/utilities/publish_check_request.rb

Constant Summary

Constants included from Utilities

Utilities::EVAL_PREFIX

Instance Method Summary collapse

Methods included from Utilities

#attributes_match?, #check_subdued?, #deep_dup, #deep_merge, #determine_check_cron_time, #eval_attribute_value, #find_attribute_value, #in_time_window?, #in_time_windows?, #object_substitute_tokens, #process_cpu_times, #process_eval_string, #random_uuid, #redact_sensitive, #retry_until_true, #substitute_tokens, #system_address, #system_hostname, #testing?

Instance Method Details

#publish_check_request(check) ⇒ Object

Publish a check request to the Transport. A check request is composed of a check ‘:name`, an `:issued` timestamp, a check `:command` if available, and a check `:extension if available. The check request is published to a Transport pipe, for each of the check `:subscribers` in its definition, eg. “webserver”. JSON serialization is used when publishing the check request payload to the Transport pipes. Transport errors are logged.

Parameters:

  • check (Hash)

    definition.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sensu/api/utilities/publish_check_request.rb', line 39

def publish_check_request(check)
  payload = check.reject do |key, value|
    [:subscribers, :interval].include?(key)
  end
  payload[:issued] = Time.now.to_i
  @logger.info("publishing check request", {
    :payload => payload,
    :subscribers => check[:subscribers]
  })
  check[:subscribers].each do |subscription|
    options = transport_publish_options(subscription.to_s, Sensu::JSON.dump(payload))
    @transport.publish(*options) do |info|
      if info[:error]
        @logger.error("failed to publish check request", {
          :subscription => subscription,
          :payload => payload,
          :error => info[:error].to_s
        })
      end
    end
  end
end

#publish_proxy_check_requests(check) ⇒ Object

Create and publish one or more proxy check requests. This method iterates through the Sensu client registry for clients that matched provided proxy request client attributes. A proxy check request is created for each client in the registry that matches the proxy request client attributes. Proxy check requests have their client tokens subsituted by the associated client attributes values. The check requests are published to the Transport via ‘publish_check_request()`.

Parameters:

  • check (Hash)

    definition.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/sensu/api/utilities/publish_check_request.rb', line 72

def publish_proxy_check_requests(check)
  client_attributes = check[:proxy_requests][:client_attributes]
  unless client_attributes.empty?
    @redis.smembers("clients") do |clients|
      clients.each do |client_name|
        @redis.get("client:#{client_name}") do |client_json|
          unless client_json.nil?
            client = Sensu::JSON.load(client_json)
            if attributes_match?(client, client_attributes)
              @logger.debug("creating a proxy check request", {
                :client => client,
                :check => check
              })
              proxy_check, unmatched_tokens = object_substitute_tokens(check.dup, client)
              if unmatched_tokens.empty?
                proxy_check[:source] ||= client[:name]
                publish_check_request(proxy_check)
              else
                @logger.warn("failed to publish a proxy check request", {
                  :reason => "unmatched client tokens",
                  :unmatched_tokens => unmatched_tokens,
                  :client => client,
                  :check => check
                })
              end
            end
          end
        end
      end
    end
  end
end

#transport_publish_options(subscription, message) ⇒ Array

Determine the Sensu Transport publish options for a subscription. If a subscription begins with a Transport pipe type, either “direct:” or “roundrobin:”, the subscription uses a direct Transport pipe. If a subscription does not specify a Transport pipe type, a fanout Transport pipe is used.

Parameters:

  • subscription (String)
  • message (String)

Returns:

  • (Array)

    containing the Transport publish options: the Transport pipe type, pipe, and the message to be published.



20
21
22
23
24
25
26
27
28
# File 'lib/sensu/api/utilities/publish_check_request.rb', line 20

def transport_publish_options(subscription, message)
  _, raw_type = subscription.split(":", 2).reverse
  case raw_type
  when "direct", "roundrobin"
    [:direct, subscription, message]
  else
    [:fanout, subscription, message]
  end
end