[CTOR-236] [Plugin] Ensure metrics list is available for MC documentation (#4802)

This commit is contained in:
Lucie Dubrunfaut 2024-02-05 10:18:49 +01:00 committed by GitHub
parent 39829cc8ee
commit c8e7a7f319
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 48 additions and 6 deletions

View File

@ -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;
}

View File

@ -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();
}