Forum Discussion

Shahram_83722's avatar
Shahram_83722
Icon for Nimbostratus rankNimbostratus
Jun 16, 2012

iRule to redirect based on path plus fallback host

I'm not super on iRules but need to write one that will redirect path A and B to pool1 and path C and D to pool2. I also need to incorporate a fallback host in case each of these pools don't have a member. I've come up with the following iRule. Can anyone tell me whether or not this is a sound iRule or if there is anything better I should write?

 

 

 

 

when HTTP_REQUEST {

 

 

switch -glob -- [string tolower [HTTP::path]] {

 

 

 

"/a*" -

 

"/b*" {

 

if {[active_members pool_1] < 1} {

 

Log and direct the client to Maintenance pool

 

log local0. "!!Maintenance page called for Pool1!!"

 

pool Fallback_Pool }

 

else {

 

pool pool_1

 

}

 

 

 

"/c*" -

 

"/d*" {

 

if {[active_members pool_2] < 1} {

 

Log and direct the client to Maintenance pool

 

log local0. "!!Maintenance page called for Pool2!!"

 

pool Fallback_Pool }

 

else {

 

pool pool_2

 

}

 

 

default { discard }

 

 

 

}

 

}

 

 

 

 

  • rather than an if statement inside the cases, just use the LB_FAILED event to catch when no pool members are available and assign to the fallback pool. I think this would be a more efficient way to handle this since both cases use the same fallback pool.
  • The rule you have looks okay. The curly braces weren't exactly correct, but I assume that's just an issue with the editing you did for the post.

    Here's your original version with balanced braces:

    when HTTP_REQUEST {
    
    switch -glob -- [string tolower [HTTP::path]] {
    
    "/a*" -
    "/b*"  {
    if {[active_members pool_1] < 1} {
     Log and direct the client to Maintenance pool
    log local0. "!!Maintenance page called for Pool1!!"
    pool Fallback_Pool
    } else {
    pool pool_1
    }
    }
    "/c*" -
    "/d*"  {
    if {[active_members pool_2] < 1} {
     Log and direct the client to Maintenance pool
    log local0. "!!Maintenance page called for Pool2!!"
    pool Fallback_Pool }
    else {
    pool pool_2
    }
    }
    default {
    discard
    }
    }
    }
    

    You could move the active_members check of the pool outside the switch statement to simplify the rule a little:

    when HTTP_REQUEST {
    
    switch -glob -- [string tolower [HTTP::path]] {
    
    "/a*" -
    "/b*"  {
    pool pool_1
    }
    "/c*" -
    "/d*"  {
    pool pool_2
    }
    default {
    discard
    return
    }
    }
    if {[active_members [LB::server pool]] < 1} {
     Log and direct the client to Maintenance pool
    log local0. "!!Maintenance page called for [LB::server pool]!!"
    pool Fallback_Pool
    }
    }
    

    Aaron
  • Posted By Brian on 06/18/2012 01:01 PM

     

    ...use the LB_FAILED event to catch when no pool members are available and assign to the fallback pool.

     

    That would work too :)

     

     

    Aaron

     

  • Thank you guys for the input. I greatly appreciate it.

     

     

    I've tried using the LB_FAILED before and it did not work for me, but the check for active members did so I'm not quite sure I want to use that. It could have been an error on my part...

     

     

    Aaron, the reason I kept the members check within the switch statement is because I don't want the absence of a member on one of the pools to trigger a fallback for the whole VS, although my fallback server can serve all paths.

     

     

    I saw you use the 'return' command. I've read the online documentation on it but haven't quite gotten an understanding of it. Could you tell me what it exactly does?
  • return exits out of the current event of the current iRule. So basically, the active_members check on the currently selected pool won't be run if the request is to a URI that's set for being dropped.

     

     

    The [LB::server pool] command will return the name of the currently selected pool. So it should work fine to move that check out of the switch statement.

     

     

    Aaron