Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / systemd / systemd_mem @ 11c6d827

Historique | Voir | Annoter | Télécharger (3,26 ko)

1
#!/bin/sh
2
: << =cut
3

    
4
=head1 NAME
5

    
6
systemd_mem - Plugin to graph the memory usage of multiple systemd services as reported by systemctl
7

    
8
=head1 CONFIGURATION
9

    
10
The environment variable "services" is used to provide a space separated list of services to graph.
11

    
12
Example:
13
   [systemd_mem]
14
   env.services apache2 tomcat9 mysql
15

    
16
env.services defaults to "munin-node".
17

    
18
=head1 VERSION
19

    
20
1.0
21

    
22
=head1 AUTHOR
23

    
24
Paul Alexandrow <paul@alexandrow.org>
25

    
26
=head1 LICENSE
27

    
28
Copyright 2021 Paul Alexandrow, paul@alexandrow.org
29

    
30
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
31
and associated documentation files (the "Software"), to deal in the Software without
32
restriction, including without limitation the rights to use, copy, modify, merge, publish,
33
distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
34
Software is furnished to do so, subject to the following conditions:
35

    
36
The above copyright notice and this permission notice shall be included in all copies or
37
substantial portions of the Software.
38

    
39
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
40
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
41
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
42
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
43
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
44

    
45
=head1 MAGIC MARKERS
46

    
47
#%# family=auto
48
#%# capabilities=autoconf
49

    
50
=cut
51

    
52
. "$MUNIN_LIBDIR/plugins/plugin.sh"
53

    
54
output_config() {
55
    echo "graph_title Systemd Service Memory Usage"
56
    echo "graph_info Memory usage for selected services as reported by systemctl"
57
    echo "graph_vlabel bytes"
58
    echo "graph_args --base 1024 --lower-limit 0"
59
    echo "graph_category memory"
60
    for service in ${services:-"munin-node"}; do
61
        clean_name="$(clean_fieldname "$service")"
62
        description=$(systemctl show "$service" --all | grep ^Description= | cut -d '=' -f 2)
63
        printf "%s.label %s\n" "$clean_name" "$description"
64
        printf "%s.info memory usage\n" "$clean_name"
65
    done
66
}
67

    
68
output_values() {
69
    for service in ${services:-"munin-node"}; do
70
        clean_name="$(clean_fieldname "$service")"
71
        usage=$(systemctl show "$service" --all | grep ^MemoryCurrent= | cut -d '=' -f 2)
72
        if [ "$usage" = "[not set]" ]; then
73
            usage=0
74
        fi
75
        printf  "%s.value %d\n" "$clean_name" "$usage"
76
    done
77
}
78

    
79
output_usage() {
80
    printf >&2 "%s - munin plugin to graph memory usage for systemd services\n" "${0##*/}"
81
    printf >&2 "Usage: %s [config]\n" "${0##*/}"
82
}
83

    
84
output_autoconf() {
85
    if command -v systemctl >/dev/null 2>&1; then
86
       echo "yes"
87
    else
88
        echo "no (systemctl not found)";
89
    fi
90
}
91

    
92
case $# in
93
    0)
94
        output_values
95
        ;;
96
    1)
97
        case $1 in
98
            autoconf)
99
                output_autoconf
100
                ;;
101
            config)
102
                output_config
103
                ;;
104
            fetch)
105
                output_values
106
                ;;
107
            *)
108
                output_usage
109
                exit 1
110
                ;;
111
        esac
112
        ;;
113
    *)
114
        output_usage
115
        exit 1
116
        ;;
117
esac