Allow embedded AMP python script to be placed in a configurable location #1734

This commit is contained in:
nicolargo 2020-09-20 17:55:54 +02:00
parent 47b7dc9d5d
commit 9e76791971
3 changed files with 34 additions and 9 deletions

View File

@ -541,6 +541,7 @@ path=/
##############################################################################
# AMPS
# * enable: Enable (true) or disable (false) the AMP
# * plugin_path: overwrite the default folder where plugin file are stored
# * regex: Regular expression to filter the process(es)
# * refresh: The AMP is executed every refresh seconds
# * one_line: (optional) Force (if true) the AMP to be displayed in one line

View File

@ -96,6 +96,14 @@ Embedded AMP
Glances provides some specific AMP scripts (replacing the ``command``
line). You can write your own AMP script to fill your needs. AMP scripts
are located in the ``amps`` folder and should be named ``glances_*.py``.
You can also overwrite this default location by adding the following
configuration key to your AMP configuration section (it should be a
folder):
[amp_foo]
plugin_path=/home/foo
An AMP script define an Amp class (``GlancesAmp``) with a mandatory
update method. The update method call the ``set_result`` method to set
the AMP return string. The return string is a string with one or more

View File

@ -64,13 +64,27 @@ class AmpsList(object):
# For each AMP scrip, call the load_config method
for s in self.config.sections():
if s.startswith("amp_"):
# An AMP section exists in the configuration file
# If an AMP script exist in the glances/amps folder, use it
# Load the plugin file (python AMP file)
# If the plugin_path is defined, it will be loaded in this path
# else it will be loaded in the default AMP folder
amps_path_external = self.config.get_value(s, 'plugin_path')
if amps_path_external:
# An external path as been defined by the user
# Check if the file exist
amp_script = os.path.join(
amps_path_external, header + s[4:] + ".py")
if not os.path.exists(amp_script):
logger.error("Can not load {} AMP".format(amp_script))
continue
else:
# If an AMP script exist in the glances/amps folder, use it
amp_script = os.path.join(amps_path, header + s[4:] + ".py")
if not os.path.exists(amp_script):
# If not, use the default script
amp_script = os.path.join(amps_path, "glances_default.py")
# Create the AMP instance and add it to the flobal dict
amp_conf_name = s[4:]
amp_script = os.path.join(amps_path, header + s[4:] + ".py")
if not os.path.exists(amp_script):
# If not, use the default script
amp_script = os.path.join(amps_path, "glances_default.py")
try:
amp = __import__(os.path.basename(amp_script)[:-3])
except ImportError as e:
@ -78,13 +92,15 @@ class AmpsList(object):
except Exception as e:
logger.warning("Cannot load {} AMP ({})".format(amp_conf_name, e))
else:
# Create the AMP instance
new_amp = amp.Amp(name=amp_conf_name, args=self.args)
# Load the AMP configuration
new_amp.load_config(self.config)
# Add the AMP to the dictionary
# The key is the AMP name
# for example, the file glances_xxx.py
# generate self._amps_list["xxx"] = ...
self.__amps_dict[amp_conf_name] = amp.Amp(name=amp_conf_name, args=self.args)
# Load the AMP configuration
self.__amps_dict[amp_conf_name].load_config(self.config)
self.__amps_dict[amp_conf_name] = new_amp
# Log AMPs list
logger.debug("AMPs list: {}".format(self.getList()))