iRule (round robin) modification - ignore if the node in pool is down!

I have an irule to divert traffic to two servers, and it is working as round robin, and want to get some help to modify this rule so if one node is down the traffic flows to another… I am hoping that someone can provide guidance on how to achieve it.

when RULE_INIT { set ::whichone 0 } when HTTP_REQUEST { switch $::whichone { 0 { HTTP::redirect “” } 1 { HTTP::redirect “” } } set ::whichone [expr ! $::whichone] }

You’ll need to create a pool, pl_redirect_pool with an HTTP monitor and Server1 and Server2 as members. Then try this rule (note you need to hardcode the IPs of Servers 1 and Server 2 in here also);-

when RULE_INIT { 

   set ::whichone 0 
   set ::servers [list "x.x.x.x" "y.y.y.y"]
    Unfortunately have to hardcode the server IPs here
   set ::redirs  [list "http://Server1/xyz" "http://Server2/xyz"]

} 
when HTTP_REQUEST { 

     if  { [LB::status pool pl_redirect_pool member [lindex $servers $::whichone] 80] eq "up" } {}
        HTTP::redirect [lindex $::redirs $::whichone]
     else {
        HTTP::redirect [lindex $::redirs [expr ! $::whichone]] 
     }

      set ::whichone [expr ! $::whichone] }
}

Thank you “IheartF5” :slight_smile: I will give it a try and will get back to you by Monday. Really appreciate your help.

Hi IheartF5 --> I am getting an error while saving the rule, seems like it doesn’t want to take the arguments: 01070151:3: Rule [/Common/App_redirection2] error: /Common/App_redirection2:12: error: [wrong args][HTTP::redirect [lindex $::redirs $::whichone] else { HTTP::redirect [lindex $::redirs [expr ! $::whichone]] }] /Common/App_redirection2:18: error: [command is not valid in the current scope][}]

I got the brackets wrong - see {} at end of the if statement? Let me knwo if that fixes it. In meantime I’ll actually try saving it myself :slight_smile:

Thank you… I removed it but no luck, I verified all brackets opening and closing in notepad++ they do line up: when HTTP_REQUEST { if { [LB::status pool App_pool member [lindex $servers $::whichone] 80] eq “up” } HTTP::redirect [lindex $::redirs $::whichone] else { HTTP::redirect [lindex $::redirs [expr ! $::whichone]] } set ::whichone [expr ! $::whichone] } Still have the same error! - Hope you can stop the fire on your end but do reply when you get a chance. Thank you for your help!

Error: error: [missing a script after “if”][] /Common/TrackwiseDev_redirection2:12: error: [wrong args][HTTP::redirect [lindex $::redirs $::whichone] else { HTTP::redirect [lindex $::redirs [expr ! $::whichone]] }]

Try this;-

when RULE_INIT { 

   set ::whichone 0 
   set ::servers [list "x.x.x.x" "y.y.y.y"]
    Unfortunately have to hardcode the server IPs here
   set ::redirs  [list "http://Server1/xyz" "http://Server2/xyz"]
    set pool pl_redirect_pool 
} 
when HTTP_REQUEST { 

     if  { [LB::status pool $pool member [lindex $servers $::whichone] 80] eq "up" } {
        HTTP::redirect [lindex $::redirs $::whichone]
    }     else {
        HTTP::redirect [lindex $::redirs [expr {! $::whichone}]] 
     }

      set ::whichone [expr {!$::whichone}] 
}

got it to “save” the irule by adding {} after if condition… { HTTP::redirect [lindex $::redirs $::whichone]}

I’ll test it tomorrow and update you :slight_smile:

Hi folks,

I’m doing something similar (with less iRule skills then you :D), I need to Load Balance with 4 links:

10.x.x.121:10080/apex/apex.jnlp 10.x.x.122:10080/apex/apex.jnlp 10.x.x.121:9080/apex/apex.jnlp 10.x.x.122:9080/apex/apex.jnlp

when RULE_INIT { 

   set ::whichone 0 
   set ::servers [list "10.203.10.121" "10.203.10.122"]
    Unfortunately have to hardcode the server IPs here
   set ::redirs  [list "10.203.10.121:10080/apex/apex.jnlp" "10.203.10.122:10080/apex/apex.jnlp" "10.203.10.122:9080/apex/apex.jnlp" "10.203.10.121:9080/apex/apex.jnlp"]
    set pool pl_redirect_pool 
} 
when HTTP_REQUEST { 

     if  { [LB::status pool $pool member [lindex $servers $::whichone] 80] eq "up" } {
        HTTP::redirect [lindex $::redirs $::whichone]
    }     else {
        HTTP::redirect [lindex $::redirs [expr {! $::whichone}]] 
     }

      set ::whichone [expr {!$::whichone}] 
}

Just to make it clear, VS is on port 80 (users will hit port 80 ), Port and Address translations are thicked, snat automap, default http profile, no pools associated, irule assigned. Point is when users are typing application-name.company.local they should get one of the links from above. Then Java Client to Server communication will follow. If you can assist me to make it work, it’s very similar to one you’re discussing about.

I HearF5 - I had no success with the basic rule if I go to the VP DNS name at least it redirects to one or the other; when I add the script for monitoring pool it doesn’t even go to the VP DNS - page cant be displayed!!! I am stumped… need help what works: when RULE_INIT { set ::whichone 0 } when HTTP_REQUEST { switch $::whichone { 0 { HTTP::redirect “http://Server1/xyz” } 1 { HTTP::redirect “http://Server2/xyz” } } set ::whichone [expr ! $::whichone] } the rule to see if node in pool is doen doesn’t work… at all. Help please…