| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package HealthCheck::Diagnostic::RemoteHealth; | 
| 2 | 1 |  |  | 1 |  | 239962 | use parent 'HealthCheck::Diagnostic::WebRequest'; | 
|  | 1 |  |  |  |  | 8 |  | 
|  | 1 |  |  |  |  | 8 |  | 
| 3 | 1 |  |  | 1 |  | 59504 | use strict; | 
|  | 1 |  |  |  |  | 7 |  | 
|  | 1 |  |  |  |  | 22 |  | 
| 4 | 1 |  |  | 1 |  | 5 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 24 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 1 |  |  | 1 |  | 741 | use JSON; | 
|  | 1 |  |  |  |  | 10998 |  | 
|  | 1 |  |  |  |  | 5 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | # ABSTRACT: Get results from an HTTP HealthCheck | 
| 9 | 1 |  |  | 1 |  | 152 | use version; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 7 |  | 
| 10 |  |  |  |  |  |  | our $VERSION = 'v0.1.1'; # VERSION | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | sub new { | 
| 13 | 1 |  |  | 1 | 1 | 94 | my ($class, @params) = @_; | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | my %params = @params == 1 && ( ref $params[0] || '' ) eq 'HASH' | 
| 16 | 1 | 50 | 33 |  |  | 10 | ? %{ $params[0] } : @params; | 
|  | 0 |  |  |  |  | 0 |  | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | # Allows 200 and 503 codes by default. | 
| 19 | 1 |  | 50 |  |  | 10 | $params{status_code} //= '200, 503'; | 
| 20 |  |  |  |  |  |  |  | 
| 21 | 1 |  |  |  |  | 12 | return $class->SUPER::new( | 
| 22 |  |  |  |  |  |  | id    => 'remotehealth', | 
| 23 |  |  |  |  |  |  | label => 'RemoteHealth', | 
| 24 |  |  |  |  |  |  | %params, | 
| 25 |  |  |  |  |  |  | ); | 
| 26 |  |  |  |  |  |  | } | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | sub run { | 
| 29 | 6 |  |  | 6 | 1 | 23620 | my ($self, %params) = @_; | 
| 30 | 6 |  |  |  |  | 28 | my $result = $self->next::method(%params); | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | # Throws away the HTTP status check if OK, | 
| 33 |  |  |  |  |  |  | # since it's implied to be successful | 
| 34 |  |  |  |  |  |  | # if it retrieves the encoded JSON object. | 
| 35 |  |  |  |  |  |  | return $result->{results}->[1] | 
| 36 | 6 | 50 |  |  |  | 65 | if @{$result->{results} || []} == 2 | 
| 37 | 6 | 100 | 66 |  |  | 2694 | and $result->{results}->[0]->{status} eq 'OK'; | 
| 38 | 2 |  |  |  |  | 13 | return $result; | 
| 39 |  |  |  |  |  |  | } | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | # Checking for content regex from JSON seems unnecessary, so this has been | 
| 42 |  |  |  |  |  |  | # repurposed to return the decoded JSON object. | 
| 43 |  |  |  |  |  |  | sub check_content { | 
| 44 | 4 |  |  | 4 | 0 | 8043 | my ($self, $response) = @_; | 
| 45 |  |  |  |  |  |  |  | 
| 46 | 4 |  |  |  |  | 8 | local $@; | 
| 47 | 4 |  |  |  |  | 9 | my $remote_result = eval { decode_json($response->content) }; | 
|  | 4 |  |  |  |  | 21 |  | 
| 48 |  |  |  |  |  |  | return { | 
| 49 | 4 | 100 | 66 |  |  | 101 | status => 'CRITICAL', | 
| 50 |  |  |  |  |  |  | info   => 'Could not decode JSON.', | 
| 51 |  |  |  |  |  |  | data   => $response->content, | 
| 52 |  |  |  |  |  |  | } if $@ or ref($remote_result) ne 'HASH'; | 
| 53 |  |  |  |  |  |  |  | 
| 54 | 3 |  |  |  |  | 16 | return { results => [ $remote_result ] }; | 
| 55 |  |  |  |  |  |  | } | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | 1; | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | __END__ |