Arborist::Monitor::Webservice::

HTTP

class
Superclass
Object
Included Modules
Arborist::Webservice::Constants
Extended With
Loggability

Arborist HTTP web service monitor logic

Constants

DEFAULT_OPTIONS

Defaults for instances of this monitor

NODE_PROPERTIES

The array of node properites used by this monitor

Attributes

timeout[RW]

The timeout for connecting, in seconds.

Public Class Methods

anchor
new( timeout: Arborist::Monitor::Webservice.default_timeout )

Create a new HTTP webservice monitor with the specified options. Valid options are:

:timeout Set the number of seconds to wait for a connection for each node.

# File lib/arborist/monitor/webservice.rb, line 82
def initialize( timeout: Arborist::Monitor::Webservice.default_timeout )
        self.timeout = timeout
end
anchor
node_properties()

Return an array of attributes to fetch from nodes for this monitor.

# File lib/arborist/monitor/webservice.rb, line 67
def self::node_properties
        return NODE_PROPERTIES
end
anchor
run( nodes )

Instantiate a monitor check and run it for the specified nodes.

# File lib/arborist/monitor/webservice.rb, line 73
def self::run( nodes )
        return self.new.run( nodes )
end

Public Instance Methods

anchor
make_headers_hash( node_data )

Extract header values from the node_data, combine them with the defaults, and return them as a Hash.

# File lib/arborist/monitor/webservice.rb, line 166
def make_headers_hash( node_data )
        headers = DEFAULT_HTTP_HEADERS.merge( node_data['http_headers'] || {} )
        if node_data['body']
                headers[ 'Content-type' ] ||= node_data['body_mimetype'] ||
                         'application/x-www-form-urlencoded'
        end

        return headers
end
anchor
make_response_results( response, expected_status=200 )

Return a Hash of results appropriate for the specified response.

# File lib/arborist/monitor/webservice.rb, line 186
def make_response_results( response, expected_status=200 )
        if response.code == expected_status
                return { webservice: self.success_results(response) }
        elsif response.timed_out?
                self.log.error "Request timed out."
                return { error: 'Request timed out.' }
        elsif response.code == 0
                self.log.error( response.return_message )
                return { error: response.return_message }
        else
                self.log.error "Got an unexpected %03d %s response; expected %03d." %
                        [ response.code, response.status_message, expected_status ]
                return {
                        error: "%03d %s" % [ response.code, response.status_message ]
                }
        end
end
anchor
make_ssl_options( uri, node_data )

Make a Hash of SSL options if any are specified.

# File lib/arborist/monitor/webservice.rb, line 148
def make_ssl_options( uri, node_data )
        return nil unless uri.start_with?( 'https:' )

        self.log.debug "Extracting valid SSL options from the node's config: %p" %
                [ node_data['config'] ]
        ssl_attributes = SSL_ATTRIBUTES.each_with_object({}) do |(key, desc), opts|
                opts[ key ] = node_data[ 'config' ][ key.to_s ] if
                        node_data['config']&.key?( key.to_s )
        end

        ssl_attributes[ :ssl_verifypeer ] ||= Arborist::Monitor::Webservice.ssl_verifypeer

        return ssl_attributes
end
anchor
request_for_node( node_data )

Return a request object built to test the specified webservice node.

# File lib/arborist/monitor/webservice.rb, line 126
def request_for_node( node_data )
        http_version = convert_http_version( node_data['http_version'] || DEFAULT_HTTP_VERSION )

        options = {
                method: node_data['http_method'] || DEFAULT_HTTP_METHOD,
                http_version: http_version,
                headers: self.make_headers_hash( node_data ),
                body: node_data['body'],
                timeout: self.timeout,
                connecttimeout: self.timeout / 2.0,
        }

        if ssl_opts = self.make_ssl_options( node_data['uri'], node_data )
                options.merge!( ssl_opts )
        end

        self.log.debug "Node options for %p are: %p" % [ node_data['uri'], options ]
        return Typhoeus::Request.new( node_data['uri'], options )
end
anchor
run( nodes )

Test HTTP connections for the specified nodes.

# File lib/arborist/monitor/webservice.rb, line 104
def run( nodes )
        results = {}
        hydra = Typhoeus::Hydra.new( self.runner_settings )

        nodes.each do |identifier, node|
                self.log.debug "Making request for node %s" % [ identifier ]
                request = self.request_for_node( node )
                request.on_complete do |response|
                        self.log.debug "Handling response for %s" % [ identifier ]
                        results[ identifier ] =
                                self.make_response_results( response, node['expected_status'] )
                end
                hydra.queue( request )
        end

        hydra.run

        return results
end
anchor
runner_settings()

Return a Hash of options to pass to the request runner.

# File lib/arborist/monitor/webservice.rb, line 178
def runner_settings
        return {
                max_concurrency: Arborist::Monitor::Webservice.max_concurrency,
        }
end
anchor
success_results( response )

Return the information hash attached to webservice nodes for successful responses.

# File lib/arborist/monitor/webservice.rb, line 207
def success_results( response )
        return {
                http_version: response.http_version,
                status: response.code,
                status_message: response.status_message,
                headers: response.headers_hash,
                appconnect_time: response.appconnect_time,
                connect_time: response.connect_time,
                lookup_time: response.name_lookup_time,
                pretransfer_time: response.pretransfer_time,
                redirect_count: response.redirect_count,
                redirect_time: response.redirect_time,
                start_transfer_time: response.start_transfer_time,
                total_time: response.total_time,
        }
end
anchor
with_timeout( new_timeout )

Return a clone of this object with its timeout set to new_timeout.

# File lib/arborist/monitor/webservice.rb, line 96
def with_timeout( new_timeout )
        copy = self.clone
        copy.timeout = new_timeout
        return copy
end