irule static::debug

Hi everyone, I am bit confused, when i interpret irule shown below. Why should i use static::debug. Could you please explain the function of this.

when RULE_INIT {

set static::debug 0

}

when HTTP_REQUEST {

set cli [IP::remote_addr]:[TCP::remote_port]

set SessionId [HTTP::cookie ASP.NET_SessionId]

if {$static::debug}{log local0. “Client: $cli Request SessionId: >$SessionId<”}

if { $SessionId != “” } { persist uie $SessionId 3000 }

}

}

It’s a static variable, similar to static (global) variables you’d use in other programming languages. So you’re setting it once and reading it throughout the code. In this case it’s being used to globally enable (1) or disable (0) logging in the iRule.

in this line what can be the value of static debug 0 or 1 can it be changed ?

You’re using a 1 for “true” and 0 for “false”, but you could use any logical true/false equivalent values here.

The iRule has it set to 0 (false), so to enable logging just change the 0 to 1 (true).

A few things to note:

  1. It is global [1], so while it is common to see

    static::debug
    

    , it’s not best-practice to do so. The reason is, if you use

    static::debug
    

    in more than one iRule, and set it to different values in each of the rules, the final value will apply to all iRules and will be essentially non-deterministic [2]. There are several ways to remedy this, but a common approach -- and the one I think is easiest -- is to simply choose a unique prefix for each iRule. So, if you have three rules that want to use

    static::debug
    

    , you might use

    static::abc_debug
    

    ,

    static::def_debug
    

    and

    static::ghi_debug
    

    (okay, use something more meaningful that

    abc_
    

    , etc.);

  2. These

    if { $static::abc_debug } { ... }
    

    commands will be evaluated on each connection. So, while the use of the “debug” value is useful when building or changing an iRule, I strongly recommend commenting these out (or removing them) for production. For complex rules, some people create an iApp which essentially inserts debugging into a rule (or uncomments existing statements) so that debugging can be “turned on” and “turned off”.

[1] To be completely pedantic, it is tied to a tmm, so if you were to change this or any

static::

scoped variable, the change would be visible only to other connections being processed on the tmm instance where the change was made. There is a valid and quite useful case for this, but 99% of the time -- and unless you really know what you’re doing and why -- treat a

static::

variable as a constant and don’t change it.

[2] It will be the value in the last iRule loaded which attempts to set this value, but that ordering could change over time.