NAME HealthCheck::Diagnostic::WebRequest - Make HTTP/HTTPS requests to web servers to check connectivity VERSION version v1.4.4 SYNOPSIS # site: https://foo.example # content: This is my content use HealthCheck::Diagnostic::WebRequest; # Look for a 200 status code and pass. my $diagnostic = HealthCheck::Diagnostic::WebRequest->new( url => 'https://foo.example', ); my $result = $diagnostic->check; print $result->{status}; # OK # Look for a 200 status code and verify request takes no more than 10 seconds. my $diagnostic = HealthCheck::Diagnostic::WebRequest->new( url => 'https://foo.example', response_time_threshold => 10, ); my $result = $diagnostic->check; print $result->{status}; # OK if no more than 10, WARNING if more than 10 # Look for a 401 status code and fail. $diagnostic = HealthCheck::Diagnostic::WebRequest->new( url => 'https://foo.example', status_code => 401, ); $result = $diagnostic->check; print $result->{status}; # CRITICAL # Look for any status code less than 500. $diagnostic = HealthCheck::Diagnostic::WebRequest->new( url => 'https://foo.example', status_code => '<500', ); $result = $diagnostic->check; print $result->{status}; # CRITICAL # Look for any 403, 405, or any 2xx range code $diagnostic = HealthCheck::Diagnostic::WebRequest->new( url => 'https://foo.example', status_code => '403, 405, >=200, <300', ); $result = $diagnostic->check; print $result->{status}; # CRITICAL # Look for a 200 status code and content matching the string regex. $diagnostic = HealthCheck::Diagnostic::WebRequest->new( url => 'https://foo.example', content_regex => 'is my', ); $result = $diagnostic->check; print $result->{status}; # OK # Use a regex as the content_regex. $diagnostic = HealthCheck::Diagnostic::WebRequest->new( url => 'https://foo.example', content_regex => qr/is my/, ); $result = $diagnostic->check; print $result->{status}; # OK # POST Method: Look for a 200 status code and content matching the string. my $data = { foo => 'tell me something', }; my $encoded_data = encode_utf8(encode_json($data)); my $header = [ 'Content-Type' => 'application/json; charset=UTF-8' ]; my $url = 'https://dev.payment-express.net/dev/env_test'; my $request = HTTP::Request->new('POST', $url, $header, $encoded_data); $diagnostic = HealthCheck::Diagnostic::WebRequest->new( request => $request, status_code => 200, content_regex => "tell me something", ); $result = $diagnostic->check; print $result->{status}; # OK DESCRIPTION Determines if a web request to a url or request is achievable. Also has the ability to check if the HTTP response contains the right content, specified by content_regex. Sets the status to "OK" or "CRITICAL" based on the success of the checks. ATTRIBUTES url The site that is checked during the HealthCheck. It can be any HTTP/S link. By default, it will send GET requests. Use "request" if you want a more complicated HTTP request. Either this option or "request" are required, and are mutually exclusive. request Allows passing in HTTP::Request object in order to use other HTTP request methods and form data during the HealthCheck. Either this option or "url" are required, and are mutually exclusive. status_code The expected HTTP response status code, or a string of status code conditions. Conditions are comma-delimited, and can optionally have an operator prefix. Any condition without a prefix goes into an OR set, while the prefixed ones go into an AND set. As such, == is not allowed as a prefix, because it's less confusing to not use a prefix here, and more than one condition while a == condition exists would not make sense. Some examples: !500 # Anything besides 500 200, 202 # 200 or 202 200, >=300, <400 # 200 or any 3xx code <400, 405, !202 # Any code below 400 except 202, or 405, # ie: (<400 && !202) || 405 The default value for this is '200', which means that we expect a successful request. response_time_threshold An optional number of seconds to compare the response time to. If it takes no more than this threshold to receive the response or if the threshold is not provided, the status is OK. If the time exceeds this threshold, the status is WARNING. content_regex The content regex to test for in the HTTP response. This is an optional field and is only checked if the status code check passes. This can either be a string or a regex. no_follow_redirects Setting this variable prevents the healthcheck from following redirects. ua An optional attribute to override the default user agent. This must be of type LWP::UserAgent. options See LWP::UserAgent for available options. Takes a hash reference of key/value pairs in order to configure things like ssl_opts, timeout, etc. It is optional. By default provides a custom agent string and a default timeout of 7. METHODS Check Methods Individual HealthCheck results are added in this order as the return value from these methods: check_status my $result = $self->check_status( $response ); This method takes in a HTTP::Response object and returns a healthcheck result with a status key and an info key. The status is CRITICAL if a successful HTTP status code was not received, if the Client-Warning HTTP response header is 'Internal response', or if the X-Squid-Error HTTP response header is set. Otherwise, it is OK. check_response_time my $result = $self->check_response_time( $elapsed_time ); This method takes in a number of seconds and returns a healthcheck result with a status key and an info key. The status is WARNING if the response time exceeds the response_time_threshold. Otherwise, it is OK. check_content my $response = $self->check_content( $response ); This method takes in a HTTP::Response object and returns a healthcheck result with a status key and an info key. The status is CRITICAL if the content of the response does not match the content_regex attribute. Otherwise, it is OK. Note, this is not called if the result of "check_status" is not OK. send_request my $response = $self->send_request; This is the method called internally to receive a response for the healthcheck. Defaults to calling the request method on the user agent and provided request attribute, but this can be overridden in a subclass. DEPENDENCIES HealthCheck::Diagnostic LWP::UserAgent CONFIGURATION AND ENVIRONMENT None AUTHOR Grant Street Group COPYRIGHT AND LICENSE This software is Copyright (c) 2018 - 2024 by Grant Street Group. This is free software, licensed under: The Artistic License 2.0 (GPL Compatible)