From dec60f5611e7820011807d51aeefecf8de379932 Mon Sep 17 00:00:00 2001 From: qgarnier Date: Wed, 1 Jun 2022 11:41:56 +0200 Subject: [PATCH] =?UTF-8?q?(plugin)=20apps::protocols::nrpe=20-=20enhance?= =?UTF-8?q?=20perfdata=20management=20in=20NRPE=20=E2=80=A6=20(#3703)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apps/protocols/nrpe/custom/nrpe.pm | 64 ++++++++++++++++--- .../apps/protocols/nrpe/mode/query.pm | 12 +++- 2 files changed, 64 insertions(+), 12 deletions(-) diff --git a/centreon-plugins/apps/protocols/nrpe/custom/nrpe.pm b/centreon-plugins/apps/protocols/nrpe/custom/nrpe.pm index 7d201a16f..8519bc575 100644 --- a/centreon-plugins/apps/protocols/nrpe/custom/nrpe.pm +++ b/centreon-plugins/apps/protocols/nrpe/custom/nrpe.pm @@ -24,6 +24,7 @@ use base qw(centreon::plugins::mode); use strict; use warnings; +use centreon::plugins::misc; use centreon::plugins::nrpe; my %errors_num = (0 => 'OK', 1 => 'WARNING', 2 => 'CRITICAL', 3 => 'UNKNOWN'); @@ -44,7 +45,8 @@ sub new { if (!defined($options{noptions})) { $options{options}->add_options(arguments => { - 'hostname:s' => { name => 'hostname' } + 'hostname:s' => { name => 'hostname' }, + 'parse' => { name => 'parse' } }); } $options{options}->add_help(package => __PACKAGE__, sections => 'CUSTOM MODE OPTIONS', once => 1); @@ -68,25 +70,67 @@ sub check_options { $self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : ''; - if (!defined($self->{hostname}) || $self->{hostname} eq '') { + if ($self->{hostname} eq '') { $self->{output}->add_option_msg(short_msg => "Need to specify --hostname option."); $self->{output}->option_exit(); } - + $self->{nrpe}->check_options(option_results => $self->{option_results}); return 0; } +sub parse_plugin_output { + my ($self, %options) = @_; + + my @lines = split(/\n/, $options{output}); + my $short = 'no output'; + my $line = shift(@lines); + if (defined($line) && $line =~ /^(.*?)(?:\|(.*)|\Z)/) { + $short = $1; + if (defined($2)) { + my $perf = $2; + while ($perf =~ /(.*?)=([0-9\.]+)([^0-9;]+?)?([0-9.@;]+?)?(?:\s+|\Z)/g) { + my ($label, $value, $unit, $extra) = ($1, $2, $3, $4); + $label = centreon::plugins::misc::trim($label); + $label =~ s/^'//; + $label =~ s/'$//; + my @extras = split(';', $extra); + push @{$options{result}->{perf}}, { + label => $label, + nlabel => $label, + unit => $unit, + value => $value, + warning => $extras[1], + critical => $extras[2], + min => $extras[3], + max => $extras[4] + }; + } + } + } + + $options{result}->{message} = $short; + $options{result}->{long_message} = []; + foreach (@lines) { + push @{$options{result}->{long_message}}, $_; + } +} + sub format_result { my ($self, %options) = @_; - my %result = ( + my $result = { code => ($options{content}->{result_code} =~ /^[0-3]$/) ? $errors_num{$options{content}->{result_code}} : $options{content}->{result_code}, message => $options{content}->{buffer}, perf => [] - ); - return \%result; + }; + + if (defined($self->{option_results}->{parse})) { + $self->parse_plugin_output(result => $result, output => $options{content}->{buffer}); + } + + return $result; } sub request { @@ -94,9 +138,7 @@ sub request { my ($content) = $self->{nrpe}->request(check => $options{command}, arg => $options{arg}); - my $result = $self->format_result(content => $content); - - return $result; + return $self->format_result(content => $content); } 1; @@ -117,6 +159,10 @@ NRPE protocol Remote hostname or IP address. +=item B<--parse> + +Parse remote plugin output. + =back =head1 DESCRIPTION diff --git a/centreon-plugins/apps/protocols/nrpe/mode/query.pm b/centreon-plugins/apps/protocols/nrpe/mode/query.pm index e499ed733..11d4cc617 100644 --- a/centreon-plugins/apps/protocols/nrpe/mode/query.pm +++ b/centreon-plugins/apps/protocols/nrpe/mode/query.pm @@ -69,22 +69,28 @@ sub sanitize_message { sub run { my ($self, %options) = @_; - + my $result = $options{custom}->request( command => $self->{option_results}->{command}, arg => $self->{option_results}->{arg} ); - + $self->{output}->output_add( severity => $result->{code}, short_msg => $self->sanitize_message(message => $result->{message}) ); + if (defined($result->{long_message})) { + foreach (@{$result->{long_message}}) { + $self->{output}->output_add(long_msg => $_); + } + } + foreach (@{$result->{perf}}) { $self->{output}->perfdata_add(%{$_}); } $self->{display_options}->{force_ignore_perfdata} = 1 if (scalar(@{$result->{perf}}) == 0); - + $self->{output}->display(%{$self->{display_options}}); $self->{output}->exit(); }