2020-09-18 09:00:27 +00:00
|
|
|
#!rsc by RouterOS
|
2019-08-27 08:35:53 +00:00
|
|
|
# RouterOS script: check-health
|
2022-01-01 20:38:15 +00:00
|
|
|
# Copyright (c) 2019-2022 Christian Hesse <mail@eworm.de>
|
2020-06-19 20:17:42 +00:00
|
|
|
# https://git.eworm.de/cgit/routeros-scripts/about/COPYING.md
|
2019-08-27 08:35:53 +00:00
|
|
|
#
|
|
|
|
# check for RouterOS health state
|
2020-03-27 20:41:40 +00:00
|
|
|
# https://git.eworm.de/cgit/routeros-scripts/about/doc/check-health.md
|
2019-08-27 08:35:53 +00:00
|
|
|
|
2021-02-22 14:14:10 +00:00
|
|
|
:local 0 "check-health";
|
2021-02-18 13:52:47 +00:00
|
|
|
:global GlobalFunctionsReady;
|
|
|
|
:while ($GlobalFunctionsReady != true) do={ :delay 500ms; }
|
|
|
|
|
2019-08-27 08:35:53 +00:00
|
|
|
:global CheckHealthLast;
|
|
|
|
:global CheckHealthTemperature;
|
2020-10-16 20:51:51 +00:00
|
|
|
:global CheckHealthTemperatureDeviation;
|
|
|
|
:global CheckHealthTemperatureNotified;
|
2021-11-13 20:29:33 +00:00
|
|
|
:global CheckHealthVoltageLow;
|
2019-10-14 17:06:21 +00:00
|
|
|
:global CheckHealthVoltagePercent;
|
2019-08-27 08:35:53 +00:00
|
|
|
:global Identity;
|
|
|
|
|
2021-11-16 13:53:25 +00:00
|
|
|
:global IfThenElse;
|
2021-02-22 14:14:10 +00:00
|
|
|
:global LogPrintExit2;
|
2021-08-27 09:21:56 +00:00
|
|
|
:global ScriptLock;
|
2021-04-27 18:42:42 +00:00
|
|
|
:global SendNotification2;
|
2020-07-17 06:07:12 +00:00
|
|
|
:global SymbolForNotification;
|
2019-08-27 08:35:53 +00:00
|
|
|
|
|
|
|
:local FormatVoltage do={
|
|
|
|
:local Voltage [ :tonum $1 ];
|
2020-11-19 19:24:09 +00:00
|
|
|
:return (($Voltage / 10) . "." . [ :pick $Voltage ([ :len $Voltage ] - 1) ] . "V");
|
2019-08-27 08:35:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
:local CheckHealthCurrent [ / system health get ];
|
|
|
|
|
2020-10-16 19:35:44 +00:00
|
|
|
:if ([ :len $CheckHealthCurrent ] = 0) do={
|
2021-02-22 14:14:10 +00:00
|
|
|
$LogPrintExit2 error $0 ("Your device does not provide any health values.") true;
|
2020-10-16 19:35:44 +00:00
|
|
|
}
|
|
|
|
|
2020-10-16 20:51:51 +00:00
|
|
|
:if ([ :typeof $CheckHealthTemperatureNotified ] != "array") do={
|
|
|
|
:set CheckHealthTemperatureNotified [ :toarray "" ];
|
|
|
|
}
|
|
|
|
|
2021-08-27 09:21:56 +00:00
|
|
|
$ScriptLock $0;
|
|
|
|
|
2020-10-17 21:05:08 +00:00
|
|
|
:foreach Name,Voltage in=$CheckHealthCurrent do={
|
|
|
|
:if ($Name ~ "(battery|voltage)" && \
|
|
|
|
[ :typeof ($CheckHealthLast->$Name) ] = "num" && \
|
|
|
|
[ :typeof $Voltage ] = "num") do={
|
|
|
|
:if ($CheckHealthLast->$Name * (100 + $CheckHealthVoltagePercent) < $Voltage * 100 || \
|
|
|
|
$CheckHealthLast->$Name * 100 > $Voltage * (100 + $CheckHealthVoltagePercent)) do={
|
2021-04-27 19:43:22 +00:00
|
|
|
$SendNotification2 ({ origin=$0; \
|
2021-11-16 13:53:25 +00:00
|
|
|
subject=([ $SymbolForNotification ("high-voltage-sign,chart-" . [ $IfThenElse ($CheckHealthLast->$Name < \
|
|
|
|
$Voltage) "in" "de" ] . "creasing") ] . "Health warning: " . $Name); \
|
2021-04-27 18:42:42 +00:00
|
|
|
message=("The " . $Name . " on " . $Identity . " jumped more than " . $CheckHealthVoltagePercent . "%.\n\n" . \
|
2020-10-17 21:05:08 +00:00
|
|
|
"old value: " . [ $FormatVoltage ($CheckHealthLast->$Name) ] . "\n" . \
|
2021-04-27 18:42:42 +00:00
|
|
|
"new value: " . [ $FormatVoltage $Voltage ]) });
|
2021-11-13 20:29:33 +00:00
|
|
|
} else={
|
|
|
|
:if ($Voltage <= $CheckHealthVoltageLow && $CheckHealthLast->$Name > $CheckHealthVoltageLow) do={
|
|
|
|
$SendNotification2 ({ origin=$0; \
|
|
|
|
subject=([ $SymbolForNotification "high-voltage-sign,chart-decreasing" ] . "Health warning: Low " . $Name); \
|
|
|
|
message=("The " . $Name . " on " . $Identity . " dropped to " . [ $FormatVoltage $Voltage ] . " below hard limit.") });
|
|
|
|
}
|
|
|
|
:if ($Voltage > $CheckHealthVoltageLow && $CheckHealthLast->$Name <= $CheckHealthVoltageLow) do={
|
|
|
|
$SendNotification2 ({ origin=$0; \
|
|
|
|
subject=([ $SymbolForNotification "high-voltage-sign,chart-increasing" ] . "Health recovery: Low " . $Name); \
|
|
|
|
message=("The " . $Name . " on " . $Identity . " recovered to " . [ $FormatVoltage $Voltage ] . " above hard limit.") });
|
|
|
|
}
|
2019-08-27 08:35:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-17 21:05:08 +00:00
|
|
|
:foreach Name,PSU in=$CheckHealthCurrent do={
|
|
|
|
:if ($Name ~ "psu.*-state" && \
|
|
|
|
[ :typeof ($CheckHealthLast->$Name) ] = "str" && \
|
|
|
|
[ :typeof $PSU ] = "str") do={
|
|
|
|
:if ($CheckHealthLast->$Name = "ok" && \
|
|
|
|
$PSU != "ok") do={
|
2021-04-27 19:43:22 +00:00
|
|
|
$SendNotification2 ({ origin=$0; \
|
|
|
|
subject=([ $SymbolForNotification "cross-mark" ] . "Health warning: " . $Name); \
|
2021-04-27 18:42:42 +00:00
|
|
|
message=("The power supply unit '" . $Name . "' on " . $Identity . " failed!") });
|
2019-08-28 18:48:32 +00:00
|
|
|
}
|
2020-10-17 21:05:08 +00:00
|
|
|
:if ($CheckHealthLast->$Name != "ok" && \
|
|
|
|
$PSU = "ok") do={
|
2021-04-27 19:43:22 +00:00
|
|
|
$SendNotification2 ({ origin=$0; \
|
|
|
|
subject=([ $SymbolForNotification "white-heavy-check-mark" ] . "Health recovery: " . $Name); \
|
2021-04-27 18:42:42 +00:00
|
|
|
message=("The power supply unit '" . $Name . "' on " . $Identity . " recovered!") });
|
2019-08-28 18:48:32 +00:00
|
|
|
}
|
2019-08-27 13:20:11 +00:00
|
|
|
}
|
2019-08-27 08:35:53 +00:00
|
|
|
}
|
|
|
|
|
2020-10-17 21:05:08 +00:00
|
|
|
:foreach Name,Temperature in=$CheckHealthCurrent do={
|
|
|
|
:if ($Name ~ "temperature" && \
|
|
|
|
[ :typeof $Temperature ] = "num") do={
|
|
|
|
:if ([ :typeof ($CheckHealthTemperature->$Name) ] != "num" ) do={
|
2021-02-22 14:14:10 +00:00
|
|
|
$LogPrintExit2 info $0 ("No threshold given for " . $Name . ", assuming 50C.") false;
|
2020-10-17 21:05:08 +00:00
|
|
|
:set ($CheckHealthTemperature->$Name) 50;
|
2019-08-27 08:35:53 +00:00
|
|
|
}
|
2020-11-24 12:53:15 +00:00
|
|
|
:local Validate [ / system health get $Name ];
|
|
|
|
:while ($Temperature != $Validate) do={
|
|
|
|
:set Temperature $Validate;
|
|
|
|
:set Validate [ / system health get $Name ];
|
2020-11-19 20:23:27 +00:00
|
|
|
}
|
2020-10-17 21:05:08 +00:00
|
|
|
:if ($Temperature > $CheckHealthTemperature->$Name && \
|
|
|
|
$CheckHealthTemperatureNotified->$Name != true) do={
|
2021-04-27 19:43:22 +00:00
|
|
|
$SendNotification2 ({ origin=$0; \
|
|
|
|
subject=([ $SymbolForNotification "fire" ] . "Health warning: " . $Name); \
|
2021-04-27 18:42:42 +00:00
|
|
|
message=("The " . $Name . " on " . $Identity . " is above threshold: " . \
|
|
|
|
$Temperature . "\C2\B0" . "C") });
|
2020-10-17 21:05:08 +00:00
|
|
|
:set ($CheckHealthTemperatureNotified->$Name) true;
|
2019-08-27 08:35:53 +00:00
|
|
|
}
|
2020-10-17 21:05:08 +00:00
|
|
|
:if ($Temperature <= ($CheckHealthTemperature->$Name - $CheckHealthTemperatureDeviation) && \
|
|
|
|
$CheckHealthTemperatureNotified->$Name = true) do={
|
2021-04-27 19:43:22 +00:00
|
|
|
$SendNotification2 ({ origin=$0; \
|
|
|
|
subject=([ $SymbolForNotification "white-heavy-check-mark" ] . "Health recovery: " . $Name); \
|
2021-04-27 18:42:42 +00:00
|
|
|
message=("The " . $Name . " on " . $Identity . " dropped below threshold: " . \
|
|
|
|
$Temperature . "\C2\B0" . "C") });
|
2020-10-17 21:05:08 +00:00
|
|
|
:set ($CheckHealthTemperatureNotified->$Name) false;
|
2019-08-27 13:20:11 +00:00
|
|
|
}
|
2019-08-27 08:35:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
:set CheckHealthLast $CheckHealthCurrent;
|