diff --git a/src/apps/centreon/local/mode/notsodummy.pm b/src/apps/centreon/local/mode/notsodummy.pm index ecdc0a40bc..7f765de5cd 100644 --- a/src/apps/centreon/local/mode/notsodummy.pm +++ b/src/apps/centreon/local/mode/notsodummy.pm @@ -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); @@ -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); @@ -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}); } @@ -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})) { @@ -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' @@ -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