2020-09-18 09:00:27 +00:00
|
|
|
#!rsc by RouterOS
|
2019-08-27 08:35:53 +00:00
|
|
|
# RouterOS script: check-health
|
2021-01-01 20:33:52 +00:00
|
|
|
# Copyright (c) 2019-2021 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
|
|
|
|
|
|
|
:global CheckHealthLast;
|
|
|
|
:global CheckHealthTemperature;
|
2020-10-16 20:51:51 +00:00
|
|
|
:global CheckHealthTemperatureDeviation;
|
|
|
|
:global CheckHealthTemperatureNotified;
|
2019-10-14 17:06:21 +00:00
|
|
|
:global CheckHealthVoltagePercent;
|
2019-08-27 08:35:53 +00:00
|
|
|
:global Identity;
|
|
|
|
|
2020-04-03 15:29:08 +00:00
|
|
|
:global LogPrintExit;
|
2019-08-27 08:35:53 +00:00
|
|
|
:global SendNotification;
|
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={
|
|
|
|
$LogPrintExit error ("Your device does not provide any health values.") true;
|
|
|
|
}
|
|
|
|
|
2020-10-16 20:51:51 +00:00
|
|
|
:if ([ :typeof $CheckHealthTemperatureNotified ] != "array") do={
|
|
|
|
:set CheckHealthTemperatureNotified [ :toarray "" ];
|
|
|
|
}
|
|
|
|
|
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={
|
|
|
|
$SendNotification ([ $SymbolForNotification "high-voltage-sign" ] . "Health warning: " . $Name) \
|
|
|
|
("The " . $Name . " on " . $Identity . " jumped more than " . $CheckHealthVoltagePercent . "%.\n\n" . \
|
|
|
|
"old value: " . [ $FormatVoltage ($CheckHealthLast->$Name) ] . "\n" . \
|
|
|
|
"new value: " . [ $FormatVoltage $Voltage ]);
|
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={
|
|
|
|
$SendNotification ([ $SymbolForNotification "cross-mark" ] . "Health warning: " . $Name) \
|
|
|
|
("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={
|
|
|
|
$SendNotification ([ $SymbolForNotification "white-heavy-check-mark" ] . "Health recovery: " . $Name) \
|
|
|
|
("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={
|
2020-11-13 21:17:55 +00:00
|
|
|
$LogPrintExit info ("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={
|
|
|
|
$SendNotification ([ $SymbolForNotification "fire" ] . "Health warning: " . $Name) \
|
|
|
|
("The " . $Name . " on " . $Identity . " is above threshold: " . \
|
|
|
|
$Temperature . "\C2\B0" . "C");
|
|
|
|
: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={
|
|
|
|
$SendNotification ([ $SymbolForNotification "white-heavy-check-mark" ] . "Health recovery: " . $Name) \
|
|
|
|
("The " . $Name . " on " . $Identity . " dropped below threshold: " . \
|
|
|
|
$Temperature . "\C2\B0" . "C");
|
|
|
|
:set ($CheckHealthTemperatureNotified->$Name) false;
|
2019-08-27 13:20:11 +00:00
|
|
|
}
|
2019-08-27 08:35:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
:set CheckHealthLast $CheckHealthCurrent;
|