From cdab656112f2106fcac746e6253cc46433a0415c Mon Sep 17 00:00:00 2001 From: Gaspard d'Hautefeuille Date: Thu, 18 Jun 2020 17:51:18 +0100 Subject: [PATCH 1/4] Reflect "used percent" user disk space `psutill` [says the following](https://github.com/giampaolo/psutil/blob/master/psutil/_psposix.py): > Note: UNIX usually reserves 5% disk space which is not accessible by user. In this function "total" and "used" values reflect the total and used disk space whereas "free" and "percent" represent the "free" and "used percent" user disk space. To fix the alert value that has been notified in this issue https://github.com/nicolargo/glances/issues/1658 where the alert has not been fixed. The issue https://github.com/nicolargo/glances/issues/644 has fixed it one way, this PR is completing the fix for the alert feature of the `fs` plugin. --- glances/plugins/glances_fs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/glances/plugins/glances_fs.py b/glances/plugins/glances_fs.py index bc0a981d..6c1e618a 100644 --- a/glances/plugins/glances_fs.py +++ b/glances/plugins/glances_fs.py @@ -199,7 +199,7 @@ class Plugin(GlancesPlugin): # Alert for i in self.stats: self.views[i[self.get_key()]]['used']['decoration'] = self.get_alert( - i['used'], maximum=i['size'], header=i['mnt_point']) + current=i['size'] - i['free'], maximum=i['size'], header=i['mnt_point']) def msg_curse(self, args=None, max_width=None): """Return the dict to display in the curse interface.""" From bf1aa0256edc2388acd90a8dce57b7ed9424d799 Mon Sep 17 00:00:00 2001 From: Gaspard d'Hautefeuille Date: Thu, 18 Jun 2020 18:25:36 +0100 Subject: [PATCH 2/4] Add user disk space usage Depends of PR https://github.com/nicolargo/glances/pull/1680 to be merged --- docs/aoa/fs.rst | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/docs/aoa/fs.rst b/docs/aoa/fs.rst index 9ee0cc54..37dae695 100644 --- a/docs/aoa/fs.rst +++ b/docs/aoa/fs.rst @@ -8,18 +8,18 @@ File System Glances displays the used and total file system disk space. The unit is adapted dynamically. -Alerts are set for used disk space. +Alerts are set for `user disk space usage `_. Legend: -=========== ============ -Disk usage Status -=========== ============ -``<50%`` ``OK`` -``>50%`` ``CAREFUL`` -``>70%`` ``WARNING`` -``>90%`` ``CRITICAL`` -=========== ============ +===================== ============ +User disk space usage Status +===================== ============ +``<50%`` ``OK`` +``>50%`` ``CAREFUL`` +``>70%`` ``WARNING`` +``>90%`` ``CRITICAL`` +===================== ============ .. note:: Limit values can be overwritten in the configuration file under From 4d032cf7982fc56d436cc89a1e089867668429bf Mon Sep 17 00:00:00 2001 From: Gaspard d'Hautefeuille Date: Mon, 22 Jun 2020 20:19:36 +0100 Subject: [PATCH 3/4] Add email action for space trigger critical --- docs/aoa/actions.rst | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/docs/aoa/actions.rst b/docs/aoa/actions.rst index b83f1737..d52ba2ab 100644 --- a/docs/aoa/actions.rst +++ b/docs/aoa/actions.rst @@ -25,10 +25,40 @@ reached: [fs] 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: + +.. code-block:: ini + + [fs] + critical=90 + critical_action_repeat=echo {{device_name}} {{percent}} > /tmp/fs.alert && python /etc/glances/actions.d/fs-critical.py + +Within ``/etc/glances/actions.d/fs-critical.py``: + +.. code-block:: python + + import subprocess + from requests import get + + fs_alert = open('/tmp/fs.alert', 'r').readline().strip().split(' ') + device = fs_alert[0] + percent = fs_alert[1] + system = subprocess.check_output(['uname', '-rn']).decode('utf-8').strip() + ip = get('https://api.ipify.org').text + + body = 'Used user disk space for ' + device + ' is at ' + percent + '%.\nPlease cleanup the filesystem to clear the alert.\nServer: ' + str(system)+ '.\nIP address: ' + ip + 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-2.x-API-How-to + https://github.com/nicolargo/glances/wiki/The-Glances-RESTFULL-JSON-API for the stats list. It is also possible to repeat action until the end of the alert. From 09cfa64596004cdd637dc96f30aef6648ddd68ec Mon Sep 17 00:00:00 2001 From: Gaspard d'Hautefeuille Date: Mon, 22 Jun 2020 20:35:45 +0100 Subject: [PATCH 4/4] pystache requirement to render the mustache syntax --- docs/aoa/actions.rst | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/aoa/actions.rst b/docs/aoa/actions.rst index d52ba2ab..448d4fea 100644 --- a/docs/aoa/actions.rst +++ b/docs/aoa/actions.rst @@ -16,7 +16,9 @@ then add the ``_action`` line to the Glances configuration file: critical_action=python /path/to/foo.py All the stats are available in the command line through the use of the -`{{mustache}}`_ syntax. Another example would be to create a log file +`{{mustache}}`_ syntax. `Pystache`_ is required to render the mustache's template syntax. + +Another example would be to create a log file containing used vs total disk space if a space trigger warning is reached: @@ -72,3 +74,4 @@ use with caution: critical_action_repeat=/home/myhome/bin/bipper.sh .. _{{mustache}}: https://mustache.github.io/ +.. _Pystache: https://github.com/defunkt/pystache