Community discussions

MikroTik App
 
VioxxElite
just joined
Topic Author
Posts: 10
Joined: Thu Apr 29, 2021 6:32 pm

Script to inform by email when the equipment was restarted due to power failure

Sat Mar 25, 2023 1:10 pm

Good morning Forum, can you help me to improve this script

Its intention is to write the date and time to the clock.txt file on disk.

It will record this time every 3 minutes

If by chance the equipment is restarted

It will check the uptime, if it is less than 5 minutes, it will go to else, and will not record the current time in the crock.txt file

instead it will take the time of this file and save it in a variable, it will also compare if there is any message in the log containing "Router was rebooted", then it will send an email, informing the last registration time, the current time and an account of how long the equipment was turned off.

/system scheduler add name=Test-Rebooted start-time=startup interval=00:03:00 on-event="
:local date
:local time
:local day
:local month
:local year
:local clock
:local timeoff
:local clocktime

delay 120s;

if ([/system/resource/get uptime] > 00:05:00) do={
   :local day1
   :local month1
   :local year1
   :local newdate [/system clock get date]
   :local newtime [/system clock get time]
   :local clock "clock.txt"

   :set day1 [:pick $newdate 4 6];
   :set month1 [:pick $newdate 0 3];
   :set year1 [:pick $newdate 7 11];

   /file print file="$clock"
   :local newfilecontent "$day1/$month1/$year1, $newtime"
   /file set $clock contents=$newfilecontent
} else={
   :local clock [/file get [/file find name=clock.txt] contents];
   if ([:len [/log find message~"Router was rebooted"]] != 0) do={

   :set date [/system clock get date];
   :set time [/system clock get time];
   :set day [:pick $date 4 6];
   :set month [:pick $date 0 3];
   :set year [:pick $date 7 11];
   :set clocktime [:pick $clock 13 21]
   :set timeoff ($time - clocktime)
   /tool e-mail send to=email@email.com subject="$[/system identity get name] - Router was rebooted - Time off: $timeoff" body="$clock, $[/system identity get name] - Time the router was rebooted
$day/$month/$year, $time, $[/system identity get name] - Time router turned on
Time off: $timeoff" file=clock.txt tls=starttls
   }
}
"
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11991
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script to inform by email when the equipment was restarted due to power failure

Sat Mar 25, 2023 7:44 pm

is not sufficent to put something between /system scheduler [...] on-event=" and " to make a working script for create a scheduler.
If the script inside is not escaped, at first " you get error, and if pasted on terminal is immediately executed, but the scheduler not created.
Also if the unit have flash memory, on reboot you lost the file, and also is for v7 only, instead must be writed for both systems for be usefull to all.
if are not on same line, do not use ; on commands,
"if" requore ":" and must be ":if"
timeoff do not work, because if is rebooted @23:58 and restarted @00:05 you can not obtain "7" from 00:05 - 23:58... (you obtain -23:53)
why uselessly send the file on email, when on email the time is already present?

This update the datetime inside the file, if the uptime is > of 5 minutes
:if ([/system resource get uptime] > 00:05:00) do={
    /system clock
    :local datetimestr "$[get date] $[get time]"
    :local filename "clock.txt"
    :if ([:len [/file find where name="flash" and type="disk"]]=1) do={:set filename "flash/$filename"}
    /file
    print file=$filename ; :delay 1s
    set $filename contents=$datetimestr
}

scheduler code

add interval=5m name=update_clock.txt on-event=":if ([/system resource get uptime] > 00:05:00) do={\r\
    \n    /system clock\r\
    \n    :local datetimestr \"\$[get date] \$[get time]\"\r\
    \n    :local filename \"clock.txt\"\r\
    \n    :if ([:len [/file find where name=\"flash\" and type=\"disk\"]]=1) do={:set filename \"flash/\$filename\"}\r\
    \n    /file\r\
    \n    print file=\$filename ; :delay 1s\r\
    \n    set \$filename contents=\$datetimestr\r\
    \n}\r\
    \n" policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon start-date=jan/01/1970 start-time=00:00:00

And this is the script to send the email
# give sufficent time to RouterOS to resync internal clock, not more 2 minutes or the script go on timeout
:delay 60s

/system clock
:local datetimestr "$[get date] $[get time]"
:local filename "clock.txt"
:if ([:len [/file find where name="flash" and type="disk"]]=1) do={:set filename "flash/$filename"}

:local lastdatetime [/file get [find where name=$filename] contents]

/tool e-mail send \
         to=email@email.com \
    subject="$[/system identity get name] - Router was rebooted after $lastdatetime" \
       body="$datetimestr $[/system identity get name] - Router was rebooted after $lastdatetime"

scheduler code

add name=sendmail_at_startup on-event="# give sufficent time to RouterOS to resync internal clock, not more 2 minutes or the script go on timeout\r\
    \n:delay 60s\r\
    \n\r\
    \n/system clock\r\
    \n:local datetimestr \"\$[get date] \$[get time]\"\r\
    \n:local filename \"clock.txt\"\r\
    \n:if ([:len [/file find where name=\"flash\" and type=\"disk\"]]=1) do={:set filename \"flash/\$filename\"}\r\
    \n\r\
    \n:local lastdatetime [/file get [find where name=\$filename] contents]\r\
    \n\r\
    \n/tool e-mail send \\\r\
    \n         to=email@email.com \\\r\
    \n    subject=\"\$[/system identity get name] - Router was rebooted after \$lastdatetime\" \\\r\
    \n       body=\"\$datetimestr \$[/system identity get name] - Router was rebooted after \$lastdatetime\"\r\
    \n" policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon start-time=startup
Last edited by rextended on Sat Mar 25, 2023 8:12 pm, edited 3 times in total.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3402
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: Script to inform by email when the equipment was restarted due to power failure

Sat Mar 25, 2023 7:59 pm

Just a thought. Pasting the script into winbox is likely easier than escaping it manually. That will generate the escaped texted on export from CLI, so you can use the script fragment elsewhere via the CLI since it will have the escapes added on export.

(or see @rextended above)
Last edited by Amm0 on Sat Mar 25, 2023 8:00 pm, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11991
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script to inform by email when the equipment was restarted due to power failure

Sat Mar 25, 2023 8:00 pm

Just a thought. Pasting the script into winbox is likely easier than escaping it manually. That will generate the escaped texted on export from CLI, so you can use it fragment elsewhere via the CLI since it will have the escapes added on export.
exactly, I'm working on redone the script and update previous post.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11991
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script to inform by email when the equipment was restarted due to power failure

Sat Mar 25, 2023 8:21 pm

If you want know the time differencies between the current and the previous date just use this:
:global datetime2epoch do={
    :local dtime [:tostr $1]
    /system clock
    :local cyear [get date] ; :if ($cyear ~ "....-..-..") do={:set cyear [:pick $cyear 0 4]} else={:set cyear [:pick $cyear 7 11]}
    :if (([:len $dtime] = 10) or ([:len $dtime] = 11)) do={:set dtime "$dtime 00:00:00"}
    :if ([:len $dtime] = 15) do={:set dtime "$[:pick $dtime 0 6]/$cyear $[:pick $dtime 7 15]"}
    :if ([:len $dtime] = 14) do={:set dtime "$cyear-$[:pick $dtime 0 5] $[:pick $dtime 6 14]"}
    :if ([:len $dtime] =  8) do={:set dtime "$[get date] $dtime"}
    :if ([:tostr $1] = "") do={:set dtime ("$[get date] $[get time]")}
    :local vdate [:pick $dtime 0 [:find $dtime " " -1]]
    :local vtime [:pick $dtime ([:find $dtime " " -1] + 1) [:len $dtime]]
    :local vgmt  [get gmt-offset]; :if ($vgmt > 0x7FFFFFFF) do={:set vgmt ($vgmt - 0x100000000)}
    :if ($vgmt < 0) do={:set vgmt ($vgmt * -1)}
    :local arrm  [:toarray "0,0,31,59,90,120,151,181,212,243,273,304,334"]
    :local vdoff [:toarray "0,4,5,7,8,10"]
    :local MM    [:pick $vdate ($vdoff->2) ($vdoff->3)]
    :local M     [:tonum $MM]
    :if ($vdate ~ ".../../....") do={
        :set vdoff [:toarray "7,11,1,3,4,6"]
        :set M     ([:find "xxanebarprayunulugepctovecANEBARPRAYUNULUGEPCTOVEC" [:pick $vdate ($vdoff->2) ($vdoff->3)] -1] / 2)
        :if ($M>12) do={:set M ($M - 12)}
    }
    :local yyyy  [:pick $vdate ($vdoff->0) ($vdoff->1)] ; :if ((($yyyy - 1968) % 4) = 0) do={:set ($arrm->1) -1; :set ($arrm->2) 30}
    :local totd  ((($yyyy - 1970) * 365) + (($yyyy - 1968) / 4) + ($arrm->$M) + ([:pick $vdate ($vdoff->4) ($vdoff->5)] - 1))
    :return      (((((($totd * 24) + [:pick $vtime 0 2]) * 60) + [:pick $vtime 3 5]) * 60) + [:pick $vtime 6 8] - $vgmt)
}

{
:local filename "clock.txt"
:if ([:len [/file find where name="flash" and type="disk"]]=1) do={:set filename "flash/$filename"}
:local lastdatetime [/file get [find where name=$filename] contents]

:local unixnow   [$datetime2epoch]
:local timestamp [$datetime2epoch $lastdatetime]
:local diff      ($unixnow - $timestamp)
:put "Seconds of differencies between the two dates: $diff"
}
Last edited by rextended on Fri May 19, 2023 3:17 pm, edited 6 times in total.
 
VioxxElite
just joined
Topic Author
Posts: 10
Joined: Thu Apr 29, 2021 6:32 pm

Re: Script to inform by email when the equipment was restarted due to power failure

Mon Mar 27, 2023 3:01 pm

good morning rextended

I didn't understand very well how to incorporate this third script to calculate the difference, with the other two that you had informed me.

You can clear this doubt, or make 1 to 2 scripts to calculate this difference in the time that the equipment was turned off. it will help me a lot.

Thanks
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11991
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script to inform by email when the equipment was restarted due to power failure

Mon Mar 27, 2023 3:17 pm

Ignoring the complex function
:global datetime2epoch do={
[…]
}
(must be pasted exactly as-is on top of the scirpt)

I prefer you learn to use scripting.
Using the example at the bottom of the script, try to send that string to email...
You must place this lines:
:local unixnow [$datetime2epoch]
:local timestamp [$datetime2epoch $lastdatetime]
:local diff ($unixnow - $timestamp)
on right point and add $diff inside the text of the email....

If you do only copy & paste, you not learn anything.
Last edited by rextended on Thu May 11, 2023 1:27 am, edited 1 time in total.
 
VioxxElite
just joined
Topic Author
Posts: 10
Joined: Thu Apr 29, 2021 6:32 pm

Re: Script to inform by email when the equipment was restarted due to power failure

Mon Mar 27, 2023 7:42 pm

rextended, I understand that you are trying to help and that you don't have to chew everything out.

I swear I really tried to make it work, but to no avail.

I even managed with the first two scripts you advised, but the third part, everything I tried to implement it, didn't work
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11991
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Script to inform by email when the equipment was restarted due to power failure

Mon Mar 27, 2023 7:45 pm

What you give on terminal if you paste full (3rd) script?
(at least the first script must be executed one time, as-is, without changes)


Use this as 2nd script:
# give sufficent time to RouterOS to resync internal clock, not more 2 minutes or the script go on timeout
:delay 60s

:global datetime2epoch do={
    :local dtime [:tostr $1]
    /system clock
    :local cyear [get date] ; :if ($cyear ~ "....-..-..") do={:set cyear [:pick $cyear 0 4]} else={:set cyear [:pick $cyear 7 11]}
    :if (([:len $dtime] = 10) or ([:len $dtime] = 11)) do={:set dtime "$dtime 00:00:00"}
    :if ([:len $dtime] = 15) do={:set dtime "$[:pick $dtime 0 6]/$cyear $[:pick $dtime 7 15]"}
    :if ([:len $dtime] = 14) do={:set dtime "$cyear-$[:pick $dtime 0 5] $[:pick $dtime 6 14]"}
    :if ([:len $dtime] =  8) do={:set dtime "$[get date] $dtime"}
    :if ([:tostr $1] = "") do={:set dtime ("$[get date] $[get time]")}
    :local vdate [:pick $dtime 0 [:find $dtime " " -1]]
    :local vtime [:pick $dtime ([:find $dtime " " -1] + 1) [:len $dtime]]
    :local vgmt  [get gmt-offset]; :if ($vgmt > 0x7FFFFFFF) do={:set vgmt ($vgmt - 0x100000000)}
    :if ($vgmt < 0) do={:set vgmt ($vgmt * -1)}
    :local arrm  [:toarray "0,0,31,59,90,120,151,181,212,243,273,304,334"]
    :local vdoff [:toarray "0,4,5,7,8,10"]
    :local MM    [:pick $vdate ($vdoff->2) ($vdoff->3)]
    :local M     [:tonum $MM]
    :if ($vdate ~ ".../../....") do={
        :set vdoff [:toarray "7,11,1,3,4,6"]
        :set M     ([:find "xxanebarprayunulugepctovecANEBARPRAYUNULUGEPCTOVEC" [:pick $vdate ($vdoff->2) ($vdoff->3)] -1] / 2)
        :if ($M>12) do={:set M ($M - 12)}
    }
    :local yyyy  [:pick $vdate ($vdoff->0) ($vdoff->1)] ; :if ((($yyyy - 1968) % 4) = 0) do={:set ($arrm->1) -1; :set ($arrm->2) 30}
    :local totd  ((($yyyy - 1970) * 365) + (($yyyy - 1968) / 4) + ($arrm->$M) + ([:pick $vdate ($vdoff->4) ($vdoff->5)] - 1))
    :return      (((((($totd * 24) + [:pick $vtime 0 2]) * 60) + [:pick $vtime 3 5]) * 60) + [:pick $vtime 6 8] - $vgmt)
}

{
/system clock
:local datetimestr "$[get date] $[get time]"
:local filename "clock.txt"
:if ([:len [/file find where name="flash" and type="disk"]]=1) do={:set filename "flash/$filename"}

:local lastdatetime [/file get [find where name=$filename] contents]

:local unixnow   [$datetime2epoch]
:local timestamp [$datetime2epoch $lastdatetime]
:local diff      ($unixnow - $timestamp)

/tool e-mail send \
         to=email@email.com \
    subject="$[/system identity get name] - Router was rebooted after $lastdatetime" \
       body="$datetimestr $[/system identity get name] - Router was rebooted after $lastdatetime - Differencies $diff seconds"
}
The e-mail part must be obviously fixed accordingly your mail settings.
Last edited by rextended on Fri May 19, 2023 3:17 pm, edited 8 times in total.
 
VioxxElite
just joined
Topic Author
Posts: 10
Joined: Thu Apr 29, 2021 6:32 pm

Re: Script to inform by email when the equipment was restarted due to power failure

Thu Apr 06, 2023 7:02 pm

Hello rextended

Sorry for the late reply, I was recovering from surgery.

I modified the second part of the script, not sure if it was the smartest way, or the prettiest one, but it works
:delay 60s

:global datetime2epoch do={
    :local vdate [:pick $1 0 [:find $1 " " -1]]
    :local vtime [:pick $1 ([:find $1 " " -1] + 1) [:len $1]]
    :local vgmt  [/system clock get gmt-offset]; :if ($vgmt > 0x7FFFFFFF) do={:set vgmt ($vgmt - 0x100000000)}
    :if ($vgmt < 0) do={:set vgmt ($vgmt * -1)}
    :local arrm  [:toarray "0,0,31,59,90,120,151,181,212,243,273,304,334"]
    :local yyyy  [:pick $vdate 7 11] ; :if ((($yyyy - 1972) % 4) = 0) do={:set ($arrm->1) -1; :set ($arrm->2) 30}
    :local M     ([:find "xxanebarprayunulugepctovecANEBARPRAYUNULUGEPCTOVEC" [:pick $vdate 1 3] -1] / 2); :if ($M>12) do={:set M ($M - 12)}
    :local totd  ((($yyyy - 1970) * 365) + (($yyyy - 1972) / 4) + ($arrm->$M) + [:pick $vdate 4  6])
    :return      (((((($totd * 24) + [:pick $vtime 0  2]) * 60) + [:pick $vtime 3  5]) * 60) + [:pick $vtime 6  8] - $vgmt)
}

{
/system clock
:local datetimestr "$[get date] $[get time]"
:local filename "clock.txt"
:if ([:len [/file find where name="flash" and type="disk"]]=1) do={:set filename "flash/$filename"}

:local lastdatetime [/file get [find where name=$filename] contents]

:local unixnow   [$datetime2epoch ("$[/sys clock get date] $[/sys clock get time]")]
:local timestamp [$datetime2epoch $lastdatetime]
:local diff      ($unixnow - $timestamp)

:local time $diff

:local days ($time / 60 / 60 / 24)
:local hours (($time / 60 / 60) % 24)
:local minutes (($time / 60) % 60)
:local seconds ($time % 60)

:local days ($days+00)
:local hours ($hours+00)
:local minutes ($minutes+00)
:local seconds ($seconds+00)

[:parse ":local timeoff ($hours:$minutes:$seconds)"]
:local timeoff ($days."d"." ".$timeoff)

/tool e-mail send \
   to=email@email.com \
   subject="$[/system identity get name] - Router was rebooted - Time off: $timeoff" \
   body=" $lastdatetime, $[/system identity get name] - Time the router was rebooted\n \
         $datetimestr, $[/system identity get name] - Router was rebooted after\n \
         $timeoff"
}

Who is online

Users browsing this forum: No registered users and 10 guests