From a2c0357898c24518867efd37adfc1e7bbed83b81 Mon Sep 17 00:00:00 2001 From: nicolargo Date: Sun, 25 Apr 2021 09:42:23 +0200 Subject: Manage multiple commands (&&) in the secure popen --- docs/aoa/actions.rst | 16 +++++++++------- docs/aoa/fs.rst | 25 ++++++++----------------- glances/secure.py | 20 +++++++++++++++++--- unitest.py | 1 + 4 files changed, 35 insertions(+), 27 deletions(-) diff --git a/docs/aoa/actions.rst b/docs/aoa/actions.rst index a594d34d..3ee6779e 100644 --- a/docs/aoa/actions.rst +++ b/docs/aoa/actions.rst @@ -28,16 +28,21 @@ reached: warning=70 warning_action=echo {{mnt_point}} {{used}}/{{size}} > /tmp/fs.alert -A last example would be to create a log file -containing the total user disk space usage for a device and notify by email each time a space trigger critical is -reached: +A last example would be to create a log file containing the total user disk +space usage for a device and notify by email each time a space trigger +critical is reached: .. code-block:: ini [fs] critical=90 critical_action_repeat=echo {{device_name}} {{percent}} > /tmp/fs.alert && python /etc/glances/actions.d/fs-critical.py - + + +.. note:: + Use && as seprator for multiple commands + + Within ``/etc/glances/actions.d/fs-critical.py``: .. code-block:: python @@ -55,9 +60,6 @@ Within ``/etc/glances/actions.d/fs-critical.py``: ps = subprocess.Popen(('echo', '-e', body), stdout=subprocess.PIPE) subprocess.call(['mail', '-s', 'CRITICAL: disk usage above 90%', '-r', 'postmaster@example.com', 'glances@example.com'], stdin=ps.stdout) - - - .. note:: You can use all the stats for the current plugin. See https://github.com/nicolargo/glances/wiki/The-Glances-RESTFULL-JSON-API diff --git a/docs/aoa/fs.rst b/docs/aoa/fs.rst index 23d4eecd..92428555 100644 --- a/docs/aoa/fs.rst +++ b/docs/aoa/fs.rst @@ -23,7 +23,7 @@ User disk space usage Status .. note:: Limit values can be overwritten in the configuration file under - the ``[filesystem]`` section. + the ``[fs]`` section. By default, the plugin only displays physical devices (hard disks, USB keys). To allow other file system types, you have to enable them in the @@ -35,29 +35,20 @@ system: [fs] allow=shm -Also, you can hide mount points as well (in the following ``/boot``): +Also, you can hide mount points using regular expressions. +To hide all mount points starting with /boot and /snap: .. code-block:: ini [fs] - hide=/boot.* + hide=/boot.*,/snap.* -Filtering can also be done on device name (Glances 3.1.4 or higher): +Filtering are also applied on device name (Glances 3.1.4 or higher). + +Example to hide all /dev/sdb mount points: .. code-block:: ini [fs] - hide=/dev/sdb2 - -RAID ----- - -*Availability: Linux* - -Thanks to the `pymdstat`_ library, if a ``RAID`` controller is detected -on your system, its status will be displayed as well: - -.. image:: ../_static/raid.png - -.. _pymdstat: https://github.com/nicolargo/pymdstat + hide=/dev/sdb.* diff --git a/glances/secure.py b/glances/secure.py index 7a2f8d7b..36af5e16 100644 --- a/glances/secure.py +++ b/glances/secure.py @@ -24,11 +24,25 @@ from subprocess import Popen, PIPE def secure_popen(cmd): - """A more or less secure way to execute system command + """A more or less secure way to execute system commands + Multiple command should be seprated with a && + + Return: the result of the commands + """ + ret = '' + + # Split by multiple commands '&&' + for c in cmd.split('&&'): + ret += __secure_popen(c) - Return: the result of the command OR an error message + return ret + + +def __secure_popen(cmd): + """A more or less secure way to execute system command + Manage redirection (>) and pipes (|) """ - ret = None + ret = '' # Split by redirection '>' cmd_split_redirect = cmd.split('>') diff --git a/unitest.py b/unitest.py index 0ed66d5b..f23864d1 100755 --- a/unitest.py +++ b/unitest.py @@ -381,6 +381,7 @@ class TestGlances(unittest.TestCase): print('INFO: [TEST_100] Secure functions') self.assertEqual(secure_popen('echo -n TEST'), 'TEST') self.assertEqual(secure_popen('echo FOO | grep FOO'), 'FOO\n') + self.assertEqual(secure_popen('echo -n TEST1 && echo -n TEST2'), 'TEST1TEST2') def test_999_the_end(self): """Free all the stats""" -- cgit v1.2.3