Community discussions

MikroTik App
 
jaxed7
newbie
Topic Author
Posts: 32
Joined: Wed May 17, 2023 11:15 pm

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

Tue May 30, 2023 5:37 pm

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"
Last edited by jaxed7 on Wed May 31, 2023 3:54 am, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

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

Tue May 30, 2023 6:22 pm

[…] 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}
}
 
jaxed7
newbie
Topic Author
Posts: 32
Joined: Wed May 17, 2023 11:15 pm

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

Wed May 31, 2023 3:48 am

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.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

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

Wed May 31, 2023 10:18 am

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...
 
jaxed7
newbie
Topic Author
Posts: 32
Joined: Wed May 17, 2023 11:15 pm

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

Wed May 31, 2023 1:52 pm

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.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

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

Wed May 31, 2023 3:52 pm

Understand.... I think to something....
 
jaxed7
newbie
Topic Author
Posts: 32
Joined: Wed May 17, 2023 11:15 pm

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

Sat Jun 03, 2023 1:02 pm

I appreciate it man
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

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

Sat Jun 03, 2023 7:58 pm

I appreciate it man
I haven't forgotten, it's just that I can't think of anything effective...
 
jaxed7
newbie
Topic Author
Posts: 32
Joined: Wed May 17, 2023 11:15 pm

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

Sun Jun 18, 2023 7:45 pm

Still nothing? :(
My OpenVPN server was down for several hours due to that bug and I wasn't there to simply restart it :(
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

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

Sun Jun 18, 2023 11:02 pm

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...
 
jaxed7
newbie
Topic Author
Posts: 32
Joined: Wed May 17, 2023 11:15 pm

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

Mon Jun 19, 2023 5:35 am

Grazie mille, davvero. Ti ringrazio di cuore per tutto il tempo e l'impegno che hai dedicato a questo. :D
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

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

Mon Jun 19, 2023 12:36 pm

Grazie mille, davvero. Ti ringrazio di cuore per tutto il tempo e l'impegno che hai dedicato a questo. :D
😁

Very very thanks!

Who is online

Users browsing this forum: philipp84 and 20 guests