global-functions: introduce and use $SymbolForNotification

This commit is contained in:
Christian Hesse 2020-07-17 08:07:12 +02:00
parent 0e7b2d3ac7
commit 075859c898
9 changed files with 35 additions and 18 deletions

View file

@ -13,7 +13,7 @@
:global LogPrintExit;
:global SendNotification;
:global SymbolByUnicodeName;
:global SymbolForNotification;
:local FormatVoltage do={
:local Voltage [ :tonum $1 ];
@ -27,7 +27,7 @@
[ :typeof ($CheckHealthCurrent->$Voltage) ] = "num") do={
:if ($CheckHealthLast->$Voltage * (100 + $CheckHealthVoltagePercent) < $CheckHealthCurrent->$Voltage * 100 || \
$CheckHealthLast->$Voltage * 100 > $CheckHealthCurrent->$Voltage * (100 + $CheckHealthVoltagePercent)) do={
$SendNotification ([ $SymbolByUnicodeName "high-voltage-sign" ] . " Health warning: " . $Voltage) \
$SendNotification ([ $SymbolForNotification "high-voltage-sign" ] . "Health warning: " . $Voltage) \
("The " . $Voltage . " on " . $Identity . " jumped more than " . $CheckHealthVoltagePercent . "%.\n\n" . \
"old value: " . [ $FormatVoltage ($CheckHealthLast->$Voltage) ] . "\n" . \
"new value: " . [ $FormatVoltage ($CheckHealthCurrent->$Voltage) ]);
@ -40,12 +40,12 @@
[ :typeof ($CheckHealthCurrent->($PSU . "-state")) ] = "str") do={
:if ($CheckHealthLast->($PSU . "-state") = "ok" && \
$CheckHealthCurrent->($PSU . "-state") != "ok") do={
$SendNotification ([ $SymbolByUnicodeName "cross-mark" ] . " Health warning: " . $PSU . " state") \
$SendNotification ([ $SymbolForNotification "cross-mark" ] . "Health warning: " . $PSU . " state") \
("The power supply unit '" . $PSU . "' on " . $Identity . " failed!");
}
:if ($CheckHealthLast->($PSU . "-state") != "ok" && \
$CheckHealthCurrent->($PSU . "-state") = "ok") do={
$SendNotification ([ $SymbolByUnicodeName "white-heavy-check-mark" ] . " Health recovery: " . $PSU . " state") \
$SendNotification ([ $SymbolForNotification "white-heavy-check-mark" ] . "Health recovery: " . $PSU . " state") \
("The power supply unit '" . $PSU . "' on " . $Identity . " recovered!");
}
}
@ -60,13 +60,13 @@
}
:if ($CheckHealthLast->$Temperature <= $CheckHealthTemperature->$Temperature && \
$CheckHealthCurrent->$Temperature > $CheckHealthTemperature->$Temperature) do={
$SendNotification ([ $SymbolByUnicodeName "fire" ] . " Health warning: " . $Temperature) \
$SendNotification ([ $SymbolForNotification "fire" ] . "Health warning: " . $Temperature) \
("The " . $Temperature . " on " . $Identity . " is above threshold: " . \
$CheckHealthCurrent->$Temperature . "\C2\B0" . "C");
}
:if ($CheckHealthLast->$Temperature > $CheckHealthTemperature->$Temperature && \
$CheckHealthCurrent->$Temperature <= $CheckHealthTemperature->$Temperature) do={
$SendNotification ([ $SymbolByUnicodeName "white-heavy-check-mark" ] . " Health recovery: " . $Temperature) \
$SendNotification ([ $SymbolForNotification "white-heavy-check-mark" ] . "Health recovery: " . $Temperature) \
("The " . $Temperature . " on " . $Identity . " dropped below threshold: " . \
$CheckHealthCurrent->$Temperature . "\C2\B0" . "C");
}

View file

@ -15,7 +15,7 @@
:global LogPrintExit;
:global ScriptFromTerminal;
:global SendNotification;
:global SymbolByUnicodeName;
:global SymbolForNotification;
:global VersionToNum;
:local DoUpdate do={
@ -89,7 +89,7 @@
$Update->"latest-version" . ".") true;
}
$SendNotification ([ $SymbolByUnicodeName "sparkles" ] . " RouterOS update") \
$SendNotification ([ $SymbolForNotification "sparkles" ] . "RouterOS update") \
("A new RouterOS version " . ($Update->"latest-version") . \
" is available for " . $Identity . ".\n\n" . \
[ $DeviceInfo ] . "\n\n" . \

View file

@ -8,7 +8,7 @@
# Make sure all configuration properties are up to date and this
# value is in sync with value in script 'global-functions'!
:global GlobalConfigVersion 23;
:global GlobalConfigVersion 24;
# This is used for DNS and backup file.
:global Domain "example.com";
@ -28,6 +28,9 @@
#:global TelegramTokenId "123456:ABCDEF-GHI";
#:global TelegramChatId "12345678";
# Toggle this to disable symbols in notifications.
:global NotificationsWithSymbols true;
# This defines what backups to generate and what password to use.
:global BackupSendBinary false;
:global BackupSendExport true;

View file

@ -9,7 +9,7 @@
# Make sure all configuration properties are up to date and this
# value is in sync with value in script 'global-functions'!
# Comment or remove to disable change notifications.
:global GlobalConfigVersion 23;
:global GlobalConfigVersion 24;
# Copy configuration from global-config here and modify it.

View file

@ -27,4 +27,5 @@
21="Added support for installing patch updates automatically by 'check-routeros-update'";
22="Dropped '\$ScriptUpdatesIgnore' from global configuration, auto-migrating to ignore flag in comment"
23="Added 'log-forward' with configurable filter, which replaces 'early-errors'";
24="Made symbols in notifications configurable.";
};

View file

@ -8,7 +8,7 @@
# https://git.eworm.de/cgit/routeros-scripts/about/
# expected configuration version
:global ExpectedConfigVersion 23;
:global ExpectedConfigVersion 24;
# global variables not to be changed by user
:global GlobalFunctionsReady false;
@ -41,6 +41,7 @@
:global SendNotification;
:global SendTelegram;
:global SymbolByUnicodeName;
:global SymbolForNotification;
:global TimeIsSync;
:global UrlEncode;
:global VersionToNum;
@ -747,6 +748,18 @@
:return ($Symbols->$1);
}
# return symbol for notification
:set SymbolForNotification do={
:global NotificationsWithSymbols;
:global SymbolByUnicodeName;
:if ($NotificationsWithSymbols != true) do={
:return "";
}
:return ([ $SymbolByUnicodeName $1 ] . " ");
}
# check if system time is sync
:set TimeIsSync do={
:if ([ / system ntp client get enabled ] = true) do={

View file

@ -13,7 +13,7 @@
:global LogPrintExit;
:global MailServerIsUp;
:global SendNotification;
:global SymbolByUnicodeName;
:global SymbolForNotification;
:global WaitFullyConnected;
$WaitFullyConnected;
@ -40,7 +40,7 @@ $WaitFullyConnected;
}
:if ($Count > 0) do={
$SendNotification ([ $SymbolByUnicodeName "warning-sign" ] . " Log Forwarding") \
$SendNotification ([ $SymbolForNotification "warning-sign" ] . "Log Forwarding") \
("The log on " . $Identity . " contains these " . $Count . " messages after " . \
[ / system resource get uptime ] . " uptime.\n" . $Messages);

View file

@ -11,7 +11,7 @@
:global ParseKeyValueStore;
:global LogPrintExit;
:global SendNotification;
:global SymbolByUnicodeName;
:global SymbolForNotification;
:if ([ :typeof $NetwatchNotify ] = "nothing") do={
:set NetwatchNotify [ :toarray "" ];
@ -32,7 +32,7 @@
:local Count ($Metric->"count");
:set ($Metric->"count") 0;
:if ($Metric->"notified" = true) do={
$SendNotification ([ $SymbolByUnicodeName "white-heavy-check-mark" ] . " Netwatch Notify: " . $HostName . " up") \
$SendNotification ([ $SymbolForNotification "white-heavy-check-mark" ] . "Netwatch Notify: " . $HostName . " up") \
("Host " . $HostName . " (" . $HostVal->"host" . ") is up since " . $HostVal->"since" . ".\n" . \
"It was down for " . $Count . " checks.");
:if ([ :typeof ($HostInfo->"up-hook") ] = "str") do={
@ -46,7 +46,7 @@
$LogPrintExit info ("Host " . $HostName . " (" . $HostVal->"host" . ") is down for " . \
$Metric->"count" . " checks.") false;
:if ($Metric->"count" >= 5 && $Metric->"notified" != true) do={
$SendNotification ([ $SymbolByUnicodeName "cross-mark" ] . " Netwatch Notify: " . $HostName . " down") \
$SendNotification ([ $SymbolForNotification "cross-mark" ] . "Netwatch Notify: " . $HostName . " down") \
("Host " . $HostName . " (" . $HostVal->"host" . ") is down since " . $HostVal->"since" . ".");
:set ($Metric->"notified") true;
:if ([ :typeof ($HostInfo->"down-hook") ] = "str") do={

View file

@ -11,7 +11,7 @@
:global LogPrintExit;
:global MailServerIsUp;
:global SendNotification;
:global SymbolByUnicodeName;
:global SymbolForNotification;
:global WaitFullyConnected;
:if ([ / tool sms get receive-enabled ] = false) do={
@ -47,7 +47,7 @@ $WaitFullyConnected;
}
:if ([ :len $Messages ] > 0) do={
$SendNotification ([ $SymbolByUnicodeName "incoming-envelope" ] . " SMS Forwarding from " . $Phone) \
$SendNotification ([ $SymbolForNotification "incoming-envelope" ] . "SMS Forwarding from " . $Phone) \
("These message(s) were received by " . $Identity . \
" from " . $Phone . ":" . $Messages);
:foreach Sms in=$Delete do={