Projet

Général

Profil

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

root / plugins / systemd / systemd_mem @ b44d8093

Historique | Voir | Annoter | Télécharger (3,8 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" --property=Description | cut -d '=' -f 2)
63
        warning=$(systemctl show "$service" --property=MemoryHigh | cut -d '=' -f 2)
64
        critical=$(systemctl show "$service" --property=MemoryMax | cut -d '=' -f 2)
65
        if [ "$critical" = "infinity" ] ; then
66
            critical=$(systemctl show "$service" --property=MemoryLimit | cut -d '=' -f 2)
67
        fi
68
        printf "%s.label %s\n" "$clean_name" "$description"
69
        printf "%s.info memory usage\n" "$clean_name"
70
        if [ "$warning" != "infinity" ] ; then
71
          printf "%s.warning %s\n" "$clean_name" "$warning"
72
        fi
73
        if [ "$critical" != "infinity" ] ; then
74
          printf "%s.critical %s\n" "$clean_name" "$critical"
75
        fi
76
    done
77
}
78

    
79
output_values() {
80
    for service in ${services:-"munin-node"}; do
81
        clean_name="$(clean_fieldname "$service")"
82
        usage=$(systemctl show "$service" --property=MemoryCurrent | cut -d '=' -f 2)
83
        if [ "$usage" = "[not set]" ]; then
84
            usage=0
85
        fi
86
        printf  "%s.value %d\n" "$clean_name" "$usage"
87
    done
88
}
89

    
90
output_usage() {
91
    printf >&2 "%s - munin plugin to graph memory usage for systemd services\n" "${0##*/}"
92
    printf >&2 "Usage: %s [config]\n" "${0##*/}"
93
}
94

    
95
output_autoconf() {
96
    if command -v systemctl >/dev/null 2>&1; then
97
       echo "yes"
98
    else
99
        echo "no (systemctl not found)";
100
    fi
101
}
102

    
103
case $# in
104
    0)
105
        output_values
106
        ;;
107
    1)
108
        case $1 in
109
            autoconf)
110
                output_autoconf
111
                ;;
112
            config)
113
                output_config
114
                ;;
115
            fetch)
116
                output_values
117
                ;;
118
            *)
119
                output_usage
120
                exit 1
121
                ;;
122
        esac
123
        ;;
124
    *)
125
        output_usage
126
        exit 1
127
        ;;
128
esac