Page 1 of 1

Script to reboot if we are having same log entry back to back

Posted: Tue May 30, 2023 5:37 pm
by jaxed7
Hey all
I'm looking for a script to treat viewtopic.php?t=160012 problem and for that I need a script that will reboot the router if we got "connection established from" entries back to back in the log, because that's where OpenVPN will stuck and the router need to be rebooted.
and by back to back I mean if the last 10 log entries are starting with "connection established from"

Re: Script to reboot if we are having same log entry back to back

Posted: Tue May 30, 2023 6:22 pm
by rextended
[…] script that will reboot the router if we got "connection established from" entries back to in the log […]
[…] and by back to back I mean if IN the last LOG CHECK, AT LEAST 10 NEW log entries are starting with "connection established from" […]
:global lastLog
:if ([:typeof $lastLog] != "num") do={:set lastLog 0}
{
:local counter 0
:local id2num do={:return [:tonum "0x$[:pick $1 1 [:len $1]]"]}
/log
:foreach item in=[find where (([$id2num $".id"] > $lastLog) and (buffer=memory) and (message~"connection established from"))] do={
    :set lastLog [$id2num $item]
    :set counter ($counter + 1)
}
:if ($counter > 9) do={/sys reboot}
}

Re: Script to reboot if we are having same log entry back to back

Posted: Wed May 31, 2023 3:48 am
by jaxed7
Thanks a lot man and so sorry for very bad explanation

While the current script is functional, it is set to restart the router upon detecting more than 10 instances of 'connection established from' in the overall log entries. However, to ensure greater precision, the desired behavior is for the router to only restart if there are 10 or more of these entries in a row, uninterrupted by any other log entries, even if there are 100 such entries in total.
I apologize for any confusion caused by my earlier lack of clarity.

Re: Script to reboot if we are having same log entry back to back

Posted: Wed May 31, 2023 10:18 am
by rextended
How the actual version work:
If on the last check are present at least 10 NEW logs with that string, the router reboot,
if are present 1000 logs, but the log are not present as NEW in the last logs check, the router not reboot.

If you schedule that script every 1 minute, and on that minute 10 or more logs with that string are presents, regardless if some other log are in between,
does the router need to be restarted? Obviously yes, so is more precise this version than one version which only checks if there are 10 consecutive ones...

Re: Script to reboot if we are having same log entry back to back

Posted: Wed May 31, 2023 1:52 pm
by jaxed7
Normally when the OpenVPN is working on mikrotik we got "connection established from" and then bunch of other log entries like ": using encoding - AES-256-GCM/[null-digest]" and "<ovpn-test>: connected" but when it does crash we only see "connection established from" on log consecutively.

since there are a lot of users connecting to the OpenVPN server even if I set the scheduler for that script to every 1 minute it's still gonna reboot the router cause in that time window (1 minute) more than 10 users connect to the server and therefore we have more than 10 "connection established from" in the log.

We can increase the number "10" but again later if huge number of users connect to the server within that time window (1 minute) it would falsely trigger a reboot.

Re: Script to reboot if we are having same log entry back to back

Posted: Wed May 31, 2023 3:52 pm
by rextended
Understand.... I think to something....

Re: Script to reboot if we are having same log entry back to back

Posted: Sat Jun 03, 2023 1:02 pm
by jaxed7
I appreciate it man

Re: Script to reboot if we are having same log entry back to back

Posted: Sat Jun 03, 2023 7:58 pm
by rextended
I appreciate it man
I haven't forgotten, it's just that I can't think of anything effective...

Re: Script to reboot if we are having same log entry back to back

Posted: Sun Jun 18, 2023 7:45 pm
by jaxed7
Still nothing? :(
My OpenVPN server was down for several hours due to that bug and I wasn't there to simply restart it :(

Re: Script to reboot if we are having same log entry back to back  [SOLVED]

Posted: Sun Jun 18, 2023 11:02 pm
by rextended
Bravo, mi hai spinto a creare qualcosa di nuovo, che può essere pure usato altrove...

:local id2num do={:return [:tonum "0x$[:pick $1 1 [:len $1]]"]}
/log
:local buffer [find where buffer=memory]
:local start  [$id2num ($buffer->([:len $buffer] - 10))]
:local buffer [find where ((buffer=memory) and ([$id2num $".id"]>=$start) and (message~"connection established from"))]
:if ([:len $buffer] = 10) do={/sys reboot}
Description:
Get all the log buffer in memory, select the tenth one from the end, from these log, catch the last 10 logs, but only when contain "connection established from",
and if the buffer size is 10 (= all the last 10 logs containing "connection established from") , restart the machine...

Re: Script to reboot if we are having same log entry back to back

Posted: Mon Jun 19, 2023 5:35 am
by jaxed7
Grazie mille, davvero. Ti ringrazio di cuore per tutto il tempo e l'impegno che hai dedicato a questo. :D

Re: Script to reboot if we are having same log entry back to back

Posted: Mon Jun 19, 2023 12:36 pm
by rextended
Grazie mille, davvero. Ti ringrazio di cuore per tutto il tempo e l'impegno che hai dedicato a questo. :D
😁

Very very thanks!