This commit is contained in:
garnier-quentin 2020-01-29 13:43:53 +01:00
parent e69ecb43b8
commit ad86fb2092
1 changed files with 38 additions and 27 deletions

View File

@ -26,6 +26,7 @@ use centreon::plugins::http;
use DateTime; use DateTime;
use JSON::XS; use JSON::XS;
use URI::Encode; use URI::Encode;
use centreon::plugins::misc;
sub new { sub new {
my ($class, %options) = @_; 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}->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 => {
"api-key:s" => { name => 'api_key' }, 'api-key:s' => { name => 'api_key' },
"hostname:s" => { name => 'hostname' }, 'hostname:s' => { name => 'hostname' },
"url-path:s" => { name => 'url_path' }, 'url-path:s' => { name => 'url_path' },
"port:s" => { name => 'port' }, 'port:s' => { name => 'port' },
"proto:s" => { name => 'proto' }, 'proto:s' => { name => 'proto' },
"credentials" => { name => 'credentials' }, 'credentials' => { name => 'credentials' },
"basic" => { name => 'basic' }, 'basic' => { name => 'basic' },
"username:s" => { name => 'username' }, 'username:s' => { name => 'username' },
"password:s" => { name => 'password' }, 'password:s' => { name => 'password' },
"timeout:s" => { name => 'timeout' }, 'timeout:s' => { name => 'timeout' },
"timeframe:s" => { name => 'timeframe' }, 'timeframe:s' => { name => 'timeframe' },
'timezone:s' => { name => 'timezone', default => 'UTC' }
}); });
} }
$options{options}->add_help(package => __PACKAGE__, sections => 'RESTAPI OPTIONS', once => 1); $options{options}->add_help(package => __PACKAGE__, sections => 'RESTAPI OPTIONS', once => 1);
@ -63,7 +65,6 @@ sub new {
$self->{http} = centreon::plugins::http->new(%options); $self->{http} = centreon::plugins::http->new(%options);
return $self; return $self;
} }
sub set_options { 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}->add_option_msg(short_msg => "Need to specify api-key option.");
$self->{output}->option_exit(); $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; return 0;
} }
@ -142,37 +148,37 @@ 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};
} }
sub query_range { sub query_range {
my ($self, %options) = @_; my ($self, %options) = @_;
my $data; my $tz = centreon::plugins::misc::set_timezone(name => $self->{timezone});
my $start_time = DateTime->now->subtract(seconds => $options{timeframe})->epoch; my $dt = DateTime->now(%$tz);
my $end_time = DateTime->now->epoch; my $start_time = $dt->subtract(seconds => $options{timeframe})->epoch;
my $end_time = $dt->now->epoch;
my $uri = URI::Encode->new({encode_reserved => 1}); my $uri = URI::Encode->new({encode_reserved => 1});
my $query = sprintf("%s SINCE %s UNTIL %s", $options{query}, $start_time, $end_time); 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(' 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(' 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(' FROM %s', $options{from}) if (defined($options{from}) && $options{from} ne '');
$query .= sprintf(" TOP %s", $options{top}) if (defined($options{top}) && $options{top} 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)); my $result = $self->get_endpoint(url_path => '/query?expr=' . $uri->encode($query));
return $result->{data}; return $result->{data};
@ -197,7 +203,7 @@ sub get_endpoint {
$self->{output}->add_option_msg(short_msg => "Cannot get data: " . $content->{error}); $self->{output}->add_option_msg(short_msg => "Cannot get data: " . $content->{error});
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
return $content->{result}; return $content->{result};
} }
@ -221,6 +227,11 @@ PVX Rest API custom mode
Set timeframe in seconds (i.e. 3600 to check last hour). 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> =item B<--api-key>
PVX API key. PVX API key.