(plugin) cloud::prometheus::restapi - mode expressions can use multiple instance option (#4262)
This commit is contained in:
parent
334bfa698b
commit
b69238bcbb
|
@ -40,7 +40,7 @@ sub new {
|
|||
$options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument.");
|
||||
$options{output}->option_exit();
|
||||
}
|
||||
|
||||
|
||||
if (!defined($options{noptions})) {
|
||||
$options{options}->add_options(arguments => {
|
||||
'hostname:s' => { name => 'hostname' },
|
||||
|
@ -117,19 +117,19 @@ sub settings {
|
|||
|
||||
sub get_connection_info {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
|
||||
return $self->{hostname} . ":" . $self->{port};
|
||||
}
|
||||
|
||||
sub get_hostname {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
|
||||
return $self->{hostname};
|
||||
}
|
||||
|
||||
sub get_port {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
|
||||
return $self->{port};
|
||||
}
|
||||
|
||||
|
@ -168,9 +168,8 @@ sub get_endpoint {
|
|||
my ($self, %options) = @_;
|
||||
|
||||
$self->settings();
|
||||
|
||||
my $response = $self->{http}->request(url_path => $self->{url_path} . $options{url_path});
|
||||
|
||||
|
||||
my $content;
|
||||
eval {
|
||||
$content = JSON::XS->new->utf8->decode($response);
|
||||
|
|
|
@ -24,17 +24,21 @@ use base qw(centreon::plugins::templates::counter);
|
|||
|
||||
use strict;
|
||||
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 {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
|
||||
my $instances = [];
|
||||
foreach (@{$self->{result_values}->{instances}}) {
|
||||
push @$instances, $_->{value};
|
||||
}
|
||||
|
||||
foreach my $key (@{$self->{instance_mode}->{custom_keys}}) {
|
||||
$self->{output}->perfdata_add(
|
||||
label => $key,
|
||||
nlabel => $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 {
|
||||
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}}) {
|
||||
$self->{result_values}->{$key} = $options{new_datas}->{$self->{instance} . '_' . $key};
|
||||
$self->{result_values}->{$key} = $options{new_datas}->{ $self->{instance} . '_' . $key };
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
@ -66,38 +75,35 @@ sub custom_status_calc {
|
|||
|
||||
sub set_counters {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
|
||||
$self->{maps_counters_type} = [
|
||||
{ name => 'expressions', type => 1,
|
||||
message_multiple => 'All expressions results are ok', skipped_code => { -11 => 1 } },
|
||||
{ name => 'expressions', type => 1, message_multiple => 'All expressions results are ok', skipped_code => { -11 => 1 } }
|
||||
];
|
||||
|
||||
$self->{maps_counters}->{expressions} = [
|
||||
{ label => 'status', set => {
|
||||
key_values => [ { name => 'instance' } ],
|
||||
{ label => 'status', type => 2, set => {
|
||||
key_values => [ { name => 'instances' } ],
|
||||
closure_custom_calc => $self->can('custom_status_calc'),
|
||||
closure_custom_output => $self->can('custom_status_output'),
|
||||
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 {
|
||||
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;
|
||||
|
||||
|
||||
$options{options}->add_options(arguments => {
|
||||
"query:s@" => { name => 'query' },
|
||||
"query-range:s@" => { name => 'query_range' },
|
||||
"instance:s" => { name => 'instance' },
|
||||
"aggregation:s" => { name => 'aggregation', default => 'average' },
|
||||
"output:s" => { name => 'output' },
|
||||
"multiple-output:s" => { name => 'multiple_output' },
|
||||
"warning-status:s" => { name => 'warning_status', default => '' },
|
||||
"critical-status:s" => { name => 'critical_status', default => '' },
|
||||
'query:s@' => { name => 'query' },
|
||||
'query-range:s@' => { name => 'query_range' },
|
||||
'instance:s@' => { name => 'instance' },
|
||||
'aggregation:s' => { name => 'aggregation', default => 'average' },
|
||||
'output:s' => { name => 'output' },
|
||||
'multiple-output:s' => { name => 'multiple_output' }
|
||||
});
|
||||
|
||||
return $self;
|
||||
|
@ -111,12 +117,20 @@ sub check_options {
|
|||
$self->{output}->add_option_msg(short_msg => "Need to specify output option.");
|
||||
$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}->option_exit();
|
||||
}
|
||||
|
||||
|
||||
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}->option_exit();
|
||||
|
@ -127,7 +141,7 @@ sub check_options {
|
|||
foreach my $query (@{$self->{option_results}->{query}}) {
|
||||
next if ($query !~ /^(\w+),(.*)/);
|
||||
$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;
|
||||
}
|
||||
|
||||
|
@ -135,16 +149,14 @@ sub check_options {
|
|||
foreach my $query (@{$self->{option_results}->{query_range}}) {
|
||||
next if ($query !~ /^(\w+),(.*)/);
|
||||
$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;
|
||||
}
|
||||
|
||||
$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_step} = defined($self->{option_results}->{step}) ? $self->{option_results}->{step} : "1m";
|
||||
|
||||
$self->change_macros(macros => ['warning_status', 'critical_status']);
|
||||
$self->{prom_step} = defined($self->{option_results}->{step}) ? $self->{option_results}->{step} : '1m';
|
||||
}
|
||||
|
||||
sub manage_selection {
|
||||
|
@ -162,19 +174,28 @@ sub manage_selection {
|
|||
push @query_ranges, $prom_query;
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
push @results, @{$queries_results} if (defined($queries_results));
|
||||
push @results, @{$query_ranges_results} if (defined($query_ranges_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;
|
||||
$value = $options{custom}->compute(aggregation => $self->{option_results}->{aggregation}, values => $result->{values}) if (defined($result->{values}));
|
||||
$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}->{$result->{metric}->{$self->{option_results}->{instance}}}->{$result->{metric}->{__name__}} = $value;
|
||||
$value = $result->{value}->[1] if (defined($result->{value}));
|
||||
$self->{expressions}->{$instance_key} = {
|
||||
instances => $instances,
|
||||
$result->{metric}->{__name__} => $value
|
||||
};
|
||||
}
|
||||
|
||||
if (scalar(keys %{$self->{expressions}}) <= 0) {
|
||||
|
|
|
@ -29,14 +29,13 @@ sub new {
|
|||
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
|
||||
bless $self, $class;
|
||||
|
||||
$self->{version} = '0.1';
|
||||
%{$self->{modes}} = (
|
||||
'discovery' => 'cloud::prometheus::restapi::mode::discovery',
|
||||
'expression' => 'cloud::prometheus::restapi::mode::expression',
|
||||
'target-status' => 'cloud::prometheus::restapi::mode::targetstatus',
|
||||
);
|
||||
$self->{modes} = {
|
||||
'discovery' => 'cloud::prometheus::restapi::mode::discovery',
|
||||
'expression' => 'cloud::prometheus::restapi::mode::expression',
|
||||
'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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue