Send email about reboot

From MikroTik Wiki
Revision as of 17:05, 22 September 2013 by Davis (talk | contribs)
Jump to: navigation, search

This script is meant for being run on router startup (using scheduler). Script sends an email with time of router reboot and entire log once 90 seconds have been passed since initial time synchronization from NTP. Besides event about reboot is logged once time has been synchronized from NTP.

:while ( [:pick [/system clock get date] 7 11]<"2013" ) do={ :delay 10s }
:delay 10s
/log info "time updated; uptime: $[/system resource get uptime]"
:local es "$[/system identity get name] rebooted on $[/system clock get date] $[/system clock get time] uptime $[/system resource get uptime]"
:delay 90s
:local eb "Log contents (with 90 seconds delay):\r\n"
:foreach le in=[/log print as-value] do={
  :set eb ($eb.[:tostr [($le->"time")]]." ".[:tostr [($le->"topics")]].": ".[:tostr [($le->"message")]]."\r\n")
/tool e-mail send to="" subject=$es body=$eb

This script helps to keep track of router reboots (e.g. of reboots caused by power interruptions).

NB Script has not been tested with logging to disk enabled. Also this script won't wait for time synchronization on any machine with real-time clock (e.g. x86 PC). In case time synchronization is not available after reboot, the script will send e-mail once the time is synchronized (date and time in subject will be from the moment of time synchronization and not from the moment of reboot).

Old (12 March 2012) version is available here.

Special thanks to mrz for suggested improvements.

Davis 15:05, 22 September 2013 (UTC)