|
1 |
| -import json |
2 |
| -import os |
3 |
| - |
4 |
| -from sonic_py_common import device_info |
5 |
| - |
6 |
| - |
7 |
| -class Config(object): |
8 |
| - """ |
9 |
| - Manage configuration of system health. |
10 |
| - """ |
11 |
| - |
12 |
| - # Default system health check interval |
13 |
| - DEFAULT_INTERVAL = 60 |
14 |
| - |
15 |
| - # Default boot up timeout. When reboot system, system health will wait a few seconds before starting to work. |
16 |
| - DEFAULT_BOOTUP_TIMEOUT = 300 |
17 |
| - |
18 |
| - # Default LED configuration. Different platform has different LED capability. This configuration allow vendor to |
19 |
| - # override the default behavior. |
20 |
| - DEFAULT_LED_CONFIG = { |
21 |
| - 'fault': 'red', |
22 |
| - 'normal': 'green', |
23 |
| - 'booting': 'orange_blink' |
24 |
| - } |
25 |
| - |
26 |
| - # System health configuration file name |
27 |
| - CONFIG_FILE = 'system_health_monitoring_config.json' |
28 |
| - |
29 |
| - # Monit service configuration file path |
30 |
| - MONIT_CONFIG_FILE = '/etc/monit/monitrc' |
31 |
| - |
32 |
| - # Monit service start delay configuration entry |
33 |
| - MONIT_START_DELAY_CONFIG = 'with start delay' |
34 |
| - |
35 |
| - def __init__(self): |
36 |
| - """ |
37 |
| - Constructor. Initialize all configuration entry to default value in case there is no configuration file. |
38 |
| - """ |
39 |
| - self.platform_name = device_info.get_platform() |
40 |
| - self._config_file = os.path.join('/usr/share/sonic/device/', self.platform_name, Config.CONFIG_FILE) |
41 |
| - self._last_mtime = None |
42 |
| - self.config_data = None |
43 |
| - self.interval = Config.DEFAULT_INTERVAL |
44 |
| - self.ignore_services = None |
45 |
| - self.ignore_devices = None |
46 |
| - self.user_defined_checkers = None |
47 |
| - |
48 |
| - def config_file_exists(self): |
49 |
| - return os.path.exists(self._config_file) |
50 |
| - |
51 |
| - def load_config(self): |
52 |
| - """ |
53 |
| - Load the configuration file from disk. |
54 |
| - 1. If there is no configuration file, current config entries will reset to default value |
55 |
| - 2. Only read the configuration file is last_mtime changes for better performance |
56 |
| - 3. If there is any format issues in configuration file, current config entries will reset to default value |
57 |
| - :return: |
58 |
| - """ |
59 |
| - if not self.config_file_exists(): |
60 |
| - if self._last_mtime is not None: |
61 |
| - self._reset() |
62 |
| - return |
63 |
| - |
64 |
| - mtime = os.stat(self._config_file) |
65 |
| - if mtime != self._last_mtime: |
66 |
| - try: |
67 |
| - self._last_mtime = mtime |
68 |
| - with open(self._config_file, 'r') as f: |
69 |
| - self.config_data = json.load(f) |
70 |
| - |
71 |
| - self.interval = self.config_data.get('polling_interval', Config.DEFAULT_INTERVAL) |
72 |
| - self.ignore_services = self._get_list_data('services_to_ignore') |
73 |
| - self.ignore_devices = self._get_list_data('devices_to_ignore') |
74 |
| - self.user_defined_checkers = self._get_list_data('user_defined_checkers') |
75 |
| - except Exception as e: |
76 |
| - self._reset() |
77 |
| - |
78 |
| - def _reset(self): |
79 |
| - """ |
80 |
| - Reset current configuration entry to default value |
81 |
| - :return: |
82 |
| - """ |
83 |
| - self._last_mtime = None |
84 |
| - self.config_data = None |
85 |
| - self.interval = Config.DEFAULT_INTERVAL |
86 |
| - self.ignore_services = None |
87 |
| - self.ignore_devices = None |
88 |
| - self.user_defined_checkers = None |
89 |
| - |
90 |
| - def get_led_color(self, status): |
91 |
| - """ |
92 |
| - Get desired LED color according to the input status |
93 |
| - :param status: System health status |
94 |
| - :return: StringLED color |
95 |
| - """ |
96 |
| - if self.config_data and 'led_color' in self.config_data: |
97 |
| - if status in self.config_data['led_color']: |
98 |
| - return self.config_data['led_color'][status] |
99 |
| - |
100 |
| - return self.DEFAULT_LED_CONFIG[status] |
101 |
| - |
102 |
| - def get_bootup_timeout(self): |
103 |
| - """ |
104 |
| - Get boot up timeout from monit configuration file. |
105 |
| - 1. If monit configuration file does not exist, return default value |
106 |
| - 2. If there is any exception while parsing monit config, return default value |
107 |
| - :return: Integer timeout value |
108 |
| - """ |
109 |
| - if not os.path.exists(Config.MONIT_CONFIG_FILE): |
110 |
| - return self.DEFAULT_BOOTUP_TIMEOUT |
111 |
| - |
112 |
| - try: |
113 |
| - with open(Config.MONIT_CONFIG_FILE) as f: |
114 |
| - lines = f.readlines() |
115 |
| - for line in lines: |
116 |
| - if not line: |
117 |
| - continue |
118 |
| - |
119 |
| - line = line.strip() |
120 |
| - if not line: |
121 |
| - continue |
122 |
| - |
123 |
| - pos = line.find('#') |
124 |
| - if pos == 0: |
125 |
| - continue |
126 |
| - |
127 |
| - line = line[:pos] |
128 |
| - pos = line.find(Config.MONIT_START_DELAY_CONFIG) |
129 |
| - if pos != -1: |
130 |
| - return int(line[pos + len(Config.MONIT_START_DELAY_CONFIG):].strip()) |
131 |
| - except Exception: |
132 |
| - return self.DEFAULT_BOOTUP_TIMEOUT |
133 |
| - |
134 |
| - def _get_list_data(self, key): |
135 |
| - """ |
136 |
| - Get list type configuration data by key and remove duplicate element. |
137 |
| - :param key: Key of the configuration entry |
138 |
| - :return: A set of configuration data if key exists |
139 |
| - """ |
140 |
| - if key in self.config_data: |
141 |
| - data = self.config_data[key] |
142 |
| - if isinstance(data, list): |
143 |
| - return set(data) |
144 |
| - return None |
| 1 | +import json |
| 2 | +import os |
| 3 | + |
| 4 | +from sonic_py_common import device_info |
| 5 | + |
| 6 | + |
| 7 | +class Config(object): |
| 8 | + """ |
| 9 | + Manage configuration of system health. |
| 10 | + """ |
| 11 | + |
| 12 | + # Default system health check interval |
| 13 | + DEFAULT_INTERVAL = 60 |
| 14 | + |
| 15 | + # Default boot up timeout. When reboot system, system health will wait a few seconds before starting to work. |
| 16 | + DEFAULT_BOOTUP_TIMEOUT = 300 |
| 17 | + |
| 18 | + # Default LED configuration. Different platform has different LED capability. This configuration allow vendor to |
| 19 | + # override the default behavior. |
| 20 | + DEFAULT_LED_CONFIG = { |
| 21 | + 'fault': 'red', |
| 22 | + 'normal': 'green', |
| 23 | + 'booting': 'orange_blink' |
| 24 | + } |
| 25 | + |
| 26 | + # System health configuration file name |
| 27 | + CONFIG_FILE = 'system_health_monitoring_config.json' |
| 28 | + |
| 29 | + # Monit service configuration file path |
| 30 | + MONIT_CONFIG_FILE = '/etc/monit/monitrc' |
| 31 | + |
| 32 | + # Monit service start delay configuration entry |
| 33 | + MONIT_START_DELAY_CONFIG = 'with start delay' |
| 34 | + |
| 35 | + def __init__(self): |
| 36 | + """ |
| 37 | + Constructor. Initialize all configuration entry to default value in case there is no configuration file. |
| 38 | + """ |
| 39 | + self.platform_name = device_info.get_platform() |
| 40 | + self._config_file = os.path.join('/usr/share/sonic/device/', self.platform_name, Config.CONFIG_FILE) |
| 41 | + self._last_mtime = None |
| 42 | + self.config_data = None |
| 43 | + self.interval = Config.DEFAULT_INTERVAL |
| 44 | + self.ignore_services = None |
| 45 | + self.ignore_devices = None |
| 46 | + self.user_defined_checkers = None |
| 47 | + |
| 48 | + def config_file_exists(self): |
| 49 | + return os.path.exists(self._config_file) |
| 50 | + |
| 51 | + def load_config(self): |
| 52 | + """ |
| 53 | + Load the configuration file from disk. |
| 54 | + 1. If there is no configuration file, current config entries will reset to default value |
| 55 | + 2. Only read the configuration file is last_mtime changes for better performance |
| 56 | + 3. If there is any format issues in configuration file, current config entries will reset to default value |
| 57 | + :return: |
| 58 | + """ |
| 59 | + if not self.config_file_exists(): |
| 60 | + if self._last_mtime is not None: |
| 61 | + self._reset() |
| 62 | + return |
| 63 | + |
| 64 | + mtime = os.stat(self._config_file) |
| 65 | + if mtime != self._last_mtime: |
| 66 | + try: |
| 67 | + self._last_mtime = mtime |
| 68 | + with open(self._config_file, 'r') as f: |
| 69 | + self.config_data = json.load(f) |
| 70 | + |
| 71 | + self.interval = self.config_data.get('polling_interval', Config.DEFAULT_INTERVAL) |
| 72 | + self.ignore_services = self._get_list_data('services_to_ignore') |
| 73 | + self.ignore_devices = self._get_list_data('devices_to_ignore') |
| 74 | + self.user_defined_checkers = self._get_list_data('user_defined_checkers') |
| 75 | + except Exception as e: |
| 76 | + self._reset() |
| 77 | + |
| 78 | + def _reset(self): |
| 79 | + """ |
| 80 | + Reset current configuration entry to default value |
| 81 | + :return: |
| 82 | + """ |
| 83 | + self._last_mtime = None |
| 84 | + self.config_data = None |
| 85 | + self.interval = Config.DEFAULT_INTERVAL |
| 86 | + self.ignore_services = None |
| 87 | + self.ignore_devices = None |
| 88 | + self.user_defined_checkers = None |
| 89 | + |
| 90 | + def get_led_color(self, status): |
| 91 | + """ |
| 92 | + Get desired LED color according to the input status |
| 93 | + :param status: System health status |
| 94 | + :return: StringLED color |
| 95 | + """ |
| 96 | + if self.config_data and 'led_color' in self.config_data: |
| 97 | + if status in self.config_data['led_color']: |
| 98 | + return self.config_data['led_color'][status] |
| 99 | + |
| 100 | + return self.DEFAULT_LED_CONFIG[status] |
| 101 | + |
| 102 | + def get_bootup_timeout(self): |
| 103 | + """ |
| 104 | + Get boot up timeout from monit configuration file. |
| 105 | + 1. If monit configuration file does not exist, return default value |
| 106 | + 2. If there is any exception while parsing monit config, return default value |
| 107 | + :return: Integer timeout value |
| 108 | + """ |
| 109 | + if not os.path.exists(Config.MONIT_CONFIG_FILE): |
| 110 | + return self.DEFAULT_BOOTUP_TIMEOUT |
| 111 | + |
| 112 | + try: |
| 113 | + with open(Config.MONIT_CONFIG_FILE) as f: |
| 114 | + lines = f.readlines() |
| 115 | + for line in lines: |
| 116 | + if not line: |
| 117 | + continue |
| 118 | + |
| 119 | + line = line.strip() |
| 120 | + if not line: |
| 121 | + continue |
| 122 | + |
| 123 | + pos = line.find('#') |
| 124 | + if pos == 0: |
| 125 | + continue |
| 126 | + |
| 127 | + line = line[:pos] |
| 128 | + pos = line.find(Config.MONIT_START_DELAY_CONFIG) |
| 129 | + if pos != -1: |
| 130 | + return int(line[pos + len(Config.MONIT_START_DELAY_CONFIG):].strip()) |
| 131 | + except Exception: |
| 132 | + return self.DEFAULT_BOOTUP_TIMEOUT |
| 133 | + |
| 134 | + def _get_list_data(self, key): |
| 135 | + """ |
| 136 | + Get list type configuration data by key and remove duplicate element. |
| 137 | + :param key: Key of the configuration entry |
| 138 | + :return: A set of configuration data if key exists |
| 139 | + """ |
| 140 | + if key in self.config_data: |
| 141 | + data = self.config_data[key] |
| 142 | + if isinstance(data, list): |
| 143 | + return set(data) |
| 144 | + return None |
0 commit comments