I have an irule that needs to execute some code in two different events, so to not have to put the same code twice in each event I would like to put that code in a function that I can call in both events, but I don’t have a clue of how to do that. For what I have read, functions in tcl are called commands, but I can’t find a reference about how define one. I tried using the proc keyword but when trying to add the irule it throws up an error of “undefined procedure: my_proc”. I defined like:
proc write_log{} {
…
}
and then call it like:
write_log
On the events I need it. Can anyone give a hint or point me to the right documentation ?
proc’s are disabled from within iRules due to performance issues with their blocking nature. If you want to use a set of code that functions on two events, your best bet is to write it twice. Another option is to define the TCL code in a global variable and then use the TCL eval command to execute it. This bypasses any of the load time error checking so use it at your own risk.
when HTTP_REQUEST {
set code {log local0. "this is executed from an eval statement"}
set error [catch { eval $code; } errVar];
if { $error } {
log local0. "Error in code execution: $errVar";
}
}