From 9e767919715982b666de544ebf3394a536ece2b7 Mon Sep 17 00:00:00 2001 From: nicolargo Date: Sun, 20 Sep 2020 17:55:54 +0200 Subject: Allow embedded AMP python script to be placed in a configurable location #1734 --- conf/glances.conf | 1 + docs/aoa/amps.rst | 8 ++++++++ glances/amps_list.py | 34 +++++++++++++++++++++++++--------- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/conf/glances.conf b/conf/glances.conf index 1502174e..463b988f 100644 --- a/conf/glances.conf +++ b/conf/glances.conf @@ -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 diff --git a/docs/aoa/amps.rst b/docs/aoa/amps.rst index 5de564ab..f44f9dd3 100644 --- a/docs/aoa/amps.rst +++ b/docs/aoa/amps.rst @@ -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 diff --git a/glances/amps_list.py b/glances/amps_list.py index 1837aec4..f567d174 100644 --- a/glances/amps_list.py +++ b/glances/amps_list.py @@ -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())) -- cgit v1.2.3