Forum Discussion

geffryti_32102's avatar
geffryti_32102
Icon for Nimbostratus rankNimbostratus
Mar 17, 2010

how 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

  • Oops. For got to explain the bracket thing. What specifically was going on was this:

     

     

    [HTTP::host][HTTP::uri] evaluates those two variables as they're coming in from the request. It'll look something like: 10.8.224.44/dir32/, for example.

     

     

    Now, that second set of brackets around it: [ [HTTP::host][HTTP::uri] ]

     

    ...was actually trying to evaluate the string returned from the inner host and uri variables after they were returned as strings, so it threw that error in your logs. The TCL interpreter tried to run:

     

    [ 10.8.224.44/dir32/ ] as a command, which is invalid.

     

     

    I hope this makes sense. By simply removing that outer set of brackets the findclass command was able to evaluate your lookup correctly.

     

     

    -Matt
  • I want findclass to only print when a match is found against the datagroup, so I added -q option. But this doesn't seem to stop findclass.

     

     

    So how do I stop it from proceeding with the rest of the script if there's no match found?

     

     

    set newURI "[findclass -q [HTTP::host][HTTP::uri] $::redlist]"

     

  • hoolio's avatar
    hoolio
    Icon for Cirrostratus rankCirrostratus
    Sorry for the typos. Thanks for the updates Matt.

     

     

    You can't check if findclass returns no result and save the result in a single step. If findclass finds no match in the class, then $newURI will have a null length value and you'll hit the else case and no more code will be run for this iRule.

     

     

    Aaron