From ffa974c7f4e404f270acecb13a05440bbbb175fa Mon Sep 17 00:00:00 2001 From: Junchao-Mellanox <57339448+Junchao-Mellanox@users.noreply.github.com> Date: Thu, 1 Dec 2022 10:38:50 +0800 Subject: [PATCH] [system-health] Led color shall be controlled by configuration when system is booting (#12487) * [system-health] Led color shall be controlled by configuration when system is booting * Fix unit test issue --- src/system-health/health_checker/config.py | 2 +- src/system-health/health_checker/manager.py | 23 +++++++++++++------ src/system-health/tests/test_system_health.py | 16 ++++++------- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/system-health/health_checker/config.py b/src/system-health/health_checker/config.py index 3d287d778377..63af22db4ad0 100644 --- a/src/system-health/health_checker/config.py +++ b/src/system-health/health_checker/config.py @@ -20,7 +20,7 @@ class Config(object): DEFAULT_LED_CONFIG = { 'fault': 'red', 'normal': 'green', - 'booting': 'orange_blink' + 'booting': 'red' } # System health configuration file name diff --git a/src/system-health/health_checker/manager.py b/src/system-health/health_checker/manager.py index b1c6dd15bec3..927086eaea02 100644 --- a/src/system-health/health_checker/manager.py +++ b/src/system-health/health_checker/manager.py @@ -3,14 +3,13 @@ from .service_checker import ServiceChecker from .hardware_checker import HardwareChecker from .user_defined_checker import UserDefinedChecker +from . import utils class HealthCheckerManager(object): """ Manage all system health checkers and system health configuration. """ - boot_timeout = None - def __init__(self): self._checkers = [] self.config = Config() @@ -42,9 +41,7 @@ def check(self, chassis): checker = UserDefinedChecker(udc) self._do_check(checker, stats) - led_status = 'normal' if HealthChecker.summary == HealthChecker.STATUS_OK else 'fault' - self._set_system_led(chassis, self.config, led_status) - + self._set_system_led(chassis) return stats def _do_check(self, checker, stats): @@ -75,10 +72,22 @@ def _do_check(self, checker, stats): else: stats['Internal'].update(entry) - def _set_system_led(self, chassis, config, status): + def _set_system_led(self, chassis): try: - chassis.set_status_led(config.get_led_color(status)) + chassis.set_status_led(self._get_led_target_color()) except NotImplementedError: print('chassis.set_status_led is not implemented') except Exception as e: print('Failed to set system led due to - {}'.format(repr(e))) + + def _get_led_target_color(self): + """Get target LED color according to health status and system uptime + + Returns: + str: LED color + """ + if HealthChecker.summary == HealthChecker.STATUS_OK: + return self.config.get_led_color('normal') + else: + uptime = utils.get_uptime() + return self.config.get_led_color('booting') if uptime < self.config.get_bootup_timeout() else self.config.get_led_color('fault') diff --git a/src/system-health/tests/test_system_health.py b/src/system-health/tests/test_system_health.py index 73beb99cae83..0196efc177a8 100644 --- a/src/system-health/tests/test_system_health.py +++ b/src/system-health/tests/test_system_health.py @@ -457,7 +457,7 @@ def test_config(): assert config.get_led_color('fault') == 'red' assert config.get_led_color('normal') == 'green' - assert config.get_led_color('booting') == 'orange_blink' + assert config.get_led_color('booting') == 'red' config._last_mtime = 1 config._config_file = 'notExistFile' @@ -530,10 +530,10 @@ def test_manager(mock_hw_info, mock_service_info, mock_udc_info): assert stat['Internal']['UserDefinedChecker - some check']['status'] == 'Not OK' chassis.set_status_led.side_effect = NotImplementedError() - manager._set_system_led(chassis, manager.config, 'normal') + manager._set_system_led(chassis) chassis.set_status_led.side_effect = RuntimeError() - manager._set_system_led(chassis, manager.config, 'normal') + manager._set_system_led(chassis) def test_utils(): output = utils.run_command(['some', 'invalid', 'command']) @@ -600,7 +600,7 @@ def test_get_app_ready_status(mock_config_db, mock_run, mock_docker_client): 'has_global_scope': 'True', 'has_per_asic_scope': 'False', 'check_up_status': 'True' - }, + }, 'snmp': { 'state': 'enabled', 'has_global_scope': 'True', @@ -691,10 +691,10 @@ def test_get_all_system_status_not_ok(): result = sysmon.get_all_system_status() print("result:{}".format(result)) assert result == 'DOWN' - + def test_post_unit_status(): sysmon = Sysmonitor() - sysmon.post_unit_status("mock_bgp", 'OK', 'Down', 'mock reason', '-') + sysmon.post_unit_status("mock_bgp", 'OK', 'Down', 'mock reason', '-') result = swsscommon.SonicV2Connector.get_all(MockConnector, 0, 'ALL_SERVICE_STATUS|mock_bgp') print(result) assert result['service_status'] == 'OK' @@ -703,7 +703,7 @@ def test_post_unit_status(): def test_post_system_status(): sysmon = Sysmonitor() - sysmon.post_system_status("UP") + sysmon.post_system_status("UP") result = swsscommon.SonicV2Connector.get(MockConnector, 0, "SYSTEM_READY|SYSTEM_STATE", 'Status') print("post system status result:{}".format(result)) assert result == "UP" @@ -721,7 +721,7 @@ def test_publish_system_status(): @patch('health_checker.sysmonitor.Sysmonitor.publish_system_status', test_publish_system_status()) def test_update_system_status(): sysmon = Sysmonitor() - sysmon.update_system_status() + sysmon.update_system_status() result = swsscommon.SonicV2Connector.get(MockConnector, 0, "SYSTEM_READY|SYSTEM_STATE", 'Status') assert result == "UP"