Forum Discussion
local variable within same irule on all events
Hello,
I'd like to use a local variable to log all event within a certain irule.
Since now I have done like this:
when CLIENT_ACCEPTED { set logdebug 1 if {$logdebug != 0} {log local0.info "Client Connected, IP: [IP::client_addr], pool [LB::server pool]"} }
when HTTP_REQUEST { set logdebug 1 if {$logdebug != 0} {log local0.info "Client IP IP: [IP::client_addr], host [HTTP::host]"} }
when LB_SELECTED { set logdebug 1 if {$logdebug != 0} {log local0.info "in LB_SELECTED, IP [IP::client_addr]:[TCP::client_port], node [LB::server addr], pool [LB::server pool]"} }
But I'd like to avoid global variable and have to set logdebug to 1 only once on the irule ? Is that possible ? maybe with "session add " on the CLIENT_ACCPTED event ?
12 Replies
- IheartF5_45022
Nacreous
Irule variables are tcp connection specific in scope , so you can set it once in CLIENT_ACCEPTED and will work for all events on that top connection.
- Mike_61663
Cirrus
How about using a static variable defined when the iRule initiates?
when RULE_INIT { set static::logdebug 1 } - helenio
Nimbostratus
Thank's, therefore the right way to log in may case should be:
when CLIENT_ACCEPTED { set debug 1 if {$debug != 0} {log local0.info "Client Connected, IP: [IP::client_addr], pool [LB::server pool]"} }
when HTTP_REQUEST { if {$debug != 0} {log local0.info "Client IP IP: [IP::client_addr], host [HTTP::host]"} }
when LB_SELECTED { if {$debug != 0} {log local0.info "in LB_SELECTED, IP [IP::client_addr]:[TCP::client_port], node [LB::server addr], pool [LB::server pool]"} }
when LB_FAILED { if {$debug != 0} {log local0.info "in LB_FAILED, IP [IP::client_addr]:[TCP::client_port], node [LB::server addr], pool [LB::server pool]"} }
- Syafiq_89483
Cirrus
Can you try this?
when RULE_INIT { set static::debug 1 } when CLIENT_ACCEPTED { set logdebug $static::debug if {$logdebug} { log local0.info "Client Connected, IP: [IP::client_addr], pool [LB::server pool]" } } when HTTP_REQUEST { if {$logdebug} { log local0.info "Client IP IP: [IP::client_addr], host [HTTP::host]" } } when LB_SELECTED { if {$logdebug} { log local0.info "in LB_SELECTED, IP [IP::client_addr]:[TCP::client_port], node [LB::server addr], pool [LB::server pool]" } } - helenio
Nimbostratus
From my knowledge static variable are global therefore if you use it on other irule you will have issues ..
- IheartF5_45022
Nacreous
Personally I generally don't tend to use static variables - they are too subject to unexpected change from other irules 'accidentally' initialising the same variable. Think variable 'static::debug' - what crimes have been committed against that?
I tend to use CLIENT_ACCEPTED to initialise my variables - the overhead is negligible.
- IheartF5_45022
Nacreous
With caveat - except where variables are actually global in scope. Even then woul try to use session table if poss.
- helenio
Nimbostratus
RULE_INT sets global variable that's ouf of my scope.
- Syafiq_89483
Cirrus
But I'd like to avoid global variable and have to set logdebug to 1 only once on the irule
sorry, I missed the requirement.
- Mike_61663
Cirrus
Yes sorry I missed that too. Just do as iheartf5 mentions above. Set the local variable in the CLIENT_ACCEPTED event and it will then exist for all the other events for that particular connection.
- helenio
Nimbostratus
No problem. It was out of the requirements because I think that if you set the variable, for log purpose, as a global one you have effect on all other irule where this variable is used and for me doesn't make sense. Isn't ?
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com