Skip to content

Commit

Permalink
Add check LTE state update script
Browse files Browse the repository at this point in the history
  • Loading branch information
bubenkoff committed Nov 4, 2022
1 parent 7594345 commit 45b82e9
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 0 deletions.
146 changes: 146 additions & 0 deletions check-lte-state-update
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
#!rsc by RouterOS
# RouterOS script: check-lte-state-update
# Copyright (c) 2018-2022 Christian Hesse <mail@eworm.de>
# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
#
# check for LTE state, send notification
# https://git.eworm.de/cgit/routeros-scripts/about/doc/check-lte-state-update.md

:local 0 "check-lte-state-update";
:global GlobalFunctionsReady;
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; }

:global CurrentLteStatePrimaryBand

:if ([ :typeof $CurrentLteStatePrimaryBand ] != "array") do={
:global CurrentLteStatePrimaryBand ({});
}

:global CurrentLteStateCaBand

:if ([ :typeof $CurrentLteStateCaBand ] != "array") do={
:global CurrentLteStateCaBand ({});
}

:global CurrentLteStateIp

:if ([ :typeof $CurrentLteStateIp ] != "array") do={
:global CurrentLteStateIp ({});
}

:global CheckLteStateUpdateBtestHost
:global CheckLteStateUpdateBtestUser
:global CheckLteStateUpdateBtestPassword

$LogPrintExit2 debug $0 ("Prepared") false;

:local CheckInterface do={
:local Interface $1;

:global Identity;
:global SentLteStateUpdateNotification;
:global CurrentLteStatePrimaryBand;
:global CurrentLteStateCaBand;
:global CurrentLteStateIp;
:global CharacterReplace;
:global LogPrintExit2;
:global ScriptFromTerminal;
:global SendNotification2;
:global SymbolForNotification;
:global CheckLteStateUpdateBtestHost;
:global CheckLteStateUpdateBtestUser;
:global CheckLteStateUpdateBtestPassword;

:local IntName [ /interface/lte/get $Interface name ];
:local Ip [ /ip address get [ find interface=$IntName ] address ]
:local Info;
:do {
:set Info [ /interface/lte/monitor $Interface once as-value ];
} on-error={
$LogPrintExit2 debug $0 ("Could not get latest LTE monitoring information for interface " . \
$IntName . ".") false;
:return false;
}
:local CurrentOperator ($Info->"current-operator");
:local PrimaryBand ($Info->"primary-band");
:local CaBand ($Info->"ca-band");
:local Sinr ($Info->"sinr");
:local Rssi ($Info->"rssi");
:local Rsrq ($Info->"rsrq");
:local Rsrp ($Info->"rsrp");
:local Ri ($Info->"ri");
:local PassedCheck false;
:local CurrentPrimaryBand ($CurrentLteStatePrimaryBand->$IntName);
:local CurrentCaBand ($CurrentLteStateCaBand->$IntName);
:local CurrentIP ($CurrentLteStateIp->$IntName);

:local IpMessage;
:local PrimaryBandMessage;
:local CaBandMessage

:if ($CurrentIP != $Ip) do={
:set IpMessage ("IP address changed from $CurrentIP to $Ip\n");
:set ($CurrentLteStateIp->$IntName) $Ip;
:set PassedCheck (true);
}
:if ($CurrentPrimaryBand != $PrimaryBand) do={
:set PrimaryBandMessage ("Primary band changed from $CurrentPrimaryBand to $PrimaryBand\n");
:set ($CurrentLteStatePrimaryBand->$IntName) $PrimaryBand;
:set PassedCheck (true);
}
:if ($CurrentCaBand != $CaBand) do={
:set CaBandMessage ("CA band changed\n");
:set ($CurrentLteStateCaBand->$IntName) $CaBand;
:set PassedCheck (true);
}

:if ($PassedCheck = false) do={
:if ([ $ScriptFromTerminal $0 ] = true) do={
$LogPrintExit2 info $0 ("No state update for LTE interface " . $IntName . ".") false;
}
:return true;
}

:local DownloadSpeed 0;
:local UploadSpeed 0;

$LogPrintExit2 debug $0 ("Checking the speed for interface " . \
$IntName . ".") false;
/tool speed-test address=[:resolve $CheckLteStateUpdateBtestHost] user=$CheckLteStateUpdateBtestUser password=$CheckLteStateUpdateBtestPassword do={
:set DownloadSpeed ($"tcp-download");
:set UploadSpeed ($"tcp-upload");
}

:local Message;
:set $Message ("LTE interface $IntName on $Identity has the following comm values:
$IpMessage$PrimaryBandMessage$CaBandMessage
CurrentOperator: $CurrentOperator
PrimaryBand: $PrimaryBand
sinr: $Sinr
rssi: $Rssi
rsrq: $Rsrq
rsrp: $Rsrp
ri: $Ri

btest:
download: $DownloadSpeed
upload: $UploadSpeed
");

:if (($SentLteStateUpdateNotification->$IntName) = ($Message)) do={
$LogPrintExit2 debug $0 ("Already sent the LTE state update notification for message " . \
($Message) . ".") false;
:return false;
}

$LogPrintExit2 info $0 ("A new LTE state " . ($Message) . " for " . \
"LTE interface " . $IntName . ".") false;
$SendNotification2 ({ origin=$0; \
subject=([ $SymbolForNotification "sparkles" ] . "LTE state update"); \
message=($Message); silent=true });
:set ($SentLteStateUpdateNotification->$IntName) ($Message);
}

:foreach Interface in=[ /interface/lte/find ] do={
$CheckInterface $Interface;
}
51 changes: 51 additions & 0 deletions doc/check-lte-state-update.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
Notify on LTE state update
==========================

[◀ Go back to main README](../README.md)

> ℹ️ **Info**: This script can not be used on its own but requires the base
> installation. See [main README](../README.md) for details.
Description
-----------

This script is run from scheduler periodically, checking for LTE state changes.

### Sample notification

![check-lte-state-update notification](check-lte-state-update.d/notification.avif)

Requirements and installation
-----------------------------

Just install the script:

$ScriptInstallUpdate check-lte-state-update;

... and create a scheduler:

/system/scheduler/add interval=1h name=check-lte-state-update on-event="/system/script/run check-lte-state-update;" start-time=startup;

Configuration
-------------

The configuration goes to `global-config-overlay`, this is the only parameter:

* `CheckLteStateUpdateBtestHost`: host to test for internet connectivity (btest server)
* `CheckLteStateUpdateBtestUser`: user to test for internet connectivity (btest server)
* `CheckLteStateUpdateBtestPassword`: password to test for internet connectivity (btest server)

Also notification settings are required for
[e-mail](mod/notification-email.md),
[matrix](mod/notification-matrix.md) and/or
[telegram](mod/notification-telegram.md).

See also
--------

* [Notify on RouterOS update](check-routeros-update.md)
* [Install LTE firmware upgrade](unattended-lte-firmware-upgrade.md)

---
[◀ Go back to main README](../README.md)
[▲ Go back to top](#top)
7 changes: 7 additions & 0 deletions global-config
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@
# add more here...
};

# host to test for internet connectivity (btest server)
:global CheckLteStateUpdateBtestHost "example.com";
# user to test for internet connectivity (btest server)
:global CheckLteStateUpdateBtestUser "mikrotik";
# password to test for internet connectivity (btest server)
:global CheckLteStateUpdateBtestPassword "v3ry-s3cr3t";

# This is the address used to send gps data to.
:global GpsTrackUrl "https://example.com/index.php";

Expand Down

0 comments on commit 45b82e9

Please sign in to comment.