Skip to content

Commit

Permalink
[CTOR-236] [Plugin] Ensure metrics list is available for MC documenta…
Browse files Browse the repository at this point in the history
…tion (#4802)
  • Loading branch information
lucie-dubrunfaut authored Feb 5, 2024
1 parent 39829cc commit c8e7a7f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
8 changes: 7 additions & 1 deletion src/centreon/plugins/alternative/Getopt.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
46 changes: 41 additions & 5 deletions src/centreon/plugins/templates/counter.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -173,6 +174,7 @@ sub new {
}
}


return $self;
}

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

0 comments on commit c8e7a7f

Please sign in to comment.