Skip to content

Commit 566ea4f

Browse files
authored
[system-health] Convert to Python 3 (#5886)
- Convert system-health scripts to Python 3 - Build and install system-health as a Python 3 wheel - Also convert newlines from DOS to UNIX
1 parent 62662ac commit 566ea4f

15 files changed

+795
-796
lines changed

files/build_templates/sonic_debian_extension.j2

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,6 @@ sudo https_proxy=$https_proxy LANG=C chroot $FILESYSTEM_ROOT pip2 install $PLATF
201201
sudo rm -rf $FILESYSTEM_ROOT/$PLATFORM_PDDF_COMMON_PY2_WHEEL_NAME
202202
{% endif %}
203203

204-
# Install system-health Python 2 package
205-
SYSTEM_HEALTH_PY2_WHEEL_NAME=$(basename {{system_health_py2_wheel_path}})
206-
sudo cp {{system_health_py2_wheel_path}} $FILESYSTEM_ROOT/$SYSTEM_HEALTH_PY2_WHEEL_NAME
207-
sudo https_proxy=$https_proxy LANG=C chroot $FILESYSTEM_ROOT pip2 install $SYSTEM_HEALTH_PY2_WHEEL_NAME
208-
sudo rm -rf $FILESYSTEM_ROOT/$SYSTEM_HEALTH_PY2_WHEEL_NAME
209-
210204
# Install sonic-platform-common Python 3 package
211205
PLATFORM_COMMON_PY3_WHEEL_NAME=$(basename {{platform_common_py3_wheel_path}})
212206
sudo cp {{platform_common_py3_wheel_path}} $FILESYSTEM_ROOT/$PLATFORM_COMMON_PY3_WHEEL_NAME
@@ -219,6 +213,12 @@ sudo https_proxy=$https_proxy LANG=C chroot $FILESYSTEM_ROOT pip2 install thrift
219213
sudo https_proxy=$https_proxy LANG=C chroot $FILESYSTEM_ROOT pip3 install thrift==0.13.0
220214
{% endif %}
221215

216+
# Install system-health Python 3 package
217+
SYSTEM_HEALTH_PY3_WHEEL_NAME=$(basename {{system_health_py3_wheel_path}})
218+
sudo cp {{system_health_py3_wheel_path}} $FILESYSTEM_ROOT/$SYSTEM_HEALTH_PY3_WHEEL_NAME
219+
sudo https_proxy=$https_proxy LANG=C chroot $FILESYSTEM_ROOT pip3 install $SYSTEM_HEALTH_PY3_WHEEL_NAME
220+
sudo rm -rf $FILESYSTEM_ROOT/$SYSTEM_HEALTH_PY3_WHEEL_NAME
221+
222222
# Install prerequisites needed for installing the Python m2crypto package, used by sonic-utilities
223223
# These packages can be uninstalled after intallation
224224
sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get -y install build-essential libssl-dev swig

rules/system-health.mk

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# system health python2 wheel
1+
# system health Python wheel
22

3-
SYSTEM_HEALTH = system_health-1.0-py2-none-any.whl
3+
SYSTEM_HEALTH = system_health-1.0-py3-none-any.whl
44
$(SYSTEM_HEALTH)_SRC_PATH = $(SRC_PATH)/system-health
5-
$(SYSTEM_HEALTH)_PYTHON_VERSION = 2
6-
$(SYSTEM_HEALTH)_DEPENDS = $(SONIC_PY_COMMON_PY2) $(SWSSSDK_PY2) $(SONIC_CONFIG_ENGINE)
5+
$(SYSTEM_HEALTH)_PYTHON_VERSION = 3
6+
$(SYSTEM_HEALTH)_DEPENDS = $(SONIC_PY_COMMON_PY3) $(SWSSSDK_PY3) $(SONIC_CONFIG_ENGINE_PY3)
77
SONIC_PYTHON_WHEELS += $(SYSTEM_HEALTH)
88

9-
export system_health_py2_wheel_path="$(addprefix $(PYTHON_WHEELS_PATH)/,$(SYSTEM_HEALTH))"
9+
export system_health_py3_wheel_path="$(addprefix $(PYTHON_WHEELS_PATH)/,$(SYSTEM_HEALTH))"
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
from . import hardware_checker
2-
from . import service_checker
1+
from . import hardware_checker
2+
from . import service_checker
Lines changed: 144 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -1,144 +1,144 @@
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

Comments
 (0)