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="lupael@i4e.com.bd" 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.
We want the script to run every time the router reboots so from the Start Time drop-down 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 Mikrotik 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!
Comments
0 comments
Please sign in to leave a comment.