Projet

Général

Profil

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

root / plugins / disk / btrfs_device_usage @ e169950d

Historique | Voir | Annoter | Télécharger (5,42 ko)

1
#!/usr/bin/env python3
2

    
3

    
4
"""
5
=pod
6

    
7
=head1 NAME
8

    
9
btrfs_device_usage - Script to monitor usage of btrfs devices
10

    
11
=head1 CONFIGURATION
12

    
13
Simply create a symlink in your plugins directory like with any other plugin.
14
Must be run as root.
15

    
16
[btrfs_device_usage]
17
user root
18

    
19
You can optionaly configure the warning and critical limits for byte and
20
percentage graphs. By default percent_warning is set to 95 and percent_critical
21
is 98. For the byte-graphs there are no default limits set. You can set the
22
limits either for the entire graph (byte_ percent_) or per individual filesystem
23
using it's UUID (replace minus with underscore). The individual values take
24
precedence over the general ones. See the following example:
25

    
26
btrfs_device_usage]
27
user root
28
env.byte_warning 400300200100
29
env.percent_critical 97
30
env.percent_10164f3f_6670_4982_a941_bffb50d27f29_warning 23
31
env.byte_10164f3f_6670_4982_a941_bffb50d27f29_critical 5000000000
32

    
33
=head2 DEFAULT CONFIGURATION
34

    
35
=head1 BUGS
36

    
37
=head1 AUTHOR
38

    
39
2019-2021, HaseHarald
40

    
41
=head1 MAGIC MARKERS
42

    
43
 #%# family=auto
44
 #%# capabilities=autoconf
45

    
46
=head1 LICENSE
47

    
48
LGPLv3
49

    
50
=cut
51
"""
52

    
53

    
54
# This file contains a munin-plugin to gather btrfs statistics per device.
55
#
56
# This is free software: you can redistribute it and/or modify
57
# it under the terms of the GNU Lesser General Public License as published by
58
# the Free Software Foundation, either version 3 of the License, or
59
# (at your option) any later version.
60
#
61
# This is distributed in the hope that it will be useful,
62
# but WITHOUT ANY WARRANTY; without even the implied warranty of
63
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
64
# GNU Lesser General Public License for more details.
65
#
66
# You should have received a copy of the GNU Lesser General Public License
67
# along with this plugin.  If not, see <http://www.gnu.org/licenses/>.
68

    
69

    
70
import btrfs
71
import os
72
import sys
73

    
74

    
75
def munin_config(fs):
76
    fsid = str(fs.fsid).replace('-', '_')
77

    
78
    byte_warning = os.getenv('byte_warning', default = False)
79
    byte_critical = os.getenv('byte_critical', default = False)
80
    byte_warning = os.getenv('byte_' + fsid + '_warning', default = byte_warning)
81
    byte_critical = os.getenv('byte_' + fsid + '_critical', default = byte_critical)
82

    
83
    percent_warning = os.getenv('percent_warning', default = 95)
84
    percent_critical = os.getenv('percent_critical', default = 98)
85
    percent_warning = os.getenv('percent_' + fsid + '_warning', default = percent_warning)
86
    percent_critical = os.getenv('percent_' + fsid + '_critical', default = percent_critical)
87

    
88
    print("multigraph btrfs_device_usage_byte_" + fsid)
89
    print("graph_title btrfs usage by device in bytes on " + fs.path)
90
    print("graph_args --base 1024 -l 0")
91
    print("graph_scale yes")
92
    print("graph_vlabel bytes")
93
    print("graph_category disk")
94
    print("graph_info This graph shows bytes used by btrfs on every device")
95

    
96
    devices = fs.devices()
97
    for this_device in devices:
98
        this_dev_info = fs.dev_info(this_device.devid)
99
        this_dev_name = this_dev_info.path.replace('/dev/', '')
100
        print("btrfs_bytes_" + fsid + "_" + str(this_device.devid) +
101
              ".label " + this_dev_name)
102
        if byte_warning != False:
103
            print("btrfs_bytes_" + fsid + "_" + str(this_device.devid) +
104
                  ".warning " + str(byte_warning))
105
        if byte_critical != False:
106
            print("btrfs_bytes_" + fsid + "_" + str(this_device.devid) +
107
                  ".critical " + str(byte_critical))
108

    
109
    print("")
110

    
111
    print("multigraph btrfs_device_usage_percent_" + fsid)
112
    print("graph_title btrfs usage by device in percent on " + fs.path)
113
    print("graph_args --base 1000 -l 0")
114
    print("graph_scale no")
115
    print("graph_vlabel %")
116
    print("graph_category disk")
117
    print("graph_info This graph shows percentage used by btrfs on every \
118
          device. Maesured in percentage of device capacity.")
119

    
120
    devices = fs.devices()
121
    for this_device in devices:
122
        this_dev_info = fs.dev_info(this_device.devid)
123
        this_dev_name = this_dev_info.path.replace('/dev/', '')
124
        print("btrfs_percent_" + fsid + "_" + str(this_device.devid) +
125
              ".label " + this_dev_name)
126
        print("btrfs_percent_" + fsid + "_" + str(this_device.devid) +
127
              ".warning " + str(percent_warning))
128
        print("btrfs_percent_" + fsid + "_" + str(this_device.devid) +
129
              ".critical " + str(percent_critical))
130

    
131
    print("")
132

    
133

    
134
def munin_values(fs):
135
    fsid = str(fs.fsid).replace('-', '_')
136
    devices = fs.devices()
137

    
138
    print("multigraph btrfs_device_usage_byte_" + fsid)
139

    
140
    for this_device in devices:
141
        this_dev_info = fs.dev_info(this_device.devid)
142
        print("btrfs_bytes_" + fsid + "_" + str(this_device.devid) +
143
              ".value " + str(this_dev_info.bytes_used))
144

    
145
    print("")
146

    
147
    # Reset device iterator
148
    devices = fs.devices()
149

    
150
    print("multigraph btrfs_device_usage_percent_" + fsid)
151

    
152
    for this_device in devices:
153
        this_dev_info = fs.dev_info(this_device.devid)
154
        usage = 100.0 * this_dev_info.bytes_used / this_dev_info.total_bytes
155
        print("btrfs_percent_" + fsid + "_" + str(this_device.devid) +
156
              ".value " + str(round(usage, 2)))
157

    
158
    print("")
159

    
160

    
161
def main():
162
    for path in btrfs.utils.mounted_filesystem_paths():
163
        with btrfs.FileSystem(path) as fs:
164
            if len(sys.argv) > 1 and sys.argv[1] == "config":
165
                munin_config(fs)
166
            else:
167
                munin_values(fs)
168

    
169

    
170
if __name__ == "__main__":
171
    main()
172

    
173
exit(0)