# This script sends an email when the routerBOARD has restarted # if the Scheduler is set to run it at Start Time 'startup' # with a On Event of eg ':delay 20; /system script run router-reboot-report' # It also sets some variables which can be used by other scripts # based on script by J Cutrer, https://jcutrer.com/howto/networking/mikrotik/router-rebooted-script, 9 Sep 21 # Denis de Castro, BMCP@pm.me. Please send corrections and improvements. # version 5, 16 Dec 22 # The script assumes email has been set up on the routerBOARD. Insert here the address you want emails sent to: :local addressTo "someone@example.com" # Script is currently set to monitor the IPv6 pool assigned to the following WAN interface; change if necessary to your WAN interface: :local WANinterface "ether1" # Script starts :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] :set reportBody ($reportBody . "\nRouter Reboot Report\n") :set reportBody ($reportBody . " - - - - - - - - - - - - - - - -\n\n") :set reportBody ($reportBody . "$deviceName, a $hwModel running routerOS version $rosVersion, restarted shortly before $deviceTime on $deviceDate.\n\n") :local x :local ts :local msg :if ([:len [/log find where topics="critical"]] > 0) do={ :set reportBody ($reportBody . "\nCritical Log Events\n") :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 . "\nEnd of report\n\n") } else={ :set $reportBody ($reportBody . "No critical-level log entries were found\n\n") } #set global variables :global countScheduled [:len [/system schedule find]]; #used by the script security-alert to check for schedule insertion :global countScripts [:len [/system script find]]; #used by the script security-alert to check for script insertion :global lastStartDate $deviceDate; #used for uptime display in script weekly-report :global lastTrafficCounterResetDate $deviceDate; #used for traffic volume estimation in script weekly-report :global currentPrefix; #reference a global variable #next: check if there exists an entry with the named WAN interface for getting the prefix :if ([:len [/ipv6 dhcp-client find where interface=$WANinterface]] >0 ) do={ :set $currentPrefix [/ipv6 dhcp-client get [find interface=$WANinterface] prefix]; #get IPv6 prefix assigned by ISP :set $currentPrefix [:pick $currentPrefix 0 [:find $currentPrefix ","]]; #remove uptime readout from it } else={ :set $currentPrefix "Unavailable. Check you have set the correct name for the WAN interface in the script"; } :set reportBody ($reportBody . "Global variables set:\n") :set reportBody ($reportBody . " During security checks, these are used to detect new entries;\n") :set reportBody ($reportBody . " 'countScheduled', the number of items in System>Scheduler [current value; $countScheduled]\n") :set reportBody ($reportBody . " 'countScripts', the number of items in System>Scripts [current value; $countScripts]\n") :set reportBody ($reportBody . " This is used in the display of device uptime;\n") :set reportBody ($reportBody . " 'lastStartDate', the date of the last device startup [current value; $lastStartDate]\n") :set reportBody ($reportBody . " This is used when reporting data transfer volumes;\n") :set reportBody ($reportBody . " 'lastTrafficCounterResetDate', the date of the last device startup [current value; $lastTrafficCounterResetDate]\n") :set reportBody ($reportBody . " This is used for alerts concerning IPv6 address prefix changes;\n") :set reportBody ($reportBody . " 'currentPrefix', the current IPv6 address prefix [current value; $currentPrefix]\n") /tool e-mail send subject="$deviceName has restarted" to=$addressTo body=$reportBody