Merge branch 'develop' of https://github.com/monochromec/glances into monochromec-develop

This commit is contained in:
nicolargo 2023-11-01 08:41:56 +01:00
commit 9bf3cb33d6
3 changed files with 40 additions and 21 deletions

View File

@ -89,6 +89,7 @@ class _GlancesCurses(object):
# 'w' > Delete finished warning logs
'W': {'switch': 'disable_wifi'},
# 'x' > Delete finished warning and critical logs
'Y': {'switch': 'enable_systemd'},
# 'z' > Enable or disable processes
# '+' > Increase the process nice level
# '-' > Decrease the process nice level
@ -117,6 +118,7 @@ class _GlancesCurses(object):
'raid',
'smart',
'sensors',
'systemd',
'now',
]
_left_sidebar_min_width = 23

View File

@ -70,8 +70,16 @@ class GlancesPluginModel(object):
:stats_init_value: Default value for a stats item
"""
# Build the plugin name
# Get second-last entry as the last one will always be 'model'
self.plugin_name = self.__class__.__module__.split('.')[-2]
_mod = self.__class__.__module__
_comp = _mod.split('.')
# Internal or external module (former prefixed by 'glances.plugins')
if 'glances.plugins' in _mod:
_ndx = 2
else:
_ndx = -2
self.plugin_name = _comp[_ndx]
if self.plugin_name.startswith('glances_'):
self.plugin_name = self.plugin_name.split('glances_')[1]
logger.debug("Init {} plugin".format(self.plugin_name))

View File

@ -15,7 +15,7 @@ import sys
import threading
import traceback
from importlib import import_module
import pkgutil
import pathlib
from glances.logger import logger
from glances.globals import exports_path, plugins_path, sys_path
@ -142,10 +142,16 @@ class GlancesStats(object):
def get_addl_plugins(self, plugin_path):
""" Get list of additonal plugins """
_plugin_list = []
for plugin in pkgutil.walk_packages([plugin_path]):
# Make sure we only include top-level packages in that directory
if plugin.ispkg and not plugin.name.startswith('__') and plugin.module_finder.path == plugin_path:
_plugin_list.append(plugin.name)
for plugin in os.listdir(plugin_path):
path = os.path.join(plugin_path, plugin)
if os.path.isdir(path) and not path.startswith('__'):
# Poor man's walk_pkgs - can't use pkgutil as the module would be already imported here!
for fil in pathlib.Path(path).glob('*.py'):
if fil.is_file():
with open(fil) as fd:
if 'PluginModel' in fd.read():
_plugin_list.append(plugin)
break
return _plugin_list
@ -154,7 +160,7 @@ class GlancesStats(object):
if config and config.parser.has_option('global', 'plugin_dir'):
path = config.parser['global']['plugin_dir']
if args and 'plugin_dir' in args:
if args and 'plugin_dir' in args and args.plugin_dir:
path = args.plugin_dir
if path:
@ -164,19 +170,22 @@ class GlancesStats(object):
# Ensure that plugins can be found in plugin_dir
sys.path.insert(0, path)
for plugin in get_addl_plugins(self, path):
start_duration.reset()
try:
_mod_loaded = import_module(plugin+'.model')
self._plugins[plugin] = _mod_loaded.PluginModel(args=args, config=config)
logger.debug("Plugin {} started in {} seconds".format(plugin, start_duration.get()))
except Exception as e:
# If a plugin can not be loaded, display a critical message
# on the console but do not crash
logger.critical("Error while initializing the {} plugin ({})".format(plugin, e))
logger.error(traceback.format_exc())
# An error occurred, disable the plugin
if args:
setattr(args, 'disable_' + plugin, False)
if plugin in sys.modules:
logger.warn(f"Pugin {plugin} already in sys.modules, skipping (workaround: rename plugin)")
else:
start_duration.reset()
try:
_mod_loaded = import_module(plugin+'.model')
self._plugins[plugin] = _mod_loaded.PluginModel(args=args, config=config)
logger.debug("Plugin {} started in {} seconds".format(plugin, start_duration.get()))
except Exception as e:
# If a plugin can not be loaded, display a critical message
# on the console but do not crash
logger.critical("Error while initializing the {} plugin ({})".format(plugin, e))
logger.error(traceback.format_exc())
# An error occurred, disable the plugin
if args:
setattr(args, 'disable_' + plugin, False)
sys.path = _sys_path
# Log plugins list