Nagios-Plugin-OverHTTP
view release on metacpan or search on metacpan
lib/Nagios/Plugin/OverHTTP/Parser/Standard.pm view on Meta::CPAN
$collected_information{message} = $body_information->{message};
}
if (exists $body_information->{status}) {
# Store the status
$collected_information{status} = $body_information->{status};
}
if (exists $body_information->{performance_data}) {
# The body contained some performance data
if (exists $collected_information{performance_data}) {
# Some performance data already exists, so add it to the end
$collected_information{performance_data} =
$body_information->{performance_data} . qq{\n} . $collected_information{performance_data};
}
else {
# Just set it
$collected_information{performance_data} = $body_information->{performance_data};
}
}
}
if (!exists $collected_information{message}) {
# The message still does not exist, so set to status line and response
$collected_information{message} = join qq{\n},
$response->status_line,
$response->as_string;
}
if (!exists $collected_information{status}) {
# The status still does not exist, so set to the default status
$collected_information{status} = $self->default_status;
}
# Return the response object
return Nagios::Plugin::OverHTTP::Response->new(
response => $response,
%collected_information,
);
}
###########################################################################
# PRIVATE METHODS
sub _parse_body {
my ($self, $response, %args) = @_;
# Get the scrape status
my $scrape_status = exists $args{scrape_status} ? $args{scrape_status}
: 1 # Default
;
# Make a HASH for the collected information
my %collected_information;
# Get the body content
my $body = $self->auto_correct_html ? $self->_strip_html($response)
: $response->decoded_content
;
# Split the body content into lines
my @lines = split m{(?:\r?\n|\n?\r)}msx, $body;
if (!@lines) {
# There is no content to parse
return {};
}
# First line is message + optional performance data
@collected_information{qw(message performance_data)} = shift(@lines)
=~ m{\A ([^\|]+?) (?:\s* \| \s* (.*))? \z}msx;
if (!defined $collected_information{performance_data}) {
# Change to an empty string
$collected_information{performance_data} = q{};
}
# Walk through the rest of the lines
LINE:
while (defined(my $line = shift @lines)) {
if ($line =~ m{\A ([^\|]+?) \s* \| \s* (.*) \z}msx) {
# This line ends the plugin output and begins the performance data
$collected_information{message} .= qq{\n$1};
$collected_information{performance_data} .= qq{\n} . join qq{\n}, $2, @lines;
last LINE;
}
else {
# This is just a normal line
$collected_information{message} .= qq{\n$line};
}
}
if ($scrape_status && $collected_information{message} =~
m{\A (?:[^a-z]+ \s+)? (OK|WARNING|CRITICAL|UNKNOWN)\b}msx) {
# Scraped the status from the message
my $status = to_Status($1);
if (defined $status) {
# Collect the valid status
$collected_information{status} = $status;
}
}
if ($collected_information{performance_data} eq q{}) {
# There was no collected performance data
delete $collected_information{performance_data};
}
# Return the collected information
return \%collected_information;
}
sub _parse_headers {
my ($self, $response) = @_;
# Create a HASH to store information
my %collected_information;
if (defined $response->header($HEADER_MESSAGE)) {
# The message header is present, so this is the message
$collected_information{message} = join qq{\n},
$response->header($HEADER_MESSAGE);
}
( run in 0.775 second using v1.01-cache-2.11-cpan-71847e10f99 )