@@ -24,6 +24,7 @@ use base qw(centreon::plugins::mode);
2424
2525use strict;
2626use warnings;
27+ use Time::HiRes;
2728use centreon::plugins::statefile;
2829use Digest::MD5 qw( md5_hex) ;
2930
@@ -46,6 +47,8 @@ sub new {
4647 " show-sequence" => { name => ' show_sequence' },
4748 " show-index" => { name => ' show_index' },
4849 " restart-sequence" => { name => ' restart_sequence' },
50+ " min-duration:s" => { name => ' min_duration' },
51+ " max-duration:s" => { name => ' max_duration' },
4952 });
5053
5154 $self -> {cache } = centreon::plugins::statefile-> new(%options );
@@ -100,6 +103,31 @@ sub check_options {
100103 $self -> {output }-> add_option_msg(short_msg => " Need to specify --metrics-values-range where range start is lower than range end." );
101104 $self -> {output }-> option_exit();
102105 }
106+
107+ if (defined ($self -> {option_results }-> {min_duration })) {
108+ if (!defined ($self -> {option_results }-> {max_duration }) || $self -> {option_results }-> {max_duration } eq ' ' ) {
109+ $self -> {output }-> add_option_msg(short_msg => " Need to specify --max-duration option if --min_duration is set." );
110+ $self -> {output }-> option_exit();
111+ }
112+ if ($self -> {option_results }-> {min_duration } !~ / ^[0-9]*\. ?[0-9]*$ / ) {
113+ $self -> {output }-> add_option_msg(short_msg => " --min-duration should be a number." );
114+ $self -> {output }-> option_exit();
115+ }
116+ }
117+ if (defined ($self -> {option_results }-> {max_duration })){
118+ if (!defined ($self -> {option_results }-> {min_duration }) || $self -> {option_results }-> {min_duration } eq ' ' ) {
119+ $self -> {output }-> add_option_msg(short_msg => " Need to specify --min-duration option if --max_duration is set." );
120+ $self -> {output }-> option_exit();
121+ }
122+ if ($self -> {option_results }-> {max_duration } !~ / ^[0-9]*\. ?[0-9]*$ / ){
123+ $self -> {output }-> add_option_msg(short_msg => " --max-duration should be a number." );
124+ $self -> {output }-> option_exit();
125+ }
126+ if ($self -> {option_results }-> {max_duration } <= $self -> {option_results }-> {min_duration } ){
127+ $self -> {output }-> add_option_msg(short_msg => " --max-duration should be higher than min-duration." );
128+ $self -> {output }-> option_exit();
129+ }
130+ }
103131
104132 $self -> {cache }-> check_options(option_results => $self -> {option_results });
105133}
@@ -154,6 +182,13 @@ sub get_sequence_output {
154182sub run {
155183 my ($self , %options ) = @_ ;
156184
185+ # Adding plugin simulated duration
186+ # rand(n) returns a random number between 0 and n value is exclude.
187+ if (defined ($self -> {option_results }-> {min_duration }) && defined ($self -> {option_results }-> {max_duration })) {
188+ my $sleep_duration = $self -> {option_results }-> {min_duration } + rand ($self -> {option_results }-> {max_duration } - $self -> {option_results }-> {min_duration });
189+ Time::HiRes::sleep ($sleep_duration );
190+ }
191+
157192 my ($status , $index ) = $self -> get_next_status(statefile => $self -> {cache });
158193 my $status_label = $status ;
159194 if (defined ($self -> {option_results }-> {host })) {
@@ -220,15 +255,15 @@ to defined the way cache file is managed.
220255
221256Examples:
222257
223- perl centreon_plugin .pl --plugin=apps::centreon::local::plugin
258+ perl centreon_plugins .pl --plugin=apps::centreon::local::plugin
224259--mode=not-so-dummy --status-sequence='ok,warning,ok,critical,critical,critical'
225260--output='Not so dummy service' --show-sequence --statefile-dir='/tmp'
226261
227- perl centreon_plugin .pl --plugin=apps::centreon::local::plugin
262+ perl centreon_plugins .pl --plugin=apps::centreon::local::plugin
228263--mode=not-so-dummy --status-sequence='up,down,down' --host
229264--output='Not so dummy host'
230265
231- perl centreon_plugin .pl --plugin=apps::centreon::local::plugin
266+ perl centreon_plugins .pl --plugin=apps::centreon::local::plugin
232267--mode=not-so-dummy --status-sequence='ok,ok,ok' --output='Not so dummy'
233268--metrics-count=5 --metrics-name='met.rics' --metrics-values-range='-15:42'
234269
@@ -275,6 +310,18 @@ Show the index as a metric (in addition to the defined metrics count).
275310
276311Restart the sequence from the beginning (ie. reset the sequence in cache file).
277312
313+ =item B<--min-duration >
314+
315+ Min duration thresholds (in seconds) use to set the range used to randomly simulate the execution of a plugin.
316+ If this option is set, max-duration is mandatory.
317+ The duration is chosen in the [min,max) range.
318+
319+ =item B<--max-duration >
320+
321+ Max duration thresholds (in seconds) use to set the range used to randomly simulate the execution of a plugin.
322+ If this option is set, min-duration is mandatory.
323+ The duration is chosen in the [min,max) range (max is excluded).
324+
278325=back
279326
280327=cut
0 commit comments