summaryrefslogtreecommitdiffstats
path: root/docs/aoa/actions.rst
blob: a594d34d2dbbedafa49641d8180d6dbc4f78f6a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
.. _actions:

Actions
=======

Glances can trigger actions on events.

By ``action``, we mean all shell command line. For example, if you want
to execute the ``foo.py`` script if the last 5 minutes load are critical
then add the ``_action`` line to the Glances configuration file:

.. code-block:: ini

    [load]
    critical=5.0
    critical_action=python /path/to/foo.py

All the stats are available in the command line through the use of the
`{{mustache}}`_ syntax. `Chevron`_ 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:

.. code-block:: ini

    [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-RESTFULL-JSON-API
    for the stats list.

It is also possible to repeat action until the end of the alert.
Keep in mind that the command line is executed every refresh time so
use with caution:

.. code-block:: ini

    [load]
    critical=5.0
    critical_action_repeat=/home/myhome/bin/bipper.sh

.. _{{mustache}}: https://mustache.github.io/
.. _Chevron: https://github.com/noahmorrison/chevron