MikroTik Script: Router Rebooted Script

MikroTik Script: Router Rebooted Script

This is a useful little RouterOS script that will email you a nice report when your router reboots. The emailed report contains recent critical log events that may point you to the cause for the reboot. ie “router was rebooted without proper shutdown” or “out of memory condition was detected”.

The Router Reboot Script

:delay 1

:local reportBody ""

:local deviceName [/system identity get name]
:local deviceDate [/system clock get date]
:local deviceTime [/system clock get time]
:local hwModel [/system routerboard get model]
:local rosVersion [/system package get system version]
:local currentFirmware [/system routerboard get current-firmware]
:local upgradeFirmware [/system routerboard get upgrade-firmware]


:set reportBody ($reportBody . "Router Reboot Report for $deviceName\n")
:set reportBody ($reportBody . "Report generated on $deviceDate at $deviceTime\n\n")

:set reportBody ($reportBody . "Hardware Model: $hwModel\n")
:set reportBody ($reportBody . "RouterOS Version: $rosVersion\n")
:set reportBody ($reportBody . "Current Firmware: $currentFirmware\n")
:set reportBody ($reportBody . "Upgrade Firmware: $upgradeFirmware")
if ( $currentFirmware < $upgradeFirmware) do={
:set reportBody ($reportBody . "NOTE: You should upgrade the RouterBOARD firmware!\n")
}

:set reportBody ($reportBody . "\n\n=== Critical Log Events ===\n" )

:local x
:local ts
:local msg
foreach i in=([/log find where topics~"critical"]) do={
:set $ts [/log get $i time]
:set $msg [/log get $i message]
:set $reportBody ($reportBody  . $ts . " " . $msg . "\n" )
}

:set reportBody ($reportBody . "\n=== end of report ===\n")

/tool e-mail send subject="[$deviceName] Router Reboot Report" to="yourname@example.com" body=$reportBody

Install the script

Using Winbox, copy and paste the above script into System | Scripts on your MikroTik Router.  The script should be named router-reboot-report.  Here is a screenshot of the installed routeros script.

Setup script to run on Startup

After you have added the script, navigate to System | Scheduler and create a new schedule to run on startup. Here is a screenshot of what the schedule looks like.

Mikrotik Winbox Scheduler Screenshot

We want the script to run every time the router reboots so from the Start Time dropdown choose “startup

In the On Event section of the schedule paste the following commands.

:delay 30
/system script run router-reboot-report

The script name should exactly match the name of the script you added.  The 30 second delay gives the router a chance to get connectivity after reboot.  If the email fails to send then you may need to increase the delay.

Configure Logging

For this script to function properly you must configure persistent logging on your MikoTik Router.  If your logs are not persistent across reboots you may miss some of the critical log events that tell the story of why it reboot in the first place.  To configure persistent logging please see the Logging page on the mikrotik wiki.

Example Email Report

This is what the report looks like, you will receive this email each time your router reboots.  As you can see in the example my router recently rebooted with an out of memory condition.  The report also provides the current version RouterOS packager version as well as the firmware version information.

This script can help troubleshoot random reboots and unexpected crashes. I hope you find this MikroTik Script useful!

Leave a comment below or checkout my other MikroTik Tutorials.

NetScout LinkRunner G2

LinkRunner G2 is the ultimate network cable test tool


CAT5 Cable Tester, Measure Cable Length,
PoE Voltage, Network Connectivity, Switch Port ID
Optional Wireless & Fiber Optics Modules
Check Price on Amazon

pictory

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Ads Blocker Image Powered by Code Help Pro

πŸ™πŸ™A Humble Request to Disable AdBlock πŸ™πŸ™

You can close this message & continue reading but...
❀️❀️❀️ Please consider visiting one of my sponsors first ❀️❀️❀️

DigitalOcean πŸš€

Sign up and get a $200, 60-day credit to try DO.
Spend $25 after your credit expires and I will also get $25 in credit!
DigitalOcean Referral Badge

Pictory πŸ€–

Create amazing videos using Pictorys AI powered software.
Its FREE to create your first 3 video projects
pictory


Hi Reader, I noticed that you are using an ad blocker while visiting my website. While I completely understand that excessive ads can hinder your browsing experience, ad revenue helps pay for the cost associated with operating this website.

jcutrer.com is a labor of love, created with the primary aim to provide you with quality content, free of cost. It’s a space where I share information, ideas, and insights that I hope have a meaningful impact. However, maintaining and updating this platform incurs substantial costs.

Sincerely,
Jonathan