GSM Modem-kicking Script for RouterOS

Submitted by davidc on Fri, 14/09/2018 - 14:23

I seem to be spending a lot of my time recently hacking around RouterOS bugs. Here's a script that monitors the state of the PPP interface for a GSM modem and does what it can to get it back up and running if necessary.

The script checks three times (10 seconds apart) to see if the PPP interface is connected. If it still isn't, it resets the USB power to try and kick the communication back to life. It waits 30 seconds after doing this then, if it's still not up for two more checks, it reboots the whole Routerboard.

It's ugly as sin but has been successfully deployed to get remote unmanned locations back online. Add it to the scheduler, but make sure the schedule time is greater than the time the script is running, so it doesn't run twice simultaneously. With the default values, that is 70 seconds, an absolute minimum schedule time of 2 minutes.

:local pppInterface "ppp-out1";
:local count 0;
 
:while ( $count < 5 ) do={
  /interface ppp-client monitor $pppInterface once do={
    :if ($status != "connected") do={
      :set count ($count+1);
      :log warning "PPP interface $pppInterface down (#$count)";
 
      :if ( $count = 3 ) do={
        :log warning "PPP still down - resetting USB"; 
        /system routerboard usb power-reset duration=5;
        :delay 20;
      }
      :if ( $count = 5 ) do={ 
        :log warning "PPP still down - rebooting"; 
        /system reboot;
      }
      :delay 10;
    } else={
      :set count 999;
    }
  };
}

Bonus: here's a tiny script that can also be put in the scheduler to periodically upload the battery voltage (in decivolts).

:local decivolts [ /system health get voltage ]
/tool fetch url="http://myserver/set_battery?site=123&decivolts=$decivolts" mode=http keep-result=no