Projet

Général

Profil

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

root / plugins / systemd / systemd_mem @ b5595716

Historique | Voir | Annoter | Télécharger (4,69 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
As an alternative you can use all_services to display memory for all running systemd services
19
   [systemd_mem]
20
   env.all_services true
21

    
22
all_services shows memory for all running units of type service.
23

    
24
=head1 VERSION
25

    
26
1.1
27

    
28
=head1 AUTHORS
29

    
30
Paul Alexandrow <paul@alexandrow.org>
31
Andreas Perhab <a.perhab@wtioit.at>
32

    
33
=head1 LICENSE
34

    
35
Copyright 2021 Paul Alexandrow, paul@alexandrow.org
36

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

    
43
The above copyright notice and this permission notice shall be included in all copies or
44
substantial portions of the Software.
45

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

    
52
=head1 MAGIC MARKERS
53

    
54
#%# family=auto
55
#%# capabilities=autoconf
56

    
57
=cut
58

    
59
. "$MUNIN_LIBDIR/plugins/plugin.sh"
60

    
61
# at least on debian stretch services report this as unlimited / unknown value (2^64)
62
# see https://lists.freedesktop.org/archives/systemd-devel/2018-July/041092.html
63
SYSTEMD_UNLIMITED="18446744073709551615"
64

    
65
all_services=${all_services:-}
66
services=${services:-"munin-node"}
67

    
68
for_services() {
69
    if [ "$all_services" != "" ]; then
70
        systemctl --type=service --state=running --no-pager --no-legend --output=short-precise \
71
          | awk '{sub(".service$", "", $1); print $1}'
72
    else
73
        echo "$services"
74
    fi
75
}
76

    
77
output_config() {
78
    echo "graph_title Systemd Service Memory Usage"
79
    echo "graph_info Memory usage for selected services as reported by systemctl"
80
    echo "graph_vlabel bytes"
81
    echo "graph_args --base 1024 --lower-limit 0"
82
    echo "graph_category memory"
83
    for service in $(for_services); do
84
        clean_name="$(clean_fieldname "$service")"
85
        description=$(systemctl show "$service" --property=Description | cut -d '=' -f 2-)
86
        warning=$(systemctl show "$service" --property=MemoryHigh | cut -d '=' -f 2)
87
        critical=$(systemctl show "$service" --property=MemoryMax | cut -d '=' -f 2)
88
        if [ "$critical" = "infinity" ] || [ "$critical" = "$SYSTEMD_UNLIMITED" ] ; then
89
            critical=$(systemctl show "$service" --property=MemoryLimit | cut -d '=' -f 2)
90
        fi
91
        printf "%s.label %s\n" "$clean_name" "$description"
92
        printf "%s.info memory usage\n" "$clean_name"
93
        if [ "$warning" != "infinity" ] && [ "$warning" != "$SYSTEMD_UNLIMITED" ] ; then
94
          printf "%s.warning %s\n" "$clean_name" "$warning"
95
        fi
96
        if [ "$critical" != "infinity" ] && [ "$critical" != "$SYSTEMD_UNLIMITED" ] ; then
97
          printf "%s.critical %s\n" "$clean_name" "$critical"
98
        fi
99
    done
100
}
101

    
102
output_values() {
103
    for service in $(for_services); do
104
        clean_name="$(clean_fieldname "$service")"
105
        usage=$(systemctl show "$service" --property=MemoryCurrent | cut -d '=' -f 2)
106
        if [ "$usage" = "[not set]" ] || [ "$usage" = "$SYSTEMD_UNLIMITED" ]; then
107
            usage="U"
108
        fi
109
        printf  "%s.value %s\n" "$clean_name" "$usage"
110
    done
111
}
112

    
113
output_usage() {
114
    printf >&2 "%s - munin plugin to graph memory usage for systemd services\n" "${0##*/}"
115
    printf >&2 "Usage: %s [config]\n" "${0##*/}"
116
}
117

    
118
output_autoconf() {
119
    if command -v systemctl >/dev/null 2>&1; then
120
       echo "yes"
121
    else
122
        echo "no (systemctl not found)";
123
    fi
124
}
125

    
126
case $# in
127
    0)
128
        output_values
129
        ;;
130
    1)
131
        case $1 in
132
            autoconf)
133
                output_autoconf
134
                ;;
135
            config)
136
                output_config
137
                ;;
138
            fetch)
139
                output_values
140
                ;;
141
            *)
142
                output_usage
143
                exit 1
144
                ;;
145
        esac
146
        ;;
147
    *)
148
        output_usage
149
        exit 1
150
        ;;
151
esac