From c8e7a7f3199767b7ccc33ff0c2f5e99c35714182 Mon Sep 17 00:00:00 2001 From: Lucie Dubrunfaut <123162035+lucie-dubrunfaut@users.noreply.github.com> Date: Mon, 5 Feb 2024 10:18:49 +0100 Subject: [PATCH] [CTOR-236] [Plugin] Ensure metrics list is available for MC documentation (#4802) --- src/centreon/plugins/alternative/Getopt.pm | 8 +++- src/centreon/plugins/templates/counter.pm | 46 +++++++++++++++++++--- 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/src/centreon/plugins/alternative/Getopt.pm b/src/centreon/plugins/alternative/Getopt.pm index 13d38af86..b7a7a8d10 100644 --- a/src/centreon/plugins/alternative/Getopt.pm +++ b/src/centreon/plugins/alternative/Getopt.pm @@ -73,7 +73,13 @@ sub GetOptions { # find type of option if ($search_str !~ /,((?:[^,]*?\|){0,}$option(?:\|.*?){0,}(:.*?){0,1}),/) { - warn "Unknown option: $option" if ($warn_message == 1); + + # for old format plugins (with run function) that not allowed list-counters options + if($option =~ /list-counters/){ + warn "list-counters option not available yet for this mode." if ($warn_message == 1); + }else{ + warn "Unknown option: $option" if ($warn_message == 1); + } $i++; next; } diff --git a/src/centreon/plugins/templates/counter.pm b/src/centreon/plugins/templates/counter.pm index 8feead304..a0b079779 100644 --- a/src/centreon/plugins/templates/counter.pm +++ b/src/centreon/plugins/templates/counter.pm @@ -26,6 +26,7 @@ use strict; use warnings; use centreon::plugins::values; use centreon::plugins::misc; +use JSON::XS; my $sort_subs = { num => sub { $a <=> $b }, @@ -173,6 +174,7 @@ sub new { } } + return $self; } @@ -181,18 +183,52 @@ sub check_options { $self->SUPER::init(%options); if (defined($self->{option_results}->{list_counters})) { - my $list_counter = 'counter list:'; + my $list_counter = ''; my $th_counter = ''; + my $counters; foreach my $key (keys %{$self->{maps_counters}}) { foreach (@{$self->{maps_counters}->{$key}}) { + $counters->{metrics}->{$_->{label}}->{nlabel} =""; + $counters->{metrics}->{$_->{label}}->{min}=""; + $counters->{metrics}->{$_->{label}}->{max}=""; + $counters->{metrics}->{$_->{label}}->{unit}=""; + $counters->{metrics}->{$_->{label}}->{output_template}=""; + if(defined($_->{nlabel})) { + $counters->{metrics}->{$_->{label}}->{nlabel} = $_->{nlabel}; + } + if(defined($_->{set}->{perfdatas}->[0]->{min})) { + $counters->{metrics}->{$_->{label}}->{min} = $_->{set}->{perfdatas}->[0]->{min}; + } + if(defined($_->{set}->{perfdatas}->[0]->{max})) { + $counters->{metrics}->{$_->{label}}->{max} = $_->{set}->{perfdatas}->[0]->{max}; + } + if(defined($_->{set}->{perfdatas}->[0]->{unit})) { + $counters->{metrics}->{$_->{label}}->{unit} = $_->{set}->{perfdatas}->[0]->{unit}; + } + if(defined($_->{set}->{perfdatas}->[0]->{template})) { + $counters->{metrics}->{$_->{label}}->{output_template} = $_->{set}->{perfdatas}->[0]->{template}; + } my $label = $_->{label}; $label =~ s/-//g; - $list_counter .= " " . $_->{label}; - $th_counter .= " --warning-$_->{label}='\$_SERVICEWARNING" . uc($label) . "\$' --critical-$_->{label}='\$_SERVICECRITICAL" . uc($label) . "\$'"; + $list_counter .= $_->{label}." "; + $th_counter .= "--warning-$_->{label}='\$_SERVICEWARNING" . uc($label) . "\$' --critical-$_->{label}='\$_SERVICECRITICAL" . uc($label) . "\$'"; + } } - $self->{output}->output_add(short_msg => $list_counter); - $self->{output}->output_add(long_msg => 'configuration: ' . $th_counter); + $counters->{"counter list"}=$list_counter; + $counters->{"pack configuration"}=$th_counter." \$_SERVICEEXTRAOPTIONS\$"; + + my $result_data =""; + eval { + $result_data = JSON::XS->new->indent->space_after->canonical->utf8->encode($counters); + }; + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot use \$counters as it is a malformed JSON: " . $@); + $self->{output}->option_exit(); + } + + $self->{output}->output_add(short_msg => "counter list: ".$list_counter); + $self->{output}->output_add(long_msg => $result_data); $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1); $self->{output}->exit(); }