Send email about reboot

From MikroTik Wiki
Revision as of 17:31, 27 July 2014 by Davis (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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 ( [/system ntp client get status]!="synchronized" ) 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="enter_your_email_here@example.com" subject=$es body=$eb

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

This script requires NTP package (from All packages download).

NB Script has not been tested with logging to disk enabled. 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 (22 September 2013) version, not requiring NTP package and working only on pre-6.16 versions of RouterOS, is available here.

Old (12 March 2012) version is available here.

Special thanks to mrz for suggested improvements.

Davis 17:31, 27 July 2014 (EEST)