From 2e5f6557f2b533335bdc6f4a6f0885c83adf5915 Mon Sep 17 00:00:00 2001 From: junchao Date: Fri, 21 Oct 2022 13:43:11 +0800 Subject: [PATCH 1/2] [system-health] Led color shall be controlled by configuration when system is booting --- src/system-health/health_checker/config.py | 2 +- src/system-health/health_checker/manager.py | 23 +++++++++++++------ src/system-health/tests/test_system_health.py | 14 +++++------ 3 files changed, 24 insertions(+), 15 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 d58c69bececa..325a9989d180 100644 --- a/src/system-health/tests/test_system_health.py +++ b/src/system-health/tests/test_system_health.py @@ -498,10 +498,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') @@ -568,7 +568,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', @@ -659,10 +659,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' @@ -671,7 +671,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" @@ -689,7 +689,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" From efde7fb3d2a4ee370fcf541a84d342d71d3aea6b Mon Sep 17 00:00:00 2001 From: Junchao-Mellanox <57339448+Junchao-Mellanox@users.noreply.github.com> Date: Tue, 25 Oct 2022 21:05:35 +0800 Subject: [PATCH 2/2] Fix unit test issue --- src/system-health/tests/test_system_health.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/system-health/tests/test_system_health.py b/src/system-health/tests/test_system_health.py index 325a9989d180..4dd0462a89d0 100644 --- a/src/system-health/tests/test_system_health.py +++ b/src/system-health/tests/test_system_health.py @@ -425,7 +425,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'