Time based iRule example

Problem this snippet solves:

Here's a simple example of taking action in an iRule based on a time window. We use the clock scan and clock seconds commands to convert a timestamp in a user-friendly format to UTC (time since the Unix epoch of 0.0.1970) and check it against the current time.

Tcl clock wiki page: http://www.tcl.tk/man/tcl8.4/TclCmd/clock.htm


Hoolio plus help with others in the forums

Code :

when RULE_INIT {

   # Start of maintenance window in YYYY-mm-dd HH:MM format
   set static::start_date "2011-05-29 18:45"

   # End of maintenance window in YYYY-mm-dd HH:MM format
   set static::end_date "2011-05-29 18:50"

   # Convert start/end times to seconds from the epoch for easier date comparisons
   set static::start [clock scan $static::start_date]
   set static::end [clock scan $static::end_date]

   # Get the current time in seconds since the Unix epoch of 0-0-1970
   set now [clock seconds]
   # Check if the current time is between the start and end times
   if {$now > $static::start and $now < $static::end}{
      pool MAINT_POOL
   # Default action is to use the virtual server default pool
Published Mar 18, 2015
Version 1.0

Was this article helpful?