fix prometheus api (#1190)
* fix prometheus api custom mode * enh prometheus timeframe/step options
This commit is contained in:
parent
df161b3d04
commit
9c7165e6de
|
@ -100,6 +100,9 @@ sub check_options {
|
|||
next if ($metric !~ /(.*),(.*)/);
|
||||
$self->{metrics}->{$1} = $2 if (defined($self->{metrics}->{$1}));
|
||||
}
|
||||
|
||||
$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";
|
||||
}
|
||||
|
||||
sub manage_selection {
|
||||
|
@ -114,7 +117,8 @@ sub manage_selection {
|
|||
}
|
||||
|
||||
my $results = $options{custom}->query_range(queries => [ '(1 - irate({__name__=~"' . $self->{metrics}->{cpu} . '",mode="idle",instance=~"' . $self->{option_results}->{node} .
|
||||
'"' . $extra_filter . '}[1m])) * 100' ]);
|
||||
'"' . $extra_filter . '}[1m])) * 100' ],
|
||||
timeframe => $self->{prom_timeframe}, step => $self->{prom_step});
|
||||
|
||||
foreach my $metric (@{$results}) {
|
||||
my $average = $options{custom}->compute(aggregation => 'average', values => $metric->{values});
|
||||
|
|
|
@ -239,6 +239,9 @@ sub check_options {
|
|||
next if ($metric !~ /(.*),(.*)/);
|
||||
$self->{metrics}->{$1} = $2 if (defined($self->{metrics}->{$1}));
|
||||
}
|
||||
|
||||
$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";
|
||||
}
|
||||
|
||||
sub manage_selection {
|
||||
|
@ -252,7 +255,8 @@ sub manage_selection {
|
|||
}
|
||||
|
||||
my $results = $options{custom}->query_range(queries => [ '(irate({__name__=~"' . $self->{metrics}->{cpu} . '",instance=~"' . $self->{option_results}->{node} .
|
||||
'"' . $extra_filter . '}[1m])) * 100' ]);
|
||||
'"' . $extra_filter . '}[1m])) * 100' ],
|
||||
timeframe => $self->{prom_timeframe}, step => $self->{prom_step});
|
||||
|
||||
foreach my $metric (@{$results}) {
|
||||
my $average = $options{custom}->compute(aggregation => 'average', values => $metric->{values});
|
||||
|
|
|
@ -114,17 +114,16 @@ sub manage_selection {
|
|||
$extra_filter .= ',' . $filter;
|
||||
}
|
||||
|
||||
my $results = $options{custom}->query_range(queries => [ 'label_replace({__name__=~"' . $self->{metrics}->{load1} . '",instance=~"' . $self->{option_results}->{node} .
|
||||
'"' . $extra_filter . '}, "__name__", "load1", "", "")',
|
||||
'label_replace({__name__=~"' . $self->{metrics}->{load5} . '",instance=~"' . $self->{option_results}->{node} .
|
||||
'"' . $extra_filter . '}, "__name__", "load5", "", "")',
|
||||
'label_replace({__name__=~"' . $self->{metrics}->{load15} . '",instance=~"' . $self->{option_results}->{node} .
|
||||
'"' . $extra_filter . '}, "__name__", "load15", "", "")' ]);
|
||||
my $results = $options{custom}->query(queries => [ 'label_replace({__name__=~"' . $self->{metrics}->{load1} . '",instance=~"' . $self->{option_results}->{node} .
|
||||
'"' . $extra_filter . '}, "__name__", "load1", "", "")',
|
||||
'label_replace({__name__=~"' . $self->{metrics}->{load5} . '",instance=~"' . $self->{option_results}->{node} .
|
||||
'"' . $extra_filter . '}, "__name__", "load5", "", "")',
|
||||
'label_replace({__name__=~"' . $self->{metrics}->{load15} . '",instance=~"' . $self->{option_results}->{node} .
|
||||
'"' . $extra_filter . '}, "__name__", "load15", "", "")' ]);
|
||||
|
||||
foreach my $metric (@{$results}) {
|
||||
my $average = $options{custom}->compute(aggregation => 'average', values => $metric->{values});
|
||||
$self->{nodes}->{$metric->{metric}->{instance}}->{display} = $metric->{metric}->{instance};
|
||||
$self->{nodes}->{$metric->{metric}->{instance}}->{$metric->{metric}->{__name__}} = $average;
|
||||
$self->{nodes}->{$metric->{metric}->{instance}}->{$metric->{metric}->{__name__}} = ${$metric->{value}}[1];
|
||||
}
|
||||
|
||||
if (scalar(keys %{$self->{nodes}}) <= 0) {
|
||||
|
|
|
@ -177,19 +177,18 @@ sub manage_selection {
|
|||
$extra_filter .= ',' . $filter;
|
||||
}
|
||||
|
||||
my $results = $options{custom}->query_range(queries => [ 'label_replace({__name__=~"' . $self->{metrics}->{total} . '",instance=~"' . $self->{option_results}->{node} .
|
||||
'"' . $extra_filter . '}, "__name__", "total", "", "")',
|
||||
'label_replace({__name__=~"' . $self->{metrics}->{available} . '",instance=~"' . $self->{option_results}->{node} .
|
||||
'"' . $extra_filter . '}, "__name__", "available", "", "")',
|
||||
'label_replace({__name__=~"' . $self->{metrics}->{cached} . '",instance=~"' . $self->{option_results}->{node} .
|
||||
'"' . $extra_filter . '}, "__name__", "cached", "", "")',
|
||||
'label_replace({__name__=~"' . $self->{metrics}->{buffer} . '",instance=~"' . $self->{option_results}->{node} .
|
||||
'"' . $extra_filter . '}, "__name__", "buffer", "", "")' ]);
|
||||
my $results = $options{custom}->query(queries => [ 'label_replace({__name__=~"' . $self->{metrics}->{total} . '",instance=~"' . $self->{option_results}->{node} .
|
||||
'"' . $extra_filter . '}, "__name__", "total", "", "")',
|
||||
'label_replace({__name__=~"' . $self->{metrics}->{available} . '",instance=~"' . $self->{option_results}->{node} .
|
||||
'"' . $extra_filter . '}, "__name__", "available", "", "")',
|
||||
'label_replace({__name__=~"' . $self->{metrics}->{cached} . '",instance=~"' . $self->{option_results}->{node} .
|
||||
'"' . $extra_filter . '}, "__name__", "cached", "", "")',
|
||||
'label_replace({__name__=~"' . $self->{metrics}->{buffer} . '",instance=~"' . $self->{option_results}->{node} .
|
||||
'"' . $extra_filter . '}, "__name__", "buffer", "", "")' ]);
|
||||
|
||||
foreach my $metric (@{$results}) {
|
||||
my $average = $options{custom}->compute(aggregation => 'average', values => $metric->{values});
|
||||
$self->{nodes}->{$metric->{metric}->{instance}}->{display} = $metric->{metric}->{instance};
|
||||
$self->{nodes}->{$metric->{metric}->{instance}}->{$metric->{metric}->{__name__}} = $average;
|
||||
$self->{nodes}->{$metric->{metric}->{instance}}->{$metric->{metric}->{__name__}} = ${$metric->{value}}[1];
|
||||
}
|
||||
|
||||
if (scalar(keys %{$self->{nodes}}) <= 0) {
|
||||
|
|
|
@ -48,7 +48,7 @@ sub new {
|
|||
"url-path:s" => { name => 'url_path' },
|
||||
"port:s" => { name => 'port' },
|
||||
"proto:s" => { name => 'proto' },
|
||||
"credentials" => { name => 'credentials' },
|
||||
"credentials" => { name => 'credentials' },
|
||||
"basic" => { name => 'basic' },
|
||||
"username:s" => { name => 'username' },
|
||||
"password:s" => { name => 'password' },
|
||||
|
@ -94,7 +94,7 @@ sub set_defaults {
|
|||
sub check_options {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
$self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : 'undef';
|
||||
$self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : undef;
|
||||
$self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 9090;
|
||||
$self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'http';
|
||||
$self->{url_path} = (defined($self->{option_results}->{url_path})) ? $self->{option_results}->{url_path} : '/api/v1';
|
||||
|
@ -108,21 +108,11 @@ sub check_options {
|
|||
$self->{timeframe} = (defined($self->{option_results}->{timeframe})) ? $self->{option_results}->{timeframe} : undef;
|
||||
$self->{step} = (defined($self->{option_results}->{step})) ? $self->{option_results}->{step} : undef;
|
||||
|
||||
if (!defined($self->{hostname}) && $self->{hostname} ne '') {
|
||||
if (!defined($self->{hostname}) || $self->{hostname} eq '') {
|
||||
$self->{output}->add_option_msg(short_msg => "Need to specify hostname option.");
|
||||
$self->{output}->option_exit();
|
||||
}
|
||||
|
||||
if (!defined($self->{timeframe}) && $self->{timeframe} ne '') {
|
||||
$self->{output}->add_option_msg(short_msg => "Need to specify timeframe option.");
|
||||
$self->{output}->option_exit();
|
||||
}
|
||||
|
||||
if (!defined($self->{step}) && $self->{step} ne '') {
|
||||
$self->{output}->add_option_msg(short_msg => "Need to specify step option.");
|
||||
$self->{output}->option_exit();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -171,15 +161,15 @@ sub query_range {
|
|||
my ($self, %options) = @_;
|
||||
|
||||
my $data;
|
||||
my $start_time = DateTime->now->subtract(seconds => $self->{timeframe})->iso8601.'Z';
|
||||
my $start_time = DateTime->now->subtract(seconds => $options{timeframe})->iso8601.'Z';
|
||||
my $end_time = DateTime->now->iso8601.'Z';
|
||||
my $uri = URI::Encode->new({encode_reserved => 1});
|
||||
|
||||
foreach my $query (@{$options{queries}}) {
|
||||
$self->{output}->output_add(long_msg => sprintf("Query range: '/query_range?query=%s&start=%s&end=%s&step=%s'",
|
||||
$query, $start_time, $end_time, $self->{step}), debug => 1);
|
||||
$query, $start_time, $end_time, $options{step}), debug => 1);
|
||||
my $result = $self->get_endpoint(url_path => '/query_range?query=' . $uri->encode($query) .
|
||||
'&start=' . $start_time . '&end=' . $end_time . '&step=' . $self->{step});
|
||||
'&start=' . $start_time . '&end=' . $end_time . '&step=' . $options{step});
|
||||
push @{$data}, @{$result->{result}};
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue