Community discussions

MikroTik App
 
User avatar
EIKA
newbie
Topic Author
Posts: 32
Joined: Thu Dec 28, 2017 7:29 pm
Location: Berlin, Germany

Problem with script (dummy question)

Thu Dec 28, 2017 7:36 pm

Hi all!

I am old MikroTik user, but not a developer at all. I've used some script for a long time (a few years), but unfortunately it's stopped working after a few RouterOS updates. And I missed that's moment. Now I am pretty sure that it's doesn't work, and I started my small investigation. I pasted it into Terminal and tried to debug, but can't see any syntax highlights or something. I have no ideas what to do next. Could you please look into?

Here is a script: https://2keep.net/3g-internet-connectio ... -mikrotik/ This script checks CPU load, WLAN, USB modem, etc., and if there are any problems, restarts OS. It's written as 2 scripts, Test-3G-Link and modemStatus. They are both available at link specified.

Please help. Beforehand thanks!
 
User avatar
EIKA
newbie
Topic Author
Posts: 32
Joined: Thu Dec 28, 2017 7:29 pm
Location: Berlin, Germany

Re: Problem with script (dummy question)

Sat Dec 30, 2017 6:22 pm

Anybody?
 
User avatar
EIKA
newbie
Topic Author
Posts: 32
Joined: Thu Dec 28, 2017 7:29 pm
Location: Berlin, Germany

Re: Problem with script (dummy question)

Sun Dec 31, 2017 11:25 am

They were a few synthax errors with do operator, but they has been fixed. But still no luck. Here is new code with no comments:
{

:global mailAddrTo "yourmail@dom.ru";
:global mailAddrCopy "yourbackupmail@dom2.com";
:global sec 3;
:local cnt 10;
:local cntOkRes 1;
:local host1 ya.ru;
:local host2 8.8.8.8;
:global cpu100 0;
for x1 from=1 to=$sec do={
   :if ( [/system resource get cpu-load]="1")  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;
}
if ([/ip route find where dst-address=0.0.0.0/0]  = "") do={
   :log warning "No default gateway! USB port will be power cycled!";
   /system routerboard usb power-reset duration=5s
   :delay 25;
}
:local defGw [/ip route get [/ip route find where dst-address=0.0.0.0/0] gateway];
:global ifName [/ip address get [/ip address find where network=$defGw] 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;
}
if ($wanTrafTxRes < 30 and $wanTrafRxRes < 30) do={
   :local cntRes1 [/ping count=$cnt $host1];
   :local cntRes2 [/ping count=$cnt $host2];
   if ($cntRes1 < $cntOkRes and $cntRes2 < $cntOkRes) do={
         :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;
         /interface ppp-client disable [/interface ppp-client find name=$ifName];
         :delay 10;
          execute "/system script run \"modemStatus\"";
         :delay 5s;
         :global modemStatus;
         /interface ppp-client enable [/interface ppp-client find name=$ifName];
         :delay 20;
         /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]"
      }
   }
}

And daughter script, modemStatus:
:global modemStatus;
:global ifName;
:global rssi;
:global operator;
:local i 0;
/interface ppp-client info $ifName do={
:set i ($i+1);
:if ($i=3) do={
:global rssi "\nSignal: $"signal-strengh"\n";
:global operator "Operator: $"current-operator"";

:set modemStatus "\nModem Status: $"modem-status"\nFunctionality: $"functionality"\nManufacturer: $"manufacturer"\nModel: $"model"\nRevision: $"revision"\nSerial Number: $"serial-number"\nCurrent Operator: $"current-operator"\nAccess Technology: $"access-technology"\nSignal Strength: $"signal-strengh"";

:log info $modemStatus;

:local stop {[:find $"signal-strengh" "dBm";]-1};
:local rssi1 {:tonum [:pick $"signal-strengh" 1 $stop]};

:log info $rssi1;

/system script job remove [find script=modemStatus ];
}}

You must have USB modem as PPP interface to test this script in full. It's easy to test scipt just by replacing host1 and host2 with any unreachable ones.

What's the problem with code? Thanks in advance and happy New Year!