Manage multiple commands (&&) in the secure popen

This commit is contained in:
nicolargo 2021-04-25 09:42:23 +02:00
parent 29d4662fd2
commit a2c0357898
4 changed files with 35 additions and 27 deletions

View File

@ -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

View File

@ -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.*

View File

@ -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 command OR an error message
Return: the result of the commands
"""
ret = None
ret = ''
# Split by multiple commands '&&'
for c in cmd.split('&&'):
ret += __secure_popen(c)
return ret
def __secure_popen(cmd):
"""A more or less secure way to execute system command
Manage redirection (>) and pipes (|)
"""
ret = ''
# Split by redirection '>'
cmd_split_redirect = cmd.split('>')

View File

@ -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"""