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
|
|
|
|
2021-11-16 12:49:14 +00:00
|
|
|
:local TempToNum do={
|
|
|
|
:global CharacterReplace;
|
|
|
|
:local T [ :toarray [ $CharacterReplace $1 "." "," ] ];
|
|
|
|
:return ($T->0 * 10 + $T->1);
|
2019-08-27 08:35:53 +00:00
|
|
|
}
|
|
|
|
|
2022-05-10 12:43:04 +00:00
|
|
|
:if ([ :len [ /system/health/find ] ] = 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
|
|
|
}
|
|
|
|
|
2021-11-16 12:49:14 +00:00
|
|
|
:if ([ :typeof $CheckHealthLast ] != "array") do={
|
2022-07-13 09:31:44 +00:00
|
|
|
:set CheckHealthLast ({});
|
2021-11-16 12:49:14 +00:00
|
|
|
}
|
2020-10-16 20:51:51 +00:00
|
|
|
:if ([ :typeof $CheckHealthTemperatureNotified ] != "array") do={
|
2022-07-13 09:31:44 +00:00
|
|
|
:set CheckHealthTemperatureNotified ({});
|
2020-10-16 20:51:51 +00:00
|
|
|
}
|
|
|
|
|
2021-08-27 09:21:56 +00:00
|
|
|
$ScriptLock $0;
|
|
|
|
|
2022-05-10 12:43:04 +00:00
|
|
|
:foreach Voltage in=[ /system/health/find where type="V" ] do={
|
|
|
|
:local Name [ /system/health/get $Voltage name ];
|
|
|
|
:local Value [ /system/health/get $Voltage value ];
|
2021-11-16 12:49:14 +00:00
|
|
|
|
|
|
|
:if ([ :typeof ($CheckHealthLast->$Name) ] != "nothing") do={
|
|
|
|
:local NumCurr [ $TempToNum $Value ];
|
|
|
|
:local NumLast [ $TempToNum ($CheckHealthLast->$Name) ];
|
|
|
|
|
|
|
|
:if ($NumLast * (100 + $CheckHealthVoltagePercent) < $NumCurr * 100 || \
|
|
|
|
$NumLast * 100 > $NumCurr * (100 + $CheckHealthVoltagePercent)) do={
|
2021-04-27 19:43:22 +00:00
|
|
|
$SendNotification2 ({ origin=$0; \
|
2021-11-16 12:49:14 +00:00
|
|
|
subject=([ $SymbolForNotification ("high-voltage-sign,chart-" . [ $IfThenElse ($NumLast < \
|
|
|
|
$NumCurr) "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" . \
|
2021-11-16 12:49:14 +00:00
|
|
|
"old value: " . ($CheckHealthLast->$Name) . " V\n" . \
|
|
|
|
"new value: " . $Value . " V") });
|
2021-11-13 20:29:33 +00:00
|
|
|
} else={
|
2021-11-16 12:49:14 +00:00
|
|
|
:if ($NumCurr <= $CheckHealthVoltageLow && $NumLast > $CheckHealthVoltageLow) do={
|
2021-11-13 20:29:33 +00:00
|
|
|
$SendNotification2 ({ origin=$0; \
|
|
|
|
subject=([ $SymbolForNotification "high-voltage-sign,chart-decreasing" ] . "Health warning: Low " . $Name); \
|
2021-11-16 12:49:14 +00:00
|
|
|
message=("The " . $Name . " on " . $Identity . " dropped to " . $Value . " V below hard limit.") });
|
2021-11-13 20:29:33 +00:00
|
|
|
}
|
2021-11-16 12:49:14 +00:00
|
|
|
:if ($NumCurr > $CheckHealthVoltageLow && $NumLast <= $CheckHealthVoltageLow) do={
|
2021-11-13 20:29:33 +00:00
|
|
|
$SendNotification2 ({ origin=$0; \
|
|
|
|
subject=([ $SymbolForNotification "high-voltage-sign,chart-increasing" ] . "Health recovery: Low " . $Name); \
|
2021-11-16 12:49:14 +00:00
|
|
|
message=("The " . $Name . " on " . $Identity . " recovered to " . $Value . " V above hard limit.") });
|
2021-11-13 20:29:33 +00:00
|
|
|
}
|
2019-08-27 08:35:53 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-16 12:49:14 +00:00
|
|
|
:set ($CheckHealthLast->$Name) $Value;
|
2019-08-27 08:35:53 +00:00
|
|
|
}
|
|
|
|
|
2022-05-10 12:43:04 +00:00
|
|
|
:foreach PSU in=[ /system/health/find where name~"^psu.*-state\$" ] do={
|
|
|
|
:local Name [ /system/health/get $PSU name ];
|
|
|
|
:local Value [ /system/health/get $PSU value ];
|
2021-11-16 12:49:14 +00:00
|
|
|
|
|
|
|
:if ([ :typeof ($CheckHealthLast->$Name) ] != "nothing") do={
|
2020-10-17 21:05:08 +00:00
|
|
|
:if ($CheckHealthLast->$Name = "ok" && \
|
2021-11-16 12:49:14 +00:00
|
|
|
$Value != "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" && \
|
2021-11-16 12:49:14 +00:00
|
|
|
$Value = "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
|
|
|
}
|
2021-11-16 12:49:14 +00:00
|
|
|
:set ($CheckHealthLast->$Name) $Value;
|
2019-08-27 08:35:53 +00:00
|
|
|
}
|
|
|
|
|
2022-05-10 12:43:04 +00:00
|
|
|
:foreach Temperature in=[ /system/health/find where type="C" ] do={
|
|
|
|
:local Name [ /system/health/get $Temperature name ];
|
|
|
|
:local Value [ /system/health/get $Temperature value ];
|
2021-11-16 12:49:14 +00:00
|
|
|
|
|
|
|
:if ([ :typeof ($CheckHealthLast->$Name) ] != "nothing") do={
|
2020-10-17 21:05:08 +00:00
|
|
|
: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
|
|
|
}
|
2022-05-10 12:43:04 +00:00
|
|
|
:local Validate [ /system/health/get [ find where name=$Name ] value ];
|
2021-11-16 12:49:14 +00:00
|
|
|
:while ($Value != $Validate) do={
|
|
|
|
:set Value $Validate;
|
2022-05-10 12:43:04 +00:00
|
|
|
:set Validate [ /system/health/get [ find where name=$Name ] value ];
|
2020-11-19 20:23:27 +00:00
|
|
|
}
|
2021-11-16 12:49:14 +00:00
|
|
|
:if ($Value > $CheckHealthTemperature->$Name && \
|
2020-10-17 21:05:08 +00:00
|
|
|
$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: " . \
|
2021-11-16 12:49:14 +00:00
|
|
|
$Value . "\C2\B0" . "C") });
|
2020-10-17 21:05:08 +00:00
|
|
|
:set ($CheckHealthTemperatureNotified->$Name) true;
|
2019-08-27 08:35:53 +00:00
|
|
|
}
|
2021-11-16 12:49:14 +00:00
|
|
|
:if ($Value <= ($CheckHealthTemperature->$Name - $CheckHealthTemperatureDeviation) && \
|
2020-10-17 21:05:08 +00:00
|
|
|
$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: " . \
|
2021-11-16 12:49:14 +00:00
|
|
|
$Value . "\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
|
|
|
}
|
2021-11-16 12:49:14 +00:00
|
|
|
:set ($CheckHealthLast->$Name) $Value;
|
2019-08-27 08:35:53 +00:00
|
|
|
}
|