{ ############################## ### SETTINGS SECTION BEGIN ### ############################## # Recipients to send notifications :global mailAddrTo "mail@mail.ru"; :global mailAddrCopy "mail@mail.com"; # CPU load test duration :global sec 3; # ICMP packets qty :local cnt 10; # Expected qty of received packets # In this case we send 10 packets and expect 1 or more to be received :local cntOkRes 1; # First test host :local host1 ya.ru; # Second test host :local host2 8.8.8.8; ############################## #### SETTINGS SECTION END #### ############################## # CPU load test :global cpu100 0; for x1 from=1 to=$sec do { :if ( [/system resource get cpu-load]="100") do={ :global cpu100 ($cpu100+1); } :delay 1; } :if ( $cpu100 = $sec ) do={ /tool e-mail send to=$mailAddrTo cc=$mailAddrCopy body="Within $sec seconds, CPU load on Mikrotik was 100%. Mikrotik will be restarted!" subject="$[/system identity get name]. Mikrotik CPU load was 100% on $[/system clock get time] $[/system clock get date]" :delay 20; /system reboot; } # Checking default route existence if ([/ip route find where dst-address=0.0.0.0/0] = "") do={ :log warning "No default gateway! USB port will be power cycled!"; # USB port power cycle /system routerboard usb power-reset duration=5s :delay 25; } # Finding IP address of default GW & looking for traffic via it :local defGw [/ip route get [/ip route find where dst-address=0.0.0.0/0] gateway]; # Default route's interface lookup :global ifName [/ip address get [/ip address find where network=$defGw] interface]; # Looking and counting traffic on interface :global wanTrafTx "0"; :global wanTrafTxRes "0"; :global wanTrafRx "0"; :global wanTrafRxRes "0"; for x from=1 to=5 do { /interface monitor-traffic [/interface find name=$ifName] once do={ :global wanTrafTx (tx-bits-per-second / 1024); } /interface monitor-traffic [/interface find name=$ifName] once do={ :global wanTrafRx (rx-bits-per-second / 1024); } if ($wanTrafTx > $wanTrafTxRes) do { :global wanTrafTxRes $wanTrafTx; } if ($wanTrafRx > $wanTrafRxRes) do { :global wanTrafRxRes $wanTrafRx; } :delay 1; } # Comparing actual traffic with reference value (30 kbps). If less, ping hosts. if ($wanTrafTxRes < 30 and $wanTrafRxRes < 30) do { # Hosts test and putting data to variables :local cntRes1 [/ping count=$cnt $host1]; :local cntRes2 [/ping count=$cnt $host2]; # If less than 1 from 10 packets was received if ($cntRes1 < $cntOkRes and $cntRes2 < $cntOkRes) do { # Writing data to log :local logmsg ("Internet connection fail! \n\nCPU Load: ".[/system resource get cpu-load]."% \nWAN speed RX: ".[:tonum $wanTrafRxRes]." kbit/s \nWAN speed TX: ".[:tonum $wanTrafTxRes]." kbit/s \nHost - ".[:tostr $host1]." lost ".[:tonum $cnt] - [:tonum $cntRes1]." packets of ".[:tostr $cnt]." \nHost - ".[:tostr $host2]." lost ".[:tonum $cnt] - [:tonum $cntRes2]." packets of ".[:tostr $cnt]."\n"); :log info $logmsg; # Disabling default route's interface /interface ppp-client disable [/interface ppp-client find name=$ifName]; :delay 10; # Checking modem status / Running "modemStatus" script execute "/system script run \"modemStatus\""; :delay 5s; :global modemStatus; # Enabling default route's interface /interface ppp-client enable [/interface ppp-client find name=$ifName]; :delay 20; # E-maling report /tool e-mail send to=$mailAddrTo cc=$mailAddrCopy body="$logmsg$modemStatus" subject="$[/system identity get name]. Internet connection fails! $[/system clock get time] $[/system clock get date]" } } }