Fix #1822
This commit is contained in:
parent
e69ecb43b8
commit
ad86fb2092
|
@ -26,6 +26,7 @@ use centreon::plugins::http;
|
|||
use DateTime;
|
||||
use JSON::XS;
|
||||
use URI::Encode;
|
||||
use centreon::plugins::misc;
|
||||
|
||||
sub new {
|
||||
my ($class, %options) = @_;
|
||||
|
@ -40,20 +41,21 @@ 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 => {
|
||||
"api-key:s" => { name => 'api_key' },
|
||||
"hostname:s" => { name => 'hostname' },
|
||||
"url-path:s" => { name => 'url_path' },
|
||||
"port:s" => { name => 'port' },
|
||||
"proto:s" => { name => 'proto' },
|
||||
"credentials" => { name => 'credentials' },
|
||||
"basic" => { name => 'basic' },
|
||||
"username:s" => { name => 'username' },
|
||||
"password:s" => { name => 'password' },
|
||||
"timeout:s" => { name => 'timeout' },
|
||||
"timeframe:s" => { name => 'timeframe' },
|
||||
'api-key:s' => { name => 'api_key' },
|
||||
'hostname:s' => { name => 'hostname' },
|
||||
'url-path:s' => { name => 'url_path' },
|
||||
'port:s' => { name => 'port' },
|
||||
'proto:s' => { name => 'proto' },
|
||||
'credentials' => { name => 'credentials' },
|
||||
'basic' => { name => 'basic' },
|
||||
'username:s' => { name => 'username' },
|
||||
'password:s' => { name => 'password' },
|
||||
'timeout:s' => { name => 'timeout' },
|
||||
'timeframe:s' => { name => 'timeframe' },
|
||||
'timezone:s' => { name => 'timezone', default => 'UTC' }
|
||||
});
|
||||
}
|
||||
$options{options}->add_help(package => __PACKAGE__, sections => 'RESTAPI OPTIONS', once => 1);
|
||||
|
@ -63,7 +65,6 @@ sub new {
|
|||
$self->{http} = centreon::plugins::http->new(%options);
|
||||
|
||||
return $self;
|
||||
|
||||
}
|
||||
|
||||
sub set_options {
|
||||
|
@ -111,7 +112,12 @@ sub check_options {
|
|||
$self->{output}->add_option_msg(short_msg => "Need to specify api-key option.");
|
||||
$self->{output}->option_exit();
|
||||
}
|
||||
|
||||
|
||||
$self->{timezone} = 'UTC';
|
||||
if (defined($self->{option_results}->{timezone}) && $self->{option_results}->{timezone} ne '') {
|
||||
$self->{timezone} = $self->{option_results}->{timezone};
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -142,37 +148,37 @@ 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};
|
||||
}
|
||||
|
||||
sub query_range {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
my $data;
|
||||
my $start_time = DateTime->now->subtract(seconds => $options{timeframe})->epoch;
|
||||
my $end_time = DateTime->now->epoch;
|
||||
my $tz = centreon::plugins::misc::set_timezone(name => $self->{timezone});
|
||||
my $dt = DateTime->now(%$tz);
|
||||
my $start_time = $dt->subtract(seconds => $options{timeframe})->epoch;
|
||||
my $end_time = $dt->now->epoch;
|
||||
my $uri = URI::Encode->new({encode_reserved => 1});
|
||||
|
||||
my $query = sprintf("%s SINCE %s UNTIL %s", $options{query}, $start_time, $end_time);
|
||||
$query .= sprintf(" BY %s", $options{instance}) if (defined($options{instance}) && $options{instance} ne '');
|
||||
$query .= sprintf(" WHERE %s", $options{filter}) if (defined($options{filter}) && $options{filter} ne '');
|
||||
$query .= sprintf(" FROM %s", $options{from}) if (defined($options{from}) && $options{from} ne '');
|
||||
$query .= sprintf(" TOP %s", $options{top}) if (defined($options{top}) && $options{top} ne '');
|
||||
my $query = sprintf('%s SINCE %s UNTIL %s', $options{query}, $start_time, $end_time);
|
||||
$query .= sprintf(' BY %s', $options{instance}) if (defined($options{instance}) && $options{instance} ne '');
|
||||
$query .= sprintf(' WHERE %s', $options{filter}) if (defined($options{filter}) && $options{filter} ne '');
|
||||
$query .= sprintf(' FROM %s', $options{from}) if (defined($options{from}) && $options{from} ne '');
|
||||
$query .= sprintf(' TOP %s', $options{top}) if (defined($options{top}) && $options{top} ne '');
|
||||
|
||||
$self->{output}->output_add(long_msg => sprintf("Query: '/query?expr=%s'", $query), debug => 1);
|
||||
my $result = $self->get_endpoint(url_path => '/query?expr=' . $uri->encode($query));
|
||||
|
||||
return $result->{data};
|
||||
|
@ -197,7 +203,7 @@ sub get_endpoint {
|
|||
$self->{output}->add_option_msg(short_msg => "Cannot get data: " . $content->{error});
|
||||
$self->{output}->option_exit();
|
||||
}
|
||||
|
||||
|
||||
return $content->{result};
|
||||
}
|
||||
|
||||
|
@ -221,6 +227,11 @@ PVX Rest API custom mode
|
|||
|
||||
Set timeframe in seconds (i.e. 3600 to check last hour).
|
||||
|
||||
=item B<--timezone>
|
||||
|
||||
Set your timezone.
|
||||
Can use format: 'Europe/London' or '+0100'.
|
||||
|
||||
=item B<--api-key>
|
||||
|
||||
PVX API key.
|
||||
|
|
Loading…
Reference in New Issue