Skip to content

Commit dcf7445

Browse files
committed
Add check LTE state update script
1 parent 7594345 commit dcf7445

File tree

3 files changed

+229
-0
lines changed

3 files changed

+229
-0
lines changed

check-lte-state-update

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
#!rsc by RouterOS
2+
# RouterOS script: check-lte-state-update
3+
# Copyright (c) 2018-2022 Christian Hesse <[email protected]>
4+
# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
5+
#
6+
# check for LTE state, send notification
7+
# https://git.eworm.de/cgit/routeros-scripts/about/doc/check-lte-state-update.md
8+
9+
:local 0 "check-lte-state-update";
10+
:global GlobalFunctionsReady;
11+
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
12+
13+
:global CurrentLteStatePrimaryBand
14+
15+
:if ([ :typeof $CurrentLteStatePrimaryBand ] != "array") do={
16+
:global CurrentLteStatePrimaryBand ({});
17+
}
18+
19+
:global CurrentLteStateCaBand
20+
21+
:if ([ :typeof $CurrentLteStateCaBand ] != "array") do={
22+
:global CurrentLteStateCaBand ({});
23+
}
24+
25+
:global CurrentLteStateIp
26+
27+
:if ([ :typeof $CurrentLteStateIp ] != "array") do={
28+
:global CurrentLteStateIp ({});
29+
}
30+
31+
$LogPrintExit2 debug $0 ("Prepared") false;
32+
33+
:local CheckInterface do={
34+
:local Interface $1;
35+
36+
:global Identity;
37+
:global SentLteStateUpdateNotification;
38+
:global CurrentLteStatePrimaryBand;
39+
:global CurrentLteStateCaBand;
40+
:global CurrentLteStateIp;
41+
:global CharacterReplace;
42+
:global LogPrintExit2;
43+
:global ScriptFromTerminal;
44+
:global SendNotification2;
45+
:global SymbolForNotification;
46+
:global CheckLteStateUpdateBtestHost;
47+
:global CheckLteStateUpdateBtestUser;
48+
:global CheckLteStateUpdateBtestPassword;
49+
:global CheckLteStateUpdateIp;
50+
:if ([ :typeof $CheckLteStateUpdateIp ] != "bool") do={
51+
:global CheckLteStateUpdateIp (true);
52+
}
53+
:global CheckLteStateUpdatePrimaryBand;
54+
:if ([ :typeof $CheckLteStateUpdatePrimaryBand ] != "bool") do={
55+
:global CheckLteStateUpdatePrimaryBand (false);
56+
}
57+
:global CheckLteStateUpdateCABand;
58+
:if ([ :typeof $CheckLteStateUpdateCABand ] != "bool") do={
59+
:global CheckLteStateUpdateCABand (false);
60+
}
61+
62+
:local IntName [ /interface/lte/get $Interface name ];
63+
:local Ip [ /ip address get [ find interface=$IntName ] address ]
64+
:local Info;
65+
:do {
66+
:set Info [ /interface/lte/monitor $Interface once as-value ];
67+
} on-error={
68+
$LogPrintExit2 debug $0 ("Could not get latest LTE monitoring information for interface " . \
69+
$IntName . ".") false;
70+
:return false;
71+
}
72+
:local CurrentOperator ($Info->"current-operator");
73+
:local PrimaryBand ($Info->"primary-band");
74+
:local CaBand ($Info->"ca-band");
75+
:local Sinr ($Info->"sinr");
76+
:local Rssi ($Info->"rssi");
77+
:local Rsrq ($Info->"rsrq");
78+
:local Rsrp ($Info->"rsrp");
79+
:local Ri ($Info->"ri");
80+
:local PassedCheck false;
81+
:local CurrentPrimaryBand ($CurrentLteStatePrimaryBand->$IntName);
82+
:local CurrentCaBand ($CurrentLteStateCaBand->$IntName);
83+
:local CurrentIP ($CurrentLteStateIp->$IntName);
84+
85+
:local IpMessage;
86+
:local PrimaryBandMessage;
87+
:local CaBandMessage
88+
89+
:if ($CheckLteStateUpdateIp && $CurrentIP != $Ip) do={
90+
:set IpMessage ("IP address changed from $CurrentIP to $Ip\n");
91+
:set ($CurrentLteStateIp->$IntName) $Ip;
92+
:set PassedCheck (true);
93+
}
94+
:if ($CheckLteStateUpdatePrimaryBand && $CurrentPrimaryBand != $PrimaryBand) do={
95+
:set PrimaryBandMessage ("Primary band changed from $CurrentPrimaryBand to $PrimaryBand\n");
96+
:set ($CurrentLteStatePrimaryBand->$IntName) $PrimaryBand;
97+
:set PassedCheck (true);
98+
}
99+
:if ($CheckLteStateUpdateCABand && $CurrentCaBand != $CaBand) do={
100+
:set CaBandMessage ("CA band changed\n");
101+
:set ($CurrentLteStateCaBand->$IntName) $CaBand;
102+
:set PassedCheck (true);
103+
}
104+
105+
:if ($PassedCheck = false) do={
106+
:if ([ $ScriptFromTerminal $0 ] = true) do={
107+
$LogPrintExit2 info $0 ("No state update for LTE interface " . $IntName . ".") false;
108+
}
109+
:return true;
110+
}
111+
112+
:local BtestMessage;
113+
:if ($CheckLteStateUpdateBtestHost) do={
114+
$LogPrintExit2 debug $0 ("Checking the speed for interface " . \
115+
$IntName . ".") false;
116+
/tool speed-test test-duration=5 address=[:resolve $CheckLteStateUpdateBtestHost] user=$CheckLteStateUpdateBtestUser password=$CheckLteStateUpdateBtestPassword do={
117+
:local DownloadSpeed;
118+
:local UploadSpeed;
119+
120+
:set DownloadSpeed ($"tcp-download");
121+
:set UploadSpeed ($"tcp-upload");
122+
:set BtestMessage ("
123+
btest:
124+
$DownloadSpeed
125+
$UploadSpeed
126+
");
127+
}
128+
}
129+
130+
:local Message;
131+
:set $Message ("LTE interface $IntName on $Identity has the following comm values:
132+
$IpMessage$PrimaryBandMessage$CaBandMessage
133+
CurrentOperator: $CurrentOperator
134+
PrimaryBand: $PrimaryBand
135+
sinr: $Sinr
136+
rssi: $Rssi
137+
rsrq: $Rsrq
138+
rsrp: $Rsrp
139+
ri: $Ri
140+
$BtestMessage
141+
");
142+
143+
:if (($SentLteStateUpdateNotification->$IntName) = ($Message)) do={
144+
$LogPrintExit2 debug $0 ("Already sent the LTE state update notification for message " . \
145+
($Message) . ".") false;
146+
:return false;
147+
}
148+
149+
$LogPrintExit2 info $0 ("A new LTE state " . ($Message) . " for " . \
150+
"LTE interface " . $IntName . ".") false;
151+
$SendNotification2 ({ origin=$0; \
152+
subject=([ $SymbolForNotification "sparkles" ] . "LTE state update"); \
153+
message=($Message); silent=true });
154+
:set ($SentLteStateUpdateNotification->$IntName) ($Message);
155+
}
156+
157+
:foreach Interface in=[ /interface/lte/find ] do={
158+
$CheckInterface $Interface;
159+
}

doc/check-lte-state-update.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
Notify on LTE state update
2+
==========================
3+
4+
[◀ Go back to main README](../README.md)
5+
6+
> ℹ️ **Info**: This script can not be used on its own but requires the base
7+
> installation. See [main README](../README.md) for details.
8+
9+
Description
10+
-----------
11+
12+
This script is run from scheduler periodically, checking for LTE state changes.
13+
14+
### Sample notification
15+
16+
![check-lte-state-update notification](check-lte-state-update.d/notification.avif)
17+
18+
Requirements and installation
19+
-----------------------------
20+
21+
Just install the script:
22+
23+
$ScriptInstallUpdate check-lte-state-update;
24+
25+
... and create a scheduler:
26+
27+
/system/scheduler/add interval=1h name=check-lte-state-update on-event="/system/script/run check-lte-state-update;" start-time=startup;
28+
29+
Configuration
30+
-------------
31+
32+
The configuration goes to `global-config-overlay`, this is the only parameter:
33+
34+
* `CheckLteStateUpdateBtestHost`: host to test for internet connectivity (btest server), leave empty to disable internet connectivity check
35+
* `CheckLteStateUpdateBtestUser`: user to test for internet connectivity (btest server)
36+
* `CheckLteStateUpdateBtestPassword`: password to test for internet connectivity (btest server)
37+
* `CheckLteStateUpdateIP`: check the IP address, set to false to disable IP address check (default true, enabled)
38+
* `CheckLteStateUpdatePrimaryBand`: check the primary band, set to true to enable primary band check, (default false, disabled)
39+
* `CheckLteStateUpdateCABand`: check the CA band, set to true to enable CA band check, (default false, disabled)
40+
41+
Also notification settings are required for
42+
[e-mail](mod/notification-email.md),
43+
[matrix](mod/notification-matrix.md) and/or
44+
[telegram](mod/notification-telegram.md).
45+
46+
See also
47+
--------
48+
49+
* [Notify on RouterOS update](check-routeros-update.md)
50+
* [Install LTE firmware upgrade](unattended-lte-firmware-upgrade.md)
51+
52+
---
53+
[◀ Go back to main README](../README.md)
54+
[▲ Go back to top](#top)

global-config

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,22 @@
147147
# add more here...
148148
};
149149

150+
# host to test for internet connectivity (btest server, disabled by default)
151+
:global CheckLteStateUpdateBtestHost;
152+
# :global CheckLteStateUpdateBtestHost "example.com"; # enable btest
153+
# user to test for internet connectivity (btest server)
154+
:global CheckLteStateUpdateBtestUser;
155+
# :global CheckLteStateUpdateBtestUser "mikrotik"; # enable btest
156+
# password to test for internet connectivity (btest server)
157+
:global CheckLteStateUpdateBtestPassword;
158+
# :global CheckLteStateUpdateBtestPassword "v3ry-s3cr3t"; # enable btest
159+
:global CheckLteStateUpdateIP true;
160+
# :global CheckLteStateUpdateIP false; # disable checking IP
161+
:global CheckLteStateUpdatePrimaryBand false;
162+
# :global CheckLteStateUpdatePrimaryBand true; # enable checking primary band
163+
:global CheckLteStateUpdateCABand false;
164+
# :global CheckLteStateUpdateCABand true; # enable checking CA band
165+
150166
# This is the address used to send gps data to.
151167
:global GpsTrackUrl "https://example.com/index.php";
152168

0 commit comments

Comments
 (0)