Forum Discussion
Remove characters from logging Irule entry
Hello,
I would like to remove the routing domain suffix "%1" in the log file i'm creating.
Here's a sample of my Irule.
when HTTP_REQUEST { set client_ip [IP::remote_addr] set log_msg "" append log_msg "$client_ip"
The result in the log file is "192.168.1.5%1" but i need to have it without the "%1". I'm wondering if a regex could do the job inside my Irule but i'm stucked in finding a way. Any help would be appreciated Aurel
19 Replies
- nitass
Employee
can we use "string map"?
e.g.
% set clientip "192.168.1.5%1" 192.168.1.5%1 % put [string map {%1 ""} $clientip] 192.168.1.5 - Kevin_Stewart
Employee
There are actually many ways to do this, some static (like string map), and some dynamic (like regsub),
% time { regsub {%\d+} $ip "" } 1000 1.996 microseconds per iteration % time { string map {%1 ""} $ip } 1000 0.661 microseconds per iterationBut then you have to be careful that the function you use doesn't negate the performance benefits of not modifying it at all.
- Aurel
Cirrus
Hello guys, Thanks for your reply. Kevin, i actually don't understand the how to use it from your examples.(sorry about it) I did understand yours nitass, and i forgot to mention something else. I actually need to match string that could be "%[1-9][1-9][1-9]" (in a regex syntax). But this is not working with tcl, the way i tried anyway.
- Kevin_Stewart
Employee
I was showing you performance output using TCLSH from the command line and the time command. It's probably not likely that you have more than a few defined route domains, all of which you know about in advance, so a literal string map is probably most efficient:
set clientip [string map {%1 "" %2 "" %3 ""} [IP::client_addr]] log local0. "Request from: $clientip"If, however, you truly need something more dynamic, then a regular expression will work. Keep in mind of course that a regular expression is, in general, going to be more CPU-intensive.
set clientip [regsub {%\d+} [IP::client_addr] ""] log local0. "Request from: $clientip - Aurel
Cirrus
Hi Kevin,
I got it now. But still, i noticed that only the first number is removed "%[1-9]".
% string map {%1 "" %2 "" %3 ""} 10.10.10.10%223 10.10.10.1023
I may give you a wrong syntax. My routing domain can have id of 1 to 3 digits (eg. %1 or %123) so i need to remove until 3 digits after the "%".
Hoping that it's more clear for you. Thanking you Aurel
- nitass_89166
Noctilucent
is tcl scan useful?
e.g.
% set test1 "10.10.10.10%1" 10.10.10.10%1 % set test123 "10.10.10.10%123" 10.10.10.10%123 % scan $test1 {%[^%%]%%%d} a b 2 % put $a 10.10.10.10 % put $b 1 % scan $test123 {%[^%%]%%%d} a b 2 % put $a 10.10.10.10 % put $b 123 %- Aurel
Cirrus
Yes Nitass, this is doing the job fine. That's seems fine. As Kevin said, it looks that is using regex and then will consume CPU a little more than "tcl native" commands. Am i right ? I'd like (as far as possible) to do this with the fewest impact on performance. - nitass_89166
Noctilucent
yes, that is correct. :-) - nitass_89166
Noctilucent
oops sorry, i misread. scan command does not use regex.
- nitass
Employee
is tcl scan useful?
e.g.
% set test1 "10.10.10.10%1" 10.10.10.10%1 % set test123 "10.10.10.10%123" 10.10.10.10%123 % scan $test1 {%[^%%]%%%d} a b 2 % put $a 10.10.10.10 % put $b 1 % scan $test123 {%[^%%]%%%d} a b 2 % put $a 10.10.10.10 % put $b 123 %- Aurel
Cirrus
Yes Nitass, this is doing the job fine. That's seems fine. As Kevin said, it looks that is using regex and then will consume CPU a little more than "tcl native" commands. Am i right ? I'd like (as far as possible) to do this with the fewest impact on performance. - nitass
Employee
yes, that is correct. :-) - nitass
Employee
oops sorry, i misread. scan command does not use regex.
- Kevin_Stewart
Employee
The scan and regsub commands both work on regular expressions, so both will do a better job of matching a dynamic set of numbers - at the expense of performance. I'm doubtful that you have a large set of dynamic route domain assignments, so given the assumption that 1) it's a small set, and 2) you already know what those RD values are, an explicit [string map] is probably more efficient. But I want to express that [string map] is an explicit search - you have to know what you're looking for. In your case, since "223" is the RD number, your syntax would look something like this:
string map {%223 "" %224 "" %225 ""} [IP::client_addr] - Aurel
Cirrus
I found a way for removing the next number (only) after the "%", do you see how do the same for others digits that would follow ?
This is my Irule command : set client_ip [string trimright [IP::remote_addr] %1]
- Aurel
Cirrus
I am using now the proper function to get the route domain ID to which i prefix the "%".
But when using string map to replace it with "", it's not working. I'm wondering if this is an interpretation issue, because of the variable "$rd" ?
set rd "%[ROUTE::domain]" set client_ip [string map {$rd ""} [IP::remote_addr]]
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