Forum Discussion
Chad_Roberts_21
Nimbostratus
May 16, 2006Time-based iRules
I hope this question is unique enough to be worthy of a new post. Unfortunately, searching for the word "time" with various other keywords didn't appear to be specific enough to find my anything of interest, so here goes...
Is it possible to use time as a factor in iRules? I'd like to direct traffic to one pool during normal business hours and to another pool after hours, but I haven't been able to find any command or variable that gives me this ability. If no one knows of a way to do it with iRules, does anyone know any other way to do it within the configuration of version 9?
Thanks for the look.
- JRahm
Admin
absolutely! You can use the TCL clock command to get the current time. - Chad_Roberts_21
Nimbostratus
Awesome! Thanks, I'll give it a try. I've gone through all kinds of stuff on iRules, but I have a lot to learn on TCL in general. - Chad_Roberts_21
Nimbostratus
Does anyone have an example of a complete iRule that chooses an action based on time? I understand better now how setting a variable to clock time is done, but I've been experimenting for the last couple of hours to determine how to write expressions using this number. For example, if I want PoolA to be used between 08:00 and 17:00 (normal business hours) and PoolB to be used during any other hours, how might the expression be written so that current time is compared to those times? - Chad_Roberts_21
Nimbostratus
Nevermind my last request. Given enough time and caffeine, anything is possible. In case anyone in the future would like to accomplish a similar task, here is the code I used:when HTTP_REQUEST { set current_day [clock format [clock seconds] -format {%a} ] set current_time [clock seconds] set time_min [clock scan {08:00}] set time_max [clock scan {17:00}] if {($current_day == {Sat}) or ($current_day == {Sun})} { pool after_hours_pool } elseif {($current_time < $time_min) or ($current_time > $time_max)} { pool after_hours_pool } }
- JRahm
Admin
I'd recommend moving this to the CLIENT_ACCEPTED event unless you have a specific need to move existing client sessions to the other pool during your on/off hour transitions. - Chad_Roberts_21
Nimbostratus
Hmm, good call. Using HTTP_REQUEST instead of CLIENT_ACCEPTED would probably have some impact on performance as well, would it not? - Chad_Roberts_21
Nimbostratus
Actually, that does present a new issue. I showed a simple version of my iRule yesterday because the rest of what I've actually used was not relevant to the conversation at hand, but the full code I used doesn't check out when I try to change it to CLIENT_ACCEPTED.when HTTP_REQUEST { set current_day [clock format [clock seconds] -format {%a} ] set current_time [clock seconds] set time_min [clock scan {08:00}] set time_max [clock scan {17:00}] set uri [HTTP::uri] if { ([HTTP::host] eq "www.domain.com") or ([HTTP::uri] contains "/folder") } { if {($current_day == {Sat}) or ($current_day == {Sun})} { HTTP::uri "/" pool test_clock_pool } elseif {($current_time < $time_min) or ($current_time > $time_max)} { HTTP::uri "/" pool test_clock_pool } } }
- JRahm
Admin
You could set your clock variables in the CLIENT_ACCEPTED event so they are only set once. If you are not using your uri variable, I wouldn't set it. If it is necessary, that variable will need to remain in the HTTP events.rule my_fast_rule { timing on when HTTP_REQUEST { Do some stuff } }
rule my_slow_rule { when HTTP_REQUEST timing on { Do some other stuff } }
Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects