[MON-21523] Plugin mode not-so-dummy - Add min and max duration to simulate plugin execution time (#4674)

This commit is contained in:
Lucie Dubrunfaut 2023-10-02 11:36:01 +02:00 committed by GitHub
parent 00a2455486
commit 6237679198
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -24,6 +24,7 @@ use base qw(centreon::plugins::mode);
use strict; use strict;
use warnings; use warnings;
use Time::HiRes;
use centreon::plugins::statefile; use centreon::plugins::statefile;
use Digest::MD5 qw(md5_hex); use Digest::MD5 qw(md5_hex);
@ -46,6 +47,8 @@ sub new {
"show-sequence" => { name => 'show_sequence' }, "show-sequence" => { name => 'show_sequence' },
"show-index" => { name => 'show_index' }, "show-index" => { name => 'show_index' },
"restart-sequence" => { name => 'restart_sequence' }, "restart-sequence" => { name => 'restart_sequence' },
"min-duration:s" => { name => 'min_duration' },
"max-duration:s" => { name => 'max_duration' },
}); });
$self->{cache} = centreon::plugins::statefile->new(%options); $self->{cache} = centreon::plugins::statefile->new(%options);
@ -100,6 +103,31 @@ sub check_options {
$self->{output}->add_option_msg(short_msg => "Need to specify --metrics-values-range where range start is lower than range end."); $self->{output}->add_option_msg(short_msg => "Need to specify --metrics-values-range where range start is lower than range end.");
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
if (defined($self->{option_results}->{min_duration})) {
if (!defined($self->{option_results}->{max_duration}) || $self->{option_results}->{max_duration} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify --max-duration option if --min_duration is set.");
$self->{output}->option_exit();
}
if ($self->{option_results}->{min_duration} !~ /^[0-9]*\.?[0-9]*$/) {
$self->{output}->add_option_msg(short_msg => "--min-duration should be a number.");
$self->{output}->option_exit();
}
}
if (defined($self->{option_results}->{max_duration})){
if (!defined($self->{option_results}->{min_duration}) || $self->{option_results}->{min_duration} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify --min-duration option if --max_duration is set.");
$self->{output}->option_exit();
}
if ($self-> {option_results}->{max_duration} !~ /^[0-9]*\.?[0-9]*$/){
$self->{output}->add_option_msg(short_msg => "--max-duration should be a number.");
$self->{output}->option_exit();
}
if ($self-> {option_results}->{max_duration} <= $self->{option_results}->{min_duration} ){
$self->{output}->add_option_msg(short_msg => "--max-duration should be higher than min-duration.");
$self->{output}->option_exit();
}
}
$self->{cache}->check_options(option_results => $self->{option_results}); $self->{cache}->check_options(option_results => $self->{option_results});
} }
@ -154,6 +182,13 @@ sub get_sequence_output {
sub run { sub run {
my ($self, %options) = @_; my ($self, %options) = @_;
# Adding plugin simulated duration
# rand(n) returns a random number between 0 and n value is exclude.
if (defined($self->{option_results}->{min_duration}) && defined($self->{option_results}->{max_duration})) {
my $sleep_duration = $self->{option_results}->{min_duration} + rand($self->{option_results}->{max_duration} - $self->{option_results}->{min_duration});
Time::HiRes::sleep($sleep_duration);
}
my ($status, $index) = $self->get_next_status(statefile => $self->{cache}); my ($status, $index) = $self->get_next_status(statefile => $self->{cache});
my $status_label = $status; my $status_label = $status;
if (defined($self->{option_results}->{host})) { if (defined($self->{option_results}->{host})) {
@ -220,15 +255,15 @@ to defined the way cache file is managed.
Examples: Examples:
perl centreon_plugin.pl --plugin=apps::centreon::local::plugin perl centreon_plugins.pl --plugin=apps::centreon::local::plugin
--mode=not-so-dummy --status-sequence='ok,warning,ok,critical,critical,critical' --mode=not-so-dummy --status-sequence='ok,warning,ok,critical,critical,critical'
--output='Not so dummy service' --show-sequence --statefile-dir='/tmp' --output='Not so dummy service' --show-sequence --statefile-dir='/tmp'
perl centreon_plugin.pl --plugin=apps::centreon::local::plugin perl centreon_plugins.pl --plugin=apps::centreon::local::plugin
--mode=not-so-dummy --status-sequence='up,down,down' --host --mode=not-so-dummy --status-sequence='up,down,down' --host
--output='Not so dummy host' --output='Not so dummy host'
perl centreon_plugin.pl --plugin=apps::centreon::local::plugin perl centreon_plugins.pl --plugin=apps::centreon::local::plugin
--mode=not-so-dummy --status-sequence='ok,ok,ok' --output='Not so dummy' --mode=not-so-dummy --status-sequence='ok,ok,ok' --output='Not so dummy'
--metrics-count=5 --metrics-name='met.rics' --metrics-values-range='-15:42' --metrics-count=5 --metrics-name='met.rics' --metrics-values-range='-15:42'
@ -275,6 +310,18 @@ Show the index as a metric (in addition to the defined metrics count).
Restart the sequence from the beginning (ie. reset the sequence in cache file). Restart the sequence from the beginning (ie. reset the sequence in cache file).
=item B<--min-duration>
Min duration thresholds (in seconds) use to set the range used to randomly simulate the execution of a plugin.
If this option is set, max-duration is mandatory.
The duration is chosen in the [min,max) range.
=item B<--max-duration>
Max duration thresholds (in seconds) use to set the range used to randomly simulate the execution of a plugin.
If this option is set, min-duration is mandatory.
The duration is chosen in the [min,max) range (max is excluded).
=back =back
=cut =cut