class Arborist::Monitor::Webservice::HTTP

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

new( timeout: Arborist::Monitor::Webservice.default_timeout ) click to toggle source

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
node_properties() click to toggle source

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
run( nodes ) click to toggle source

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

make_headers_hash( node_data ) click to toggle source

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
make_response_results( response, node ) click to toggle source

Return a Hash of results appropriate for the specified response.

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

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
request_for_node( node_data ) click to toggle source

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
run( nodes ) click to toggle source

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 )
                end
                hydra.queue( request )
        end

        hydra.run

        return results
end
runner_settings() click to toggle source

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
success_results( response ) click to toggle source

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
with_timeout( new_timeout ) click to toggle source

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

Private Instance Methods

convert_http_version( version_string ) click to toggle source

Convert a version string like `1.1` into the Symbol Typhoeus/Ethon expect (e.g., `:httpv1_1`)

# File lib/arborist/monitor/webservice.rb, line 231
def convert_http_version( version_string )
        return case version_string
                when '1.0'
                        :httpv1_0
                when '1.1'
                        :httpv1_1
                when '2.0'
                        :httpv2_0
                else
                        version_string.to_sym
                end
end