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