Nagios-Plugin-OverHTTP

 view release on metacpan or  search on metacpan

lib/Nagios/Plugin/OverHTTP/PerformanceData.pm  view on Meta::CPAN


	return $label;
}
sub _performance_data_split {
	my ($string) = @_;

	# Get all the performance data items
	my @items = ($string =~ m{( $QUOTED_LABEL? \S+ )}gmsx);

	return @items;
}
sub _performance_data_args_from_string {
	my ($string) = @_;

	# Trim the string
	$string =~ s{\A \s* | \s* \z}{}gmsx;

	# This is the arguments hash
	my %args;

	if ($string =~ m{\A '}msx) {
		# Match the label in a quoted label
		my ($label) = $string =~ m{\A ( $QUOTED_LABEL ) }msx;

		# Remove the surrounding quotes
		($label) = $label =~ m{\A ' (.+?) ' \z}msx;

		# Chop the label off the string
		$string = substr $string, 2 + length $label;

		# Unescape the single quotes in the string
		$label =~ s{''}{'}gmsx;

		# Save the label
		$args{label} = $label;
	}
	elsif ($string =~ m{\A ([^=]+) =}msx) {
		# The label is everything until the equal sign
		my $label = $1;

		# Chop the label off the string
		$string = substr $string, length $label;

		# Save the label
		$args{label} = $label;
	}
	else {
		croak 'Invalid performance string format';
	}

	# Equal sign must be present here
	if (q{=} ne substr $string, 0, 1) {
		croak 'Invalid performance string format';
	}

	# Remove equal sign
	$string = substr $string, 1;

	# Get all the arguments split out
	@args{qw(value warning_threshold critical_threshold minimum_value maximum_value)} =
		split m{;}msx, $string;

	# Remove all undefined values
	while (my ($key, $value) = each %args) {
		if (!defined $value || $value eq q{}) {
			delete $args{$key};
		}
	}

	if (exists $args{value}) {
		# Scrape out the units
		my ($value, $units) = $args{value} =~ m{\A ([+-]?\d*\.?\d*) ([%a-z]+) \z}imsx;

		if (defined $units) {
			# Set the new value and the units
			@args{qw(value units)} = ($value, $units);
		}
	}

	# Return hash of arguments as a list
	return %args;
}

1;

__END__

=head1 NAME

Nagios::Plugin::OverHTTP::PerformanceData - Represents performance data of a
Nagios plugin

=head1 VERSION

This documentation refers to L<Nagios::Plugin::OverHTTP::PerformanceData>
version 0.16

=head1 SYNOPSIS

  use Nagios::Plugin::OverHTTP::PerformanceData;

  # New from many options
  my $data = Nagios::Plugin::OverHTTP::PerformanceData->new(
      label => q{time},
      value => 5,
      units => q{s}, # Seconds
  );

  # New from a performance string
  my $data = Nagios::Plugin::OverHTTP::PerformanceData->new('time=5s');

  # Set a new critical threshold
  $data->critical_threshold('@10:20');

  # Check if matches the critical threshold
  say $data->is_critical ? 'CRITICAL' : 'NOT CRITICAL';

  # Print out plugin information with performance data
  printf q{%s - %s | %s}, $status, $message, $data->to_string;

=head1 DESCRIPTION



( run in 1.640 second using v1.01-cache-2.11-cpan-71847e10f99 )