Let’s make an event driven Lua script for processing when some kind of event occurs at the router.
Events Driven by Logs
With the Lua script functionality, a library function, rt.syslogwatch is prepared by which, until a specified string is output in the log a script can be temporarily halted and remain on standby. Event driven is achieved thus.
A tunnel interface switches route via up / down
Let's try making a script that switches the tunnel interface route up / down.
When up / down occurs on a tunnel interface, the following is recorded in the log.
rt.syslogwatch captures this log, and then switches the route.
The functions tunnel_up and tunnel_down, change routes using rt.command when any tunnel interface goes up or down.
With the variable PATTERN configure the waiting log pattern.
With this pattern, with the infinite loop of the while sentence, wait until the tunnel up / down is recorded in the following line in the log. In the rt.syslogwatchargument, write the log to capture with a pattern. The return value is the number of times the log is found and the actually captured arrays of the log string. In this example, rt.syslogwatch omits the number of instances of detection and the argument to specify the monitoring time, so the variable rtn is always 1.
Next, whether or not the tunnel is up or down and the tunnel no. are checked by string.match and PATTERN. In PATTERN there are two capture instances and both support tunnel no. and "Up"/"Down” strings. So, if the tunnel is up, tunnel_up is called, and if it is not, tunnel_down is called.