|
| 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 | +} |
0 commit comments