(plugin) cloud::prometheus::restapi - mode expressions can use multiple instance option (#4262)

This commit is contained in:
qgarnier 2023-03-07 15:37:09 +00:00 committed by GitHub
parent 16c5cddafe
commit f2afd9f5d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 69 additions and 50 deletions

View File

@ -40,7 +40,7 @@ sub new {
$options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument."); $options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument.");
$options{output}->option_exit(); $options{output}->option_exit();
} }
if (!defined($options{noptions})) { if (!defined($options{noptions})) {
$options{options}->add_options(arguments => { $options{options}->add_options(arguments => {
'hostname:s' => { name => 'hostname' }, 'hostname:s' => { name => 'hostname' },
@ -117,19 +117,19 @@ sub settings {
sub get_connection_info { sub get_connection_info {
my ($self, %options) = @_; my ($self, %options) = @_;
return $self->{hostname} . ":" . $self->{port}; return $self->{hostname} . ":" . $self->{port};
} }
sub get_hostname { sub get_hostname {
my ($self, %options) = @_; my ($self, %options) = @_;
return $self->{hostname}; return $self->{hostname};
} }
sub get_port { sub get_port {
my ($self, %options) = @_; my ($self, %options) = @_;
return $self->{port}; return $self->{port};
} }
@ -168,9 +168,8 @@ sub get_endpoint {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->settings(); $self->settings();
my $response = $self->{http}->request(url_path => $self->{url_path} . $options{url_path}); my $response = $self->{http}->request(url_path => $self->{url_path} . $options{url_path});
my $content; my $content;
eval { eval {
$content = JSON::XS->new->utf8->decode($response); $content = JSON::XS->new->utf8->decode($response);

View File

@ -24,17 +24,21 @@ use base qw(centreon::plugins::templates::counter);
use strict; use strict;
use warnings; use warnings;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold); use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
sub custom_status_perfdata { sub custom_status_perfdata {
my ($self, %options) = @_; my ($self, %options) = @_;
my $instances = [];
foreach (@{$self->{result_values}->{instances}}) {
push @$instances, $_->{value};
}
foreach my $key (@{$self->{instance_mode}->{custom_keys}}) { foreach my $key (@{$self->{instance_mode}->{custom_keys}}) {
$self->{output}->perfdata_add( $self->{output}->perfdata_add(
label => $key,
nlabel => $key, nlabel => $key,
value => $self->{result_values}->{$key}, value => $self->{result_values}->{$key},
instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{instance} : undef instances => $instances
); );
} }
} }
@ -56,9 +60,14 @@ sub custom_status_output {
sub custom_status_calc { sub custom_status_calc {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->{result_values}->{instance} = $options{new_datas}->{$self->{instance} . '_instance'}; $self->{result_values}->{instances} = $options{new_datas}->{ $self->{instance} . '_instances' };
$self->{result_values}->{instance} = $self->{result_values}->{instances}->[0]->{value}; # compatibility
foreach (@{$self->{result_values}->{instances}}) {
$self->{result_values}->{ $_->{name} } = $_->{value};
}
foreach my $key (@{$self->{instance_mode}->{custom_keys}}) { foreach my $key (@{$self->{instance_mode}->{custom_keys}}) {
$self->{result_values}->{$key} = $options{new_datas}->{$self->{instance} . '_' . $key}; $self->{result_values}->{$key} = $options{new_datas}->{ $self->{instance} . '_' . $key };
} }
return 0; return 0;
@ -66,38 +75,35 @@ sub custom_status_calc {
sub set_counters { sub set_counters {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->{maps_counters_type} = [ $self->{maps_counters_type} = [
{ name => 'expressions', type => 1, { name => 'expressions', type => 1, message_multiple => 'All expressions results are ok', skipped_code => { -11 => 1 } }
message_multiple => 'All expressions results are ok', skipped_code => { -11 => 1 } },
]; ];
$self->{maps_counters}->{expressions} = [ $self->{maps_counters}->{expressions} = [
{ label => 'status', set => { { label => 'status', type => 2, set => {
key_values => [ { name => 'instance' } ], key_values => [ { name => 'instances' } ],
closure_custom_calc => $self->can('custom_status_calc'), closure_custom_calc => $self->can('custom_status_calc'),
closure_custom_output => $self->can('custom_status_output'), closure_custom_output => $self->can('custom_status_output'),
closure_custom_perfdata => $self->can('custom_status_perfdata'), closure_custom_perfdata => $self->can('custom_status_perfdata'),
closure_custom_threshold_check => \&catalog_status_threshold, closure_custom_threshold_check => \&catalog_status_threshold_ng
} }
}, }
]; ];
} }
sub new { sub new {
my ($class, %options) = @_; my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options); my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
bless $self, $class; bless $self, $class;
$options{options}->add_options(arguments => { $options{options}->add_options(arguments => {
"query:s@" => { name => 'query' }, 'query:s@' => { name => 'query' },
"query-range:s@" => { name => 'query_range' }, 'query-range:s@' => { name => 'query_range' },
"instance:s" => { name => 'instance' }, 'instance:s@' => { name => 'instance' },
"aggregation:s" => { name => 'aggregation', default => 'average' }, 'aggregation:s' => { name => 'aggregation', default => 'average' },
"output:s" => { name => 'output' }, 'output:s' => { name => 'output' },
"multiple-output:s" => { name => 'multiple_output' }, 'multiple-output:s' => { name => 'multiple_output' }
"warning-status:s" => { name => 'warning_status', default => '' },
"critical-status:s" => { name => 'critical_status', default => '' },
}); });
return $self; return $self;
@ -111,12 +117,20 @@ sub check_options {
$self->{output}->add_option_msg(short_msg => "Need to specify output option."); $self->{output}->add_option_msg(short_msg => "Need to specify output option.");
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
if (!defined($self->{option_results}->{instance}) || $self->{option_results}->{instance} eq '') { $self->{instances} = [];
if (defined($self->{option_results}->{instance})) {
foreach (@{$self->{option_results}->{instance}}) {
next if ($_ eq '');
push @{$self->{instances}}, $_;
}
}
if (scalar(@{$self->{instances}}) <= 0) {
$self->{output}->add_option_msg(short_msg => "Need to specify instance option."); $self->{output}->add_option_msg(short_msg => "Need to specify instance option.");
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
if (!defined($self->{option_results}->{query}) && !defined($self->{option_results}->{query_range})) { if (!defined($self->{option_results}->{query}) && !defined($self->{option_results}->{query_range})) {
$self->{output}->add_option_msg(short_msg => "Need to specify query or query-range option."); $self->{output}->add_option_msg(short_msg => "Need to specify query or query-range option.");
$self->{output}->option_exit(); $self->{output}->option_exit();
@ -127,7 +141,7 @@ sub check_options {
foreach my $query (@{$self->{option_results}->{query}}) { foreach my $query (@{$self->{option_results}->{query}}) {
next if ($query !~ /^(\w+),(.*)/); next if ($query !~ /^(\w+),(.*)/);
$self->{queries}->{$1} = $2; $self->{queries}->{$1} = $2;
push @{$self->{maps_counters}->{expressions}[0]->{set}->{key_values}}, { name => $1 }; push @{$self->{maps_counters}->{expressions}->[0]->{set}->{key_values}}, { name => $1 };
push @{$self->{custom_keys}}, $1; push @{$self->{custom_keys}}, $1;
} }
@ -135,16 +149,14 @@ sub check_options {
foreach my $query (@{$self->{option_results}->{query_range}}) { foreach my $query (@{$self->{option_results}->{query_range}}) {
next if ($query !~ /^(\w+),(.*)/); next if ($query !~ /^(\w+),(.*)/);
$self->{query_ranges}->{$1} = $2; $self->{query_ranges}->{$1} = $2;
push @{$self->{maps_counters}->{expressions}[0]->{set}->{key_values}}, { name => $1 }; push @{$self->{maps_counters}->{expressions}->[0]->{set}->{key_values}}, { name => $1 };
push @{$self->{custom_keys}}, $1; push @{$self->{custom_keys}}, $1;
} }
$self->{maps_counters_type}[0]->{message_multiple} = $self->{option_results}->{multiple_output} if (defined($self->{option_results}->{multiple_output})); $self->{maps_counters_type}->[0]->{message_multiple} = $self->{option_results}->{multiple_output} if (defined($self->{option_results}->{multiple_output}));
$self->{prom_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 900; $self->{prom_timeframe} = defined($self->{option_results}->{timeframe}) ? $self->{option_results}->{timeframe} : 900;
$self->{prom_step} = defined($self->{option_results}->{step}) ? $self->{option_results}->{step} : "1m"; $self->{prom_step} = defined($self->{option_results}->{step}) ? $self->{option_results}->{step} : '1m';
$self->change_macros(macros => ['warning_status', 'critical_status']);
} }
sub manage_selection { sub manage_selection {
@ -162,19 +174,28 @@ sub manage_selection {
push @query_ranges, $prom_query; push @query_ranges, $prom_query;
} }
my $queries_results = $options{custom}->query(queries => \@queries) if (scalar(@queries) > 0); my $queries_results = $options{custom}->query(queries => \@queries) if (scalar(@queries) > 0);
my $query_ranges_results = $options{custom}->query_range(queries => \@query_ranges, timeframe => $self->{prom_timeframe}, step => $self->{prom_step}) if (scalar(@query_ranges) > 0); my $query_ranges_results = $options{custom}->query_range(queries => \@query_ranges, timeframe => $self->{prom_timeframe}, step => $self->{prom_step}) if (scalar(@query_ranges) > 0);
push @results, @{$queries_results} if (defined($queries_results)); push @results, @{$queries_results} if (defined($queries_results));
push @results, @{$query_ranges_results} if (defined($query_ranges_results)); push @results, @{$query_ranges_results} if (defined($query_ranges_results));
foreach my $result (@results) { foreach my $result (@results) {
next if (!defined($result->{metric}->{$self->{option_results}->{instance}})); my $instances = [];
foreach (@{$self->{instances}}) {
next if (!defined($result->{metric}->{$_}));
push @$instances, { name => $_, value => $result->{metric}->{$_} };
}
my $instance_key = join('_', @$instances);
next if (!defined($instance_key) || $instance_key eq '');
my $value; my $value;
$value = $options{custom}->compute(aggregation => $self->{option_results}->{aggregation}, values => $result->{values}) if (defined($result->{values})); $value = $options{custom}->compute(aggregation => $self->{option_results}->{aggregation}, values => $result->{values}) if (defined($result->{values}));
$value = ${$result->{value}}[1] if (defined($result->{value})); $value = $result->{value}->[1] if (defined($result->{value}));
$self->{expressions}->{$result->{metric}->{$self->{option_results}->{instance}}}->{instance} = $result->{metric}->{$self->{option_results}->{instance}}; $self->{expressions}->{$instance_key} = {
$self->{expressions}->{$result->{metric}->{$self->{option_results}->{instance}}}->{$result->{metric}->{__name__}} = $value; instances => $instances,
$result->{metric}->{__name__} => $value
};
} }
if (scalar(keys %{$self->{expressions}}) <= 0) { if (scalar(keys %{$self->{expressions}}) <= 0) {

View File

@ -29,14 +29,13 @@ sub new {
my $self = $class->SUPER::new(package => __PACKAGE__, %options); my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class; bless $self, $class;
$self->{version} = '0.1'; $self->{modes} = {
%{$self->{modes}} = ( 'discovery' => 'cloud::prometheus::restapi::mode::discovery',
'discovery' => 'cloud::prometheus::restapi::mode::discovery', 'expression' => 'cloud::prometheus::restapi::mode::expression',
'expression' => 'cloud::prometheus::restapi::mode::expression', 'target-status' => 'cloud::prometheus::restapi::mode::targetstatus'
'target-status' => 'cloud::prometheus::restapi::mode::targetstatus', };
);
$self->{custom_modes}{api} = 'cloud::prometheus::restapi::custom::api'; $self->{custom_modes}->{api} = 'cloud::prometheus::restapi::custom::api';
return $self; return $self;
} }