Skip to content

Commit 881017e

Browse files
committed
Reduced complexity of glances.plugins.system.PluginModel.update()
Part of nicolargo#2801. Broke `update` in two parts: * if `local`, stats taken from `get_stats_from_std_sys_lib` * and readability achieved by `add_human_readable_name` * else if `snmp`, taken from `update_stats_with_snmp`. `get_stats_from_std_sys_lib` has two helpers: 1. `get_win_version_and_platform`. 2. `get_linux_version_and_distro`. Signed-off-by: Ariel Otilibili <otilibil@eurecom.fr>
1 parent 6b036ec commit 881017e

File tree

1 file changed

+83
-50
lines changed

1 file changed

+83
-50
lines changed

glances/plugins/system/__init__.py

Lines changed: 83 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,84 @@ def __init__(self, args=None, config=None):
128128
# Get the default message (if defined)
129129
self.system_info_msg = config.get_value('system', 'system_info_msg') if config else None
130130

131+
def update_stats_with_snmp(self):
132+
try:
133+
stats = self.get_stats_snmp(snmp_oid=snmp_oid[self.short_system_name])
134+
except KeyError:
135+
stats = self.get_stats_snmp(snmp_oid=snmp_oid['default'])
136+
137+
# Default behavior: display all the information
138+
stats['os_name'] = stats['system_name']
139+
140+
# Windows OS tips
141+
if self.short_system_name == 'windows':
142+
for key, value in iteritems(snmp_to_human['windows']):
143+
if re.search(key, stats['system_name']):
144+
stats['os_name'] = value
145+
break
146+
147+
return stats
148+
149+
def add_human_readable_name(self, stats):
150+
if self.system_info_msg:
151+
try:
152+
hr_name = self.system_info_msg.format(**stats)
153+
except KeyError as e:
154+
logger.debug(f'Error in system_info_msg ({e})')
155+
hr_name = '{os_name} {os_version} {platform}'.format(**stats)
156+
elif stats['os_name'] == "Linux":
157+
hr_name = '{linux_distro} {platform} / {os_name} {os_version}'.format(**stats)
158+
else:
159+
hr_name = '{os_name} {os_version} {platform}'.format(**stats)
160+
return hr_name
161+
162+
def get_win_version_and_platform(self, stats):
163+
os_version = platform.win32_ver()
164+
# if the python version is 32 bit perhaps the windows operating
165+
# system is 64bit
166+
conditions = [
167+
stats['platform'] == '32bit',
168+
'PROCESSOR_ARCHITEW6432' in os.environ
169+
]
170+
171+
return {
172+
'os_version' : ' '.join(os_version[::2]),
173+
'platform' : '64bit' if all(conditions) else stats['platform']
174+
}
175+
176+
def get_linux_version_and_distro(self):
177+
try:
178+
linux_distro = platform.linux_distribution()
179+
except AttributeError:
180+
distro = _linux_os_release()
181+
else:
182+
if linux_distro[0] == '':
183+
distro = _linux_os_release()
184+
else:
185+
distro = ' '.join(linux_distro[:2])
186+
187+
return {
188+
'os_version' : platform.release(),
189+
'linux_distro' : distro
190+
}
191+
192+
def get_stats_from_std_sys_lib(self, stats):
193+
stats['os_name'] = platform.system()
194+
stats['hostname'] = platform.node()
195+
stats['platform'] = platform.architecture()[0]
196+
if stats['os_name'] == "Linux":
197+
stats.update(self.get_linux_version_and_distro())
198+
elif stats['os_name'].endswith('BSD') or stats['os_name'] == 'SunOS':
199+
stats['os_version'] = platform.release()
200+
elif stats['os_name'] == "Darwin":
201+
stats['os_version'] = platform.mac_ver()[0]
202+
elif stats['os_name'] == "Windows":
203+
stats.update(self.get_win_version_and_platform(stats))
204+
else:
205+
stats['os_version'] = ""
206+
207+
return stats
208+
131209
@GlancesPluginModel._check_decorator
132210
@GlancesPluginModel._log_result_decorator
133211
def update(self):
@@ -139,61 +217,16 @@ def update(self):
139217
stats = self.get_init_value()
140218

141219
if self.input_method == 'local':
142-
# Update stats using the standard system lib
143-
stats['os_name'] = platform.system()
144-
stats['hostname'] = platform.node()
145-
stats['platform'] = platform.architecture()[0]
146-
if stats['os_name'] == "Linux":
147-
try:
148-
linux_distro = platform.linux_distribution()
149-
except AttributeError:
150-
stats['linux_distro'] = _linux_os_release()
151-
else:
152-
if linux_distro[0] == '':
153-
stats['linux_distro'] = _linux_os_release()
154-
else:
155-
stats['linux_distro'] = ' '.join(linux_distro[:2])
156-
stats['os_version'] = platform.release()
157-
elif stats['os_name'].endswith('BSD') or stats['os_name'] == 'SunOS':
158-
stats['os_version'] = platform.release()
159-
elif stats['os_name'] == "Darwin":
160-
stats['os_version'] = platform.mac_ver()[0]
161-
elif stats['os_name'] == "Windows":
162-
os_version = platform.win32_ver()
163-
stats['os_version'] = ' '.join(os_version[::2])
164-
# if the python version is 32 bit perhaps the windows operating
165-
# system is 64bit
166-
if stats['platform'] == '32bit' and 'PROCESSOR_ARCHITEW6432' in os.environ:
167-
stats['platform'] = '64bit'
168-
else:
169-
stats['os_version'] = ""
220+
# Update stats using the standard system library
221+
stats = self.get_stats_from_std_sys_lib(stats)
170222

171223
# Add human readable name
172-
if self.system_info_msg:
173-
try:
174-
stats['hr_name'] = self.system_info_msg.format(**stats)
175-
except KeyError as e:
176-
logger.debug(f'Error in system_info_msg ({e})')
177-
stats['hr_name'] = '{os_name} {os_version} {platform}'.format(**stats)
178-
elif stats['os_name'] == "Linux":
179-
stats['hr_name'] = '{linux_distro} {platform} / {os_name} {os_version}'.format(**stats)
180-
else:
181-
stats['hr_name'] = '{os_name} {os_version} {platform}'.format(**stats)
224+
stats['hr_name'] = self.add_human_readable_name(stats)
182225

183226
elif self.input_method == 'snmp':
184227
# Update stats using SNMP
185-
try:
186-
stats = self.get_stats_snmp(snmp_oid=snmp_oid[self.short_system_name])
187-
except KeyError:
188-
stats = self.get_stats_snmp(snmp_oid=snmp_oid['default'])
189-
# Default behavior: display all the information
190-
stats['os_name'] = stats['system_name']
191-
# Windows OS tips
192-
if self.short_system_name == 'windows':
193-
for r, v in iteritems(snmp_to_human['windows']):
194-
if re.search(r, stats['system_name']):
195-
stats['os_name'] = v
196-
break
228+
stats = self.update_stats_with_snmp()
229+
197230
# Add human readable name
198231
stats['hr_name'] = stats['os_name']
199232

0 commit comments

Comments
 (0)