From 95e3b594c597dbacc5a69e4f04697192ec6cd077 Mon Sep 17 00:00:00 2001 From: Benjamin Walker Date: Mon, 14 Oct 2024 16:38:23 +1000 Subject: [PATCH] Mutes should only lower level #196 --- classes/check/failingtaskcheck.php | 2 +- classes/checker.php | 36 ++++++++++++++++++++---------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/classes/check/failingtaskcheck.php b/classes/check/failingtaskcheck.php index ab37862..97683ea 100644 --- a/classes/check/failingtaskcheck.php +++ b/classes/check/failingtaskcheck.php @@ -123,7 +123,7 @@ public function get_ref(): string { * @return array of failing tasks */ public static function get_failing_tasks(): array { - GLOBAL $DB; + global $DB; $tasks = []; // Instead of using task API here, we read directly from the database. diff --git a/classes/checker.php b/classes/checker.php index 82cf9ef..a35713c 100644 --- a/classes/checker.php +++ b/classes/checker.php @@ -39,6 +39,17 @@ class checker { 3 => "UNKNOWN", ]; + /** @var array Map check result to nagios level **/ + public const RESULT_MAPPING = [ + result::OK => resultmessage::LEVEL_OK, + result::INFO => resultmessage::LEVEL_OK, + result::NA => resultmessage::LEVEL_OK, + result::WARNING => resultmessage::LEVEL_WARN, + result::CRITICAL => resultmessage::LEVEL_CRITICAL, + result::ERROR => resultmessage::LEVEL_CRITICAL, + result::UNKNOWN => resultmessage::LEVEL_UNKNOWN, + ]; + /** * Returns an array of check API messages. * If exceptions are thrown, they are caught and returned as result messages as well. @@ -133,15 +144,7 @@ private static function process_check_and_get_result(check $check): resultmessag $checkresult = self::get_overridden_result($check); // Map check result to nagios level. - $map = [ - result::OK => resultmessage::LEVEL_OK, - result::INFO => resultmessage::LEVEL_OK, - result::NA => resultmessage::LEVEL_OK, - result::WARNING => resultmessage::LEVEL_WARN, - result::CRITICAL => resultmessage::LEVEL_CRITICAL, - result::ERROR => resultmessage::LEVEL_CRITICAL, - result::UNKNOWN => resultmessage::LEVEL_UNKNOWN, - ]; + $map = self::RESULT_MAPPING; // Get the level, or default to unknown. $status = $checkresult->get_status(); @@ -318,9 +321,18 @@ public static function get_overridden_result(check $check): result { $result = $check->get_result(); $override = \tool_heartbeat\object\override::get_active_override($ref); - if (isset($override)) { - return new result($override->get('override'), $result->get_summary(), $result->get_details()); + if (!isset($override)) { + return $result; } - return $result; + + // Mutes should only be able to lower the level. + $map = self::RESULT_MAPPING; + $status = $override->get('override'); + if ($map[$status] >= $map[$result->get_status()]) { + // Don't automatically resolve the override as some checks may fail sporadically. + return $result; + } + + return new result($status, $result->get_summary(), $result->get_details()); } }