Forum Discussion

annielee_13548's avatar
annielee_13548
Icon for Nimbostratus rankNimbostratus
May 13, 2015

irules optimization

Hi,

I am trying to optimize the irules below (to a for each loop, if possible) im modifying and need some programming help 🙂

if { ([HTTP::host] contains "env-1") || ([HTTP::host] contains "1.1.1.1") } {
    log local0. "ENV-1, HEADER=[HTTP::header Host], URI=[HTTP::uri]"
    set AA_pool env-1_AA_11
    set BB_pool env-1_BB_88

elseif { ([HTTP::host] contains "env-2") || ([HTTP::host] contains "1.1.1.2") } {
    log local0. "ENV-2, HEADER=[HTTP::header Host], URI=[HTTP::uri]"
    set AA_pool env-2_AA_22
    set BB_pool env-2_BB_88

elseif { ([HTTP::host] contains "env-3") || ([HTTP::host] contains "1.1.1.3") } {
    log local0. "ENV-3, HEADER=[HTTP::header Host], URI=[HTTP::uri]"
    set AA_pool env-3_AA_33
    set BB_pool env-3_BB_88

elseif......til env-10

Is that possible ?? Thanks in advance.

  • can you try something like this?

    when HTTP_REQUEST {
      switch -glob [HTTP::host] {
        "*env-1" -
        "1.1.1.1*" {
           Do something
        }
        "*env-2*" -
        "1.1.1.2*" {
           Do something
        }
        default {
           Do something
        }
      }
    }
    
  • can you try something like this?

    when HTTP_REQUEST {
      switch -glob [HTTP::host] {
        "*env-1" -
        "1.1.1.1*" {
           Do something
        }
        "*env-2*" -
        "1.1.1.2*" {
           Do something
        }
        default {
           Do something
        }
      }
    }
    
    • annielee_13548's avatar
      annielee_13548
      Icon for Nimbostratus rankNimbostratus
      just tried.. when the HTTP::host = 1.1.1.1, it works but not env-1, we need if the HTTP:host is env-1 or 1.1.1.1, then do the same thing.. (which is setting the pool variable accordingly)
  • can you try something like this?

    when HTTP_REQUEST {
      switch -glob [HTTP::host] {
        "*env-1" -
        "1.1.1.1*" {
           Do something
        }
        "*env-2*" -
        "1.1.1.2*" {
           Do something
        }
        default {
           Do something
        }
      }
    }
    
    • annielee_13548's avatar
      annielee_13548
      Icon for Nimbostratus rankNimbostratus
      just tried.. when the HTTP::host = 1.1.1.1, it works but not env-1, we need if the HTTP:host is env-1 or 1.1.1.1, then do the same thing.. (which is setting the pool variable accordingly)
  • when the HTTP::host = 1.1.1.1, it works but not env-1, we need if the HTTP:host is env-1 or 1.1.1.1, then do the same thing

    sorry i missed one asterisk after env-1. can you try one more time?

    when HTTP_REQUEST {
      switch -glob [HTTP::host] {
        "*env-1*" -
        "1.1.1.1*" {
           Do something
        }
        "*env-2*" -
        "1.1.1.2*" {
           Do something
        }
        default {
           Do something
        }
      }
    }
    
    • annielee_13548's avatar
      annielee_13548
      Icon for Nimbostratus rankNimbostratus
      now it works ! (thanks) but is that the better irules than for each ? (if there is any) ?
  • when the HTTP::host = 1.1.1.1, it works but not env-1, we need if the HTTP:host is env-1 or 1.1.1.1, then do the same thing

    sorry i missed one asterisk after env-1. can you try one more time?

    when HTTP_REQUEST {
      switch -glob [HTTP::host] {
        "*env-1*" -
        "1.1.1.1*" {
           Do something
        }
        "*env-2*" -
        "1.1.1.2*" {
           Do something
        }
        default {
           Do something
        }
      }
    }
    
    • annielee_13548's avatar
      annielee_13548
      Icon for Nimbostratus rankNimbostratus
      now it works ! (thanks) but is that the better irules than for each ? (if there is any) ?
    • annielee_13548's avatar
      annielee_13548
      Icon for Nimbostratus rankNimbostratus
      thanks for the link, will try to 'evaluate' the irules.. and also thanks for making my irules better :-)
    • annielee_13548's avatar
      annielee_13548
      Icon for Nimbostratus rankNimbostratus
      thanks for the link, will try to 'evaluate' the irules.. and also thanks for making my irules better :-)
  • can i ask can the below exist ? multiple switch -glob

    when HTTP_REQUEST {
      switch -glob [HTTP::host] {
        "*env-1" -
        "1.1.1.1*" {
           Do something
        }
        "*env-2*" -
        "1.1.1.2*" {
           Do something
        }
        default {
           Do something
        }
       switch -glob [HTTP::uri] {
       "aaa"
       do AAA
       }
    }
    
  • can i ask can the below exist ? multiple switch -glob

    if you mean something like this, yes.

    when HTTP_REQUEST {
      switch -glob [HTTP::host] {
        "*env-1*" -
        "1.1.1.1*" {
           Do something
        }
        "*env-2*" -
        "1.1.1.2*" {
           Do something
        }
        default {
          switch -glob [string tolower [HTTP::uri]] {
            "*aaa*" {
               Do something
            }
            default {
               Do something
            }
          }
        }
      }
    }
    
    • annielee_13548's avatar
      annielee_13548
      Icon for Nimbostratus rankNimbostratus
      not really.. coz in the existing irules now, there are a lot of IFs and some ifs are comparing the URI (with equals, ends with, contains), some comparing variables, some comparing headers. I would like to modify the rules to be 'better', hence you recommended the switch -glob
  • can i ask can the below exist ? multiple switch -glob

    if you mean something like this, yes.

    when HTTP_REQUEST {
      switch -glob [HTTP::host] {
        "*env-1*" -
        "1.1.1.1*" {
           Do something
        }
        "*env-2*" -
        "1.1.1.2*" {
           Do something
        }
        default {
          switch -glob [string tolower [HTTP::uri]] {
            "*aaa*" {
               Do something
            }
            default {
               Do something
            }
          }
        }
      }
    }
    
    • annielee_13548's avatar
      annielee_13548
      Icon for Nimbostratus rankNimbostratus
      not really.. coz in the existing irules now, there are a lot of IFs and some ifs are comparing the URI (with equals, ends with, contains), some comparing variables, some comparing headers. I would like to modify the rules to be 'better', hence you recommended the switch -glob
  • coz in the existing irules now, there are a lot of IFs and some ifs are comparing the URI (with equals, ends with, contains), some comparing variables, some comparing headers. I would like to modify the rules to be 'better', hence you recommended the switch -glob

     

    i do not think switch is always the best. you do not need to change all condition check to switch. it should be pretty fine as long as irule is easy to understand and maintain.

     

    Comparing iRule Control Statements by Joe Pruitt

     

    https://devcentral.f5.com/articles/comparing-irule-control-statements

     

    just my 2 cents.