Forum Discussion

MACTEP's avatar
MACTEP
Icon for Altocumulus rankAltocumulus
Feb 25, 2013

Check if static variable exists before referencing to it

Hi All,

 

 

I have created an iRule performing DNS lookup and storing the result in static variable, so it could be referred from another iRule.

 

....

 

set static::my_variable "some value"

 

.....

 

 

As a precaution, in the second iRule I put verification of variable value before actually using it

 

if {"${static::my_variable}X" eq "X"}{

 

Input was empty, take default action

 

log local0.err "Lookup failed"

 

} else {

 

Variable is not empty, I can use it

 

log local0.err "Name resolved to $static::my_variable"

 

}

 

 

It worked fine during tests as I build iRules consequently.

 

It worked as expected even when I simulated DNS server failure and name actually could not be resolved - health check worked fine.

 

 

BUT.

 

When F5 was rebooted, log was flooded with error messages:

 

"tmm3 err tmm3[11714]: 01220001:3: TCL error: /Common/iRule_2 - can't read "static::my_variable": no such variable while executing "if {"${static::my_variable}X" eq "X"}{ Input was empty, take default action..."

 

 

 

errr... OK, I got it. My rule was checking if variable has empty value, but when F5 was rebooted second iRule was hit before the first one, so variable was not defined at all. So I should check not only variable value, but variable existence as well.

 

 

Question: how should I check if variable exists when variable is defined in another iRule?

 

 

 

 

 

6 Replies

  • Question: how should I check if variable exists when variable is defined in another iRule?is "info exists" usable?

     

     

    info exists

     

    http://wiki.tcl.tk/1185

     

     

    by the way, which event do you perform dns lookup and set it to static global variable?
  • Thanks for the tip, I'll give it a try.

    I use it that way:

     set static::myhost_ip [RESOLV::lookup @ -a ]
  • Confirmed, works as expected whenever variable is defined, not defined, has empty or non-empty value.

    if {[info exists "static::my_variable"]}{
    log local0.err "Variable my_variable is defined"
       if {"${static::my_variable}X" eq "X"}{
          log local0.err "variable value is empty"
       } else {
          log local0.err "variable value is ${static::my_variable}" 
               }
    } else {
    log local0.err "Variable static::my_variable is NOT defined"
    }
     
  • you know changing static global variable (outside RULE_INIT) won't be synchronized to other tmm (it will change static global variable value on local tmm only), don't you?

     

     

    This namespace is intended only for use with values that will not change. While it is possible to alter the value of a global variable in the static namespace, this value will not be propagated to other CMP nodes in the system. Therefore, doing so may lead to unexpected results. If you need to share information across CMP nodes, you should use the session or table commands instead.static wiki

     

    https://devcentral.f5.com/wiki/irules.static.ashx
  • Well, I missed that and I do change static variable outside of RULE_INIT, but iRules work fine there.

     

    LTM v11.1.0 HF5

     

    Maybe I should change it all back to global variables and skip that CMP compatibility in case future firmware updates will break current behavior.
  • we can change static global variable value as long as we understand how it works. :-)