Forum Discussion
geffryti_32102
Nimbostratus
Mar 17, 2010how to use data group
Requirements:
1. system for redirecting certain host+uri to a different server (no pools)
2. use data group file to maintain uri-to-server list, so I can keep using the same irule for other VS.
Questions:
1. how do I call a data group from an irule? (assuming I already created the class in GUI and the name is redlist) will $::redlist work? My redlist is a string type with three fields, and below is an example. (some only has two fields).
2. I want to be able to use the second field as the new HOST and the third field as the new URI, but I'm having problem with inserting them. Pls see my draft below and let me know what's wrong and if there's a better way of doing this. Thanks.
REDLIST
"10.6.254.10/dir3/ 172.16.254.115 "
"10.41.254.121/dir6/ 172.16.254.115 /dir60000/"
"10.8.224.44/dir32/ 172.16.254.115 /dir60000/"
DRAFT SCRIPT
when HTTP_REQUEST {
find a match using host+uri against the class and
returns the whole string (field1 field2 field3)then
set it as newURI variable.
set newURI "[findclass [[HTTP::host][HTTP::uri]] $::redlist]"
if { $newURI ne "" } {
pick 2nd field and set as variable (newhost)
set newhost "[getfield $newURI " " 2]"
pick 3rd field and set as variable (newuri)
set newuri "[getfield $newURI " " 3]"
change host and uri
HTTP::host $newhost
HTTP::uri $newuri
Clear the newURI variable
unset newURI
}
}
14 Replies
- hoolio
Cirrostratus
Do you really have clients making requests using several different IP addresses to the same host VIP? Or do you have this iRule applied to multiple host VIPs? Or are you using a 0.0.0.0:80 VIP?
If you add logs to the iRule, can you see what's being hit when you're testing? What does a client see when a failure occurs?
Also, which LTM version are you running? For 10.x, you can use the class command to get a bit more functionality for doing lookups against a datagroup. In 10.1.x, you have even more functionality with address type datagroups that you use.
Aaronwhen HTTP_REQUEST { log local0. "[IP::client_addr]:[TCP::client_port]: [HTTP::method] request to [HTTP::host][HTTP::uri]" find a match using host+uri against the class and returns the whole string (field1 field2 field3)then set it as newURI variable. set newURI "[findclass [[HTTP::host][HTTP::uri]] $::redlist]" if { $newURI ne "" } { log local0. "[IP::client_addr]:[TCP::client_port]: Matched line $newURI" Parse the three fields in the matched datagroup line scan $newuri {%s %s %s} unused host uri log local0. "Parsed: $unused $host $uri" change host and uri if {$host ne ""}{ HTTP::host $host } if {$uri ne ""}{ HTTP::uri $uri } } } - geffryti_32102
Nimbostratus
Hey, thanks for taking time Aaron.
I'll be using this on different VIPs representing different physical servers, I just forgot to tweak my example above to show different addresses.
I haven't tried this yet and I'm planning to dip it first on a dummy virtual server. Thanks for the tip about the log.
We're running v9. So I don't know if you answered it already, but does this mean that using $::redlist will call the "redlist " data group I created on the LTM>iRules>Data Group?
Cheers!
iRule newbie - geffryti_32102
Nimbostratus
Almost forgot, thanks for the script! very intuitive... - geffryti_32102
Nimbostratus
I get the error below when saving the irule...line 20: [wrong args] [HTTP::host $host ]
- L4L7_53191
Nimbostratus
I'm not sure you can set the HTTP::host variable quite like that (the wiki implies it's read-only). I'm just looking at it here with no time to test but try changing this single line in the "host" test block below:if {$host ne ""}{ HTTP::host $host <<-- Change this line to: }if {$host ne ""}{ HTTP::header replace Host $host <<--this one }
And give that a try. This should set your new host header.
-Matt - geffryti_32102
Nimbostratus
sweet!!! it accepted it. Now I'm going to run it, I'll let you know how it goes!
nice work Matt and Aaron! - L4L7_53191
Nimbostratus
I see the issue, and I see one other minor one with the current rule. Posting a new one for you in a minute.
-Matt - L4L7_53191
Nimbostratus
Ok give this a try please:when HTTP_REQUEST { log local0. "[IP::client_addr]:[TCP::client_port]: [HTTP::method] request to [HTTP::host][HTTP::uri]" find a match using host+uri against the class and returns the whole string (field1 field2 field3)then set it as newURI variable. set newURI "[findclass [HTTP::host][HTTP::uri] $::redlist]" if { $newURI ne "" } { log local0. "[IP::client_addr]:[TCP::client_port]: Matched line $newURI" Parse the three fields in the matched datagroup line scan $newURI {%s %s %s} unused host uri log local0. "Parsed: $unused $host $uri" change host and uri if {$host ne ""}{ log local0. "Setting new host to $host" HTTP::header replace Host $host } if {$uri ne ""}{ log local0. "Setting new uri to $uri" HTTP::uri $uri } } }
-Matt - geffryti_32102
Nimbostratus
great! it's working... can you share what happened there and what you did? the log entry fixed it?
I also want to add, just in case someone decides to use this, I mentioned that there are some lines in my datagroup where I only have two fields (redirect to default web directory), well I changed it since I was having problem, so instead of just skipping the 3rd field, I added a "/" as it's 3rd field data.
Thanks Matt! - L4L7_53191
Nimbostratus
Great news. There were two very minor issues:
1) The original error ('invalid command name...') was due to the brackets in bold - they needed to be removed: [findclass [[HTTP::host][HTTP::uri]] $::redlist]
2) There was a reference to a variable named newuri, but it should have been newURI (we're case sensitive, obviously).
That was it. Glad you're working!
-Matt
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)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
