cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
7816
Views
5
Helpful
2
Replies

EEM - random delay / wait

Hello,

how can i delay an actions in a applet? But it should no be a fixed time, the best solution would be a random value. Let's say +/- 5s...

If generating a real random a valus is not possible, some pseudo-random value might help. Something like extracting a numeric value from the router's serial#.

thanks for ideas / Karsten

1 Accepted Solution

Accepted Solutions

Jason Pfeifer
Cisco Employee
Cisco Employee

While not necessarily random you could use the msec parameter of show clock output to simulate a random number between 0 and 5 using something like this:

event manager applet two

event none

action 1   cli command "show clock"

action 2   regexp ".*\.([0-9]+).*" "$_cli_result" match msec

action 2.5 divide $msec 6

action 3   puts "random: $_remainder"

Unfortunately the wait action does not accept a variable name as input so if you wanted to run commands in a loop and have each one run at a random second you would have to create a wait loop prior to running the command.  Here is an example:

event manager applet three

event none maxrun 60

action 098 puts "loop running 5 times"

action 099 set count 0

action 100 while $count lt 5

action 101  cli command "show clock"

action 102  regexp ".*\.([0-9]+).*" "$_cli_result" match msec

action 102.6 multiply $count $count

action 102.7 multiply $msec $_result

action 103  divide $_result 6

action 104  set i "0"

action 105  while $i le $_remainder

action 106   wait 1

action 107   incr i

action 108  end

action 109  puts "waited $_remainder seconds"

action 110  incr count

action 111 end

action 112 puts "loop finished"

This will produce output such as:

#ev man run three

loop running 5 times

waited 0 seconds

waited 5 seconds

waited 2 seconds

waited 3 seconds

waited 2 seconds

loop finished

Thanks

View solution in original post

2 Replies 2

Jason Pfeifer
Cisco Employee
Cisco Employee

While not necessarily random you could use the msec parameter of show clock output to simulate a random number between 0 and 5 using something like this:

event manager applet two

event none

action 1   cli command "show clock"

action 2   regexp ".*\.([0-9]+).*" "$_cli_result" match msec

action 2.5 divide $msec 6

action 3   puts "random: $_remainder"

Unfortunately the wait action does not accept a variable name as input so if you wanted to run commands in a loop and have each one run at a random second you would have to create a wait loop prior to running the command.  Here is an example:

event manager applet three

event none maxrun 60

action 098 puts "loop running 5 times"

action 099 set count 0

action 100 while $count lt 5

action 101  cli command "show clock"

action 102  regexp ".*\.([0-9]+).*" "$_cli_result" match msec

action 102.6 multiply $count $count

action 102.7 multiply $msec $_result

action 103  divide $_result 6

action 104  set i "0"

action 105  while $i le $_remainder

action 106   wait 1

action 107   incr i

action 108  end

action 109  puts "waited $_remainder seconds"

action 110  incr count

action 111 end

action 112 puts "loop finished"

This will produce output such as:

#ev man run three

loop running 5 times

waited 0 seconds

waited 5 seconds

waited 2 seconds

waited 3 seconds

waited 2 seconds

loop finished

Thanks

Hello Jason,

thanks a lot for the great explanation!

Your script shows me the right way. But i have to modify something. Let me explain, i've got >100 routers with the same ntp-syncronized clock. And i want to avoid the action to be happen at same time, that's the reason why i am looking for randomizing it. So the clock might be not optimal.

Any idea, what could be a different value from router to router? Serial# was my first idea. Something else?

thanks / Karsten