From bef7aed6f09a3bc840737638b8c2a79f3c2e20cb Mon Sep 17 00:00:00 2001 From: qgarnier Date: Tue, 12 Jan 2021 15:20:25 +0100 Subject: [PATCH] Fix #1888 (#2502) --- .../elasticsearch/restapi/mode/license.pm | 56 +++++++------------ database/elasticsearch/restapi/plugin.pm | 9 +-- 2 files changed, 25 insertions(+), 40 deletions(-) diff --git a/database/elasticsearch/restapi/mode/license.pm b/database/elasticsearch/restapi/mode/license.pm index d91d3237a..b9756b991 100644 --- a/database/elasticsearch/restapi/mode/license.pm +++ b/database/elasticsearch/restapi/mode/license.pm @@ -24,27 +24,18 @@ 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_output { my ($self, %options) = @_; - my $msg = sprintf("License Status '%s' [type: %s] [issued to: %s] [issue date: %s]", + + return sprintf( + "License Status '%s' [type: %s] [issued to: %s] [issue date: %s]", $self->{result_values}->{status}, $self->{result_values}->{type}, $self->{result_values}->{issued_to}, - $self->{result_values}->{issue_date}); - - return $msg; -} - -sub custom_status_calc { - my ($self, %options) = @_; - - $self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_status'}; - $self->{result_values}->{type} = $options{new_datas}->{$self->{instance} . '_type'}; - $self->{result_values}->{issued_to} = $options{new_datas}->{$self->{instance} . '_issued_to'}; - $self->{result_values}->{issue_date} = $options{new_datas}->{$self->{instance} . '_issue_date'}; - return 0; + $self->{result_values}->{issue_date} + ); } sub set_counters { @@ -55,13 +46,14 @@ sub set_counters { ]; $self->{maps_counters}->{global} = [ - { label => 'status', threshold => 0, set => { - key_values => [ { name => 'status' }, { name => 'type' }, { name => 'issued_to' }, - { name => 'issue_date' } ], - closure_custom_calc => $self->can('custom_status_calc'), + { label => 'status', type => 2, critical_default => '%{status} !~ /active/i', set => { + key_values => [ + { name => 'status' }, { name => 'type' }, { name => 'issued_to' }, + { name => 'issue_date' }, { name => 'expiry_date_in_seconds' } + ], closure_custom_output => $self->can('custom_status_output'), closure_custom_perfdata => sub { return 0; }, - closure_custom_threshold_check => \&catalog_status_threshold, + closure_custom_threshold_check => \&catalog_status_threshold_ng } }, ]; @@ -71,32 +63,24 @@ sub new { my ($class, %options) = @_; my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); bless $self, $class; - + $options{options}->add_options(arguments => { - "warning-status:s" => { name => 'warning_status', default => '' }, - "critical-status:s" => { name => 'critical_status', default => '%{status} !~ /active/i' }, }); - + return $self; } -sub check_options { - my ($self, %options) = @_; - $self->SUPER::check_options(%options); - - $self->change_macros(macros => ['warning_status', 'critical_status']); -} - sub manage_selection { my ($self, %options) = @_; - + my $result = $options{custom}->get(path => '/_license'); - + $self->{global} = { type => $result->{license}->{type}, status => $result->{license}->{status}, issued_to => $result->{license}->{issued_to}, issue_date => $result->{license}->{issue_date}, + expiry_date_in_seconds => int($result->{license}->{expiry_date_in_millis} / 1000) }; } @@ -112,13 +96,13 @@ Check license. =item B<--warning-status> -Set warning threshold for status (Default: '') -Can used special variables like: %{status}, %{type}, %{issued_to}. +Set warning threshold for status. +Can used special variables like: %{status}, %{type}, %{issued_to}, %{expiry_date_in_seconds}. =item B<--critical-status> Set critical threshold for status (Default: '%{status} !~ /active/i'). -Can used special variables like: %{status}, %{type}, %{issued_to}. +Can used special variables like: %{status}, %{type}, %{issued_to}, %{expiry_date_in_seconds}. =back diff --git a/database/elasticsearch/restapi/plugin.pm b/database/elasticsearch/restapi/plugin.pm index 2cb868bfc..4bcb76781 100644 --- a/database/elasticsearch/restapi/plugin.pm +++ b/database/elasticsearch/restapi/plugin.pm @@ -30,15 +30,16 @@ sub new { bless $self, $class; $self->{version} = '0.1'; - %{$self->{modes}} = ( + $self->{modes} = { 'cluster-statistics' => 'database::elasticsearch::restapi::mode::clusterstatistics', 'indice-statistics' => 'database::elasticsearch::restapi::mode::indicestatistics', 'license' => 'database::elasticsearch::restapi::mode::license', 'list-indices' => 'database::elasticsearch::restapi::mode::listindices', 'list-nodes' => 'database::elasticsearch::restapi::mode::listnodes', - 'node-statistics' => 'database::elasticsearch::restapi::mode::nodestatistics', - ); - $self->{custom_modes}{api} = 'database::elasticsearch::restapi::custom::api'; + 'node-statistics' => 'database::elasticsearch::restapi::mode::nodestatistics' + }; + + $self->{custom_modes}->{api} = 'database::elasticsearch::restapi::custom::api'; return $self; }