Forum Discussion

DM_5174's avatar
DM_5174
Icon for Nimbostratus rankNimbostratus
Jul 09, 2007

<b>Better way to code iRule in 4.5.x code ???</b>

Hi all,

 

 

I have version 4.5x code and would like to know if there is any efficient way to

 

consolidate the code below. I tired to use tolower or class but got an error message. I believe the reason is due to the current 4.5 code does not support

 

the commands.

 

 

Thanks!

 

 

 

 

 

if (http_uri contains "site1") {

 

use pool APACHE_POOL

 

}

 

 

else if (http_uri contains "site2") {

 

use pool APACHE_POOL

 

}

 

 

else if(http_uri contains "site3") {

 

use pool APACHE_POOL

 

}

 

 

else if (http_uri contains "site4") {

 

use pool APACHE_POOL

 

}

 

 

else if (http_uri contains "site5") {

 

use pool APACHE_POOL

 

}

 

 

else if (http_uri contains "site6") {

 

use pool APACHE_POOL

 

}

 

 

else if (http_uri contains "site7") {

 

use pool APACHE_POOL

 

}

 

 

 

else {

 

use pool IISWEB-POOL

 

}

 

 

  • Deb_Allen_18's avatar
    Deb_Allen_18
    Historic F5 Account
    I think "matches_regex" function would work well here:
    
    if ( tolower(http_uri) matches_regex "(.*)(site1|site2|site3|site4|site5|site6|site7)(.*))" ) {
      use pool APACHE_POOL
    }
    else {
      use pool IISWEB_POOL
    }

    (if the site names were really that similar, an even simpler expression would work)

    HTH

    /deb
  • Deb_Allen_18's avatar
    Deb_Allen_18
    Historic F5 Account
    I just saw your earlier post, and since it looks like you need to switch just of the beginning of the path, you can simplify just a bit, eliminating the leading wildcard to save the regex engine some work:
    if ( tolower(http_uri) matches_regex "(/)(site1|site2|site3|site4|site5|site6|site7)(.*))" ) {
      use pool APACHE_POOL
    }
    else {
      use pool IISWEB_POOL
    }

    /d
  • Deb_Allen_18's avatar
    Deb_Allen_18
    Historic F5 Account
    (edited both previous posts to correct syntax. Hopefully I got it right this time -- it's been quite a while since I wrote a v4 iRule...)
  • Hey deb,

     

     

    this is the error i get from the LB.

     

     

    Error 331835 -- Rule string to tree failed. - syntax error at 'tolower'

     

    line 1: if ( tolower(http_uri) matches_regex "(/)(site1|site2|site3|site4|site5|site6|site7)(.*))" ) {

     

     

     

     

    It looks like it does not like the "tolower" expression.

     

     

  • Martin_Machacek's avatar
    Martin_Machacek
    Historic F5 Account
    Regular expressions are the least efficient way of matching in

    iRules (especially in v4.x). The regex version of the rule is very likely going to be measurably slower than the if-then chain. The most efficient way to match an item of a set in an iRules is to use classes. Like this:

    
    class apache_sites {
       "site1"
       "site2"
       "site3"
       "site4"
       "site5"
       "site6"
       "site7"
    }
    rule match_apache_sites {
       if( http_uri contains one of apache_sites) {
           use pool APACHE_POOL
       } else {
           use pool IISWEB-POOL
       }
    }

    NOTE: the above rule matches also URIs containing the items of the class as substring (e.g. "site666" or "parasite1"). If those matches are not desired, you need to either add delimiting characters to the items in the set (e.g. "/site1/"), use other match operator (e.g. "starts_with", "equals" or "ends_with) or use the substr function to extract the portion of the URI to be matched.

    FYI, the tolower function is available only in 4.6.x.
  • Unfortunately we are running 4.5.13 so the class function does not work. Please let me know if there are any other way of doing this since upgrading our environment right now is not an option. Eventually we are migrating to the LTMs running v.9x, but right now its to get

     

    things rolling until them. Any help is appriciated!

     

     

    Error 331835 -- Rule string to tree failed. - syntax error at 'class'

     

    line 1: class apache_sites {
  • Martin_Machacek's avatar
    Martin_Machacek
    Historic F5 Account
    I can guarantee you that the rule works on 4.5.13. Classes are part of iRules since version 4.2.

     

     

    How did you enter the class? Via the GUI? Have you used the Classes tab of the Rules page of the GUI? The syntax I've shown is as it appears in the actual configuration file. Please, refer to the section titled "Configuring class lists" of the "BIG-IP Reference Guide" available on-line here:

     

     

    https://tech.f5.com/home/bigip/manuals/bigip4_5_10/bigip4_5_10ref/BIGip_rules.html1204392