Forum Discussion
Paul_Slosberg_8
May 17, 2007Historic F5 Account
A better way
Greetings,
Im trying to grab the last octect of the client adress so I can then use that to send the thraffic to the proper pool. I will have 254 pools that I need to send traffic too. Im trying to use variables to stream line this i-Rule any help or sugestions would be great. I will be using a nework virtual server and 254 port spcific pools. ie (4001-4254)
when CLIENT_ACCEPTED {
set port_info [getfield [IP::client_addr] "." 4]
set poolname "mypool_"
if
{ $port_info matches_regex "[ 1-9]"}
{ pool $poolname400$portinfo }
elseif
{ $port_info matches_regex "[ 10-99]"}
{pool $poolname40$port_info}
else
{ $port_info matches_regex "[ 100-254]"}{
pool $poolname4$portinfo}
}
}13 Replies
- JRahm
Admin
regex is expensive. Try this:when CLIENT_ACCEPTED { set port_info [getfield [IP::client_addr] "." 4] if { [string length $port_info] equals "1" } { pool mypool_400$port_info } elseif { [string length $port_info] equals "2" } { pool mypool_40$port_info } elseif { [string length $port_info] equals "3" } { pool mypool_4$port_info } else { pool myDefault } }
Untested, but passes syntax checker - Paul_Slosberg_8Historic F5 AccountIll stat testing thank you! I have been searching for a way to search string length. Ill let you know how it goes.
- hoolio
Cirrostratus
You could use a switch statement on the string length of $port_info to avoid using a regex. The string length command is more efficient than the regex comparison.... switch [string length $port_info] { 1 { pool $poolname400$portinfo } 2 { pool $poolname40$portinfo } 3 { pool $poolname4$portinfo } }
If you do stick with the regex, I think you'll need to adjust the comparisons. [10-99] would mean match a single character that's either 1, 0 through 9, or 9. Likewise [100-254] would match a single character that's either 1, 0, 0 through 2, 5 or 4.
Else, if you wanted to avoid using a rule altogether, you might be able to use the least connections load balancing method to ensure the load is distributed evenly, with source address persistence to ensure clients are sent to the same node. You'd need to add all of the nodes from the current 254 pools into one pool though.
Aaron - hoolio
Cirrostratus
Didn't see that you posted, Citizen...
On a side note, DC seems to translate a [ and 0 into a question mark [0
Aaron - JRahm
Admin
Yeah, I've noticed that certain patters produce undesired displays in DC.
I like the switch, very clean. - You've had your share of showing off, I thought I'd get my dig in while I had a chance...
BTW, keep in mind that this will likely break if on an IPv6 network...
-Joe - Paul_Slosberg_8Historic F5 AccountCrazy stuff, now I will test these thanks!!!
- JRahm
Admin
perhaps this is a dumb question, but are all the pools already defined? Either way, I'd introduce some logging and you can check your /var/log/ltm file to see if you are getting any runtime TCL errors.
when LB_SELECTED {
log local0. "Pool selected is [LB::server pool]"
} - Paul_Slosberg_8Historic F5 AccountYes the pools would be predefined. So far this has been working great. It would be great if I could use a one wild card pool as well but unfortunately the I rule editor will take this command but it does not work in practice.
when CLIENT_ACCEPTED { TCP::local_port 4[format "%.3d" [getfield [IP::local_addr] "." 4]]}
This way I could have just 2 objects a network virtual and a wild card pool. I would just define the default pool as the wild card pool.
But thank you all for the help the original rule works great! - What do you mean when you say that the editor will take this command? The TCP::local_port command is read-only and in your case you are trying to set it to a value. When I load the above iRule into the editor and try to save it, it yields this error:
"command is not valid in the current scope"
I'm not quite sure what you are getting at with regards to a wild-card pool. A pool is just a group of servers with each server having a pre-defined port. Do you mean that you would like to have a single pool with all the backend servers, but be able to select a certain subset depending on the 4xxx value you are calculating? If you could clarify that a bit, then there might be some more iRule stuff we can do to make it happen.
-Joe
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
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