Skip to content

Commit

Permalink
[MON-21523] Plugin mode not-so-dummy - Add min and max duration to si…
Browse files Browse the repository at this point in the history
…mulate plugin execution time (#4674)
  • Loading branch information
lucie-dubrunfaut authored Oct 2, 2023
1 parent 00a2455 commit 6237679
Showing 1 changed file with 50 additions and 3 deletions.
53 changes: 50 additions & 3 deletions src/apps/centreon/local/mode/notsodummy.pm
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use base qw(centreon::plugins::mode);

use strict;
use warnings;
use Time::HiRes;
use centreon::plugins::statefile;
use Digest::MD5 qw(md5_hex);

Expand All @@ -46,6 +47,8 @@ sub new {
"show-sequence" => { name => 'show_sequence' },
"show-index" => { name => 'show_index' },
"restart-sequence" => { name => 'restart_sequence' },
"min-duration:s" => { name => 'min_duration' },
"max-duration:s" => { name => 'max_duration' },
});

$self->{cache} = centreon::plugins::statefile->new(%options);
Expand Down Expand Up @@ -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}->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});
}
Expand Down Expand Up @@ -154,6 +182,13 @@ sub get_sequence_output {
sub run {
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_label = $status;
if (defined($self->{option_results}->{host})) {
Expand Down Expand Up @@ -220,15 +255,15 @@ to defined the way cache file is managed.
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'
--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
--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'
--metrics-count=5 --metrics-name='met.rics' --metrics-values-range='-15:42'
Expand Down Expand Up @@ -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).
=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
=cut

0 comments on commit 6237679

Please sign in to comment.