Projet

Général

Profil

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

root / plugins / disk / btrfs_device_usage @ 16d38264

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

1 bffbc23a HaseHarald
#!/usr/bin/env python3
2 6f0e91f8 HaseHarald
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 3f92394b HaseHarald
Must be run as root.
15
16 68190a6d HaseHarald
[btrfs_device_usage]
17
user root
18 6f0e91f8 HaseHarald
19 e169950d HaseHarald
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 6f0e91f8 HaseHarald
=head2 DEFAULT CONFIGURATION
34
35
=head1 BUGS
36
37
=head1 AUTHOR
38
39 e169950d HaseHarald
2019-2021, HaseHarald
40 6f0e91f8 HaseHarald
41
=head1 MAGIC MARKERS
42
43
 #%# family=auto
44 3f92394b HaseHarald
 #%# capabilities=autoconf
45 6f0e91f8 HaseHarald
46
=head1 LICENSE
47
48
LGPLv3
49
50
=cut
51
"""
52
53
54 10f2d97f HaseHarald
# 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 68190a6d HaseHarald
import os
72 10f2d97f HaseHarald
import sys
73
74 7d220d0a HaseHarald
75 10f2d97f HaseHarald
def munin_config(fs):
76
    fsid = str(fs.fsid).replace('-', '_')
77 bffbc23a HaseHarald
78 50265cce HaseHarald
    byte_warning = os.getenv('byte_warning', default=False)
79
    byte_critical = os.getenv('byte_critical', default=False)
80 76c21fe7 HaseHarald
    byte_warning = os.getenv('byte_' + fsid + '_warning',
81 50265cce HaseHarald
                             default=byte_warning)
82 76c21fe7 HaseHarald
    byte_critical = os.getenv('byte_' + fsid + '_critical',
83 50265cce HaseHarald
                              default=byte_critical)
84 68190a6d HaseHarald
85 50265cce HaseHarald
    percent_warning = os.getenv('percent_warning', default=95)
86
    percent_critical = os.getenv('percent_critical', default=98)
87 76c21fe7 HaseHarald
    percent_warning = os.getenv('percent_' + fsid + '_warning',
88 50265cce HaseHarald
                                default=percent_warning)
89 76c21fe7 HaseHarald
    percent_critical = os.getenv('percent_' + fsid + '_critical',
90 50265cce HaseHarald
                                 default=percent_critical)
91 68190a6d HaseHarald
92 10f2d97f HaseHarald
    print("multigraph btrfs_device_usage_byte_" + fsid)
93
    print("graph_title btrfs usage by device in bytes on " + fs.path)
94
    print("graph_args --base 1024 -l 0")
95
    print("graph_scale yes")
96
    print("graph_vlabel bytes")
97
    print("graph_category disk")
98
    print("graph_info This graph shows bytes used by btrfs on every device")
99 bffbc23a HaseHarald
100 10f2d97f HaseHarald
    devices = fs.devices()
101
    for this_device in devices:
102
        this_dev_info = fs.dev_info(this_device.devid)
103
        this_dev_name = this_dev_info.path.replace('/dev/', '')
104 675dedf1 HaseHarald
        print("btrfs_bytes_" + fsid + "_" + str(this_device.devid) +
105 bffbc23a HaseHarald
              ".label " + this_dev_name)
106 409e8896 HaseHarald
        if byte_warning:
107 68190a6d HaseHarald
            print("btrfs_bytes_" + fsid + "_" + str(this_device.devid) +
108
                  ".warning " + str(byte_warning))
109 409e8896 HaseHarald
        if byte_critical:
110 68190a6d HaseHarald
            print("btrfs_bytes_" + fsid + "_" + str(this_device.devid) +
111
                  ".critical " + str(byte_critical))
112 bffbc23a HaseHarald
113 10f2d97f HaseHarald
    print("")
114 bffbc23a HaseHarald
115 10f2d97f HaseHarald
    print("multigraph btrfs_device_usage_percent_" + fsid)
116
    print("graph_title btrfs usage by device in percent on " + fs.path)
117
    print("graph_args --base 1000 -l 0")
118
    print("graph_scale no")
119
    print("graph_vlabel %")
120
    print("graph_category disk")
121 7d220d0a HaseHarald
    print("graph_info This graph shows percentage used by btrfs on every \
122 bffbc23a HaseHarald
          device. Maesured in percentage of device capacity.")
123
124 10f2d97f HaseHarald
    devices = fs.devices()
125
    for this_device in devices:
126
        this_dev_info = fs.dev_info(this_device.devid)
127
        this_dev_name = this_dev_info.path.replace('/dev/', '')
128 675dedf1 HaseHarald
        print("btrfs_percent_" + fsid + "_" + str(this_device.devid) +
129 bffbc23a HaseHarald
              ".label " + this_dev_name)
130 68190a6d HaseHarald
        print("btrfs_percent_" + fsid + "_" + str(this_device.devid) +
131
              ".warning " + str(percent_warning))
132
        print("btrfs_percent_" + fsid + "_" + str(this_device.devid) +
133
              ".critical " + str(percent_critical))
134 bffbc23a HaseHarald
135 10f2d97f HaseHarald
    print("")
136
137 7d220d0a HaseHarald
138 10f2d97f HaseHarald
def munin_values(fs):
139
    fsid = str(fs.fsid).replace('-', '_')
140
    devices = fs.devices()
141 bffbc23a HaseHarald
142 10f2d97f HaseHarald
    print("multigraph btrfs_device_usage_byte_" + fsid)
143 bffbc23a HaseHarald
144 10f2d97f HaseHarald
    for this_device in devices:
145
        this_dev_info = fs.dev_info(this_device.devid)
146 675dedf1 HaseHarald
        print("btrfs_bytes_" + fsid + "_" + str(this_device.devid) +
147 bffbc23a HaseHarald
              ".value " + str(this_dev_info.bytes_used))
148
149 10f2d97f HaseHarald
    print("")
150 bffbc23a HaseHarald
151 10f2d97f HaseHarald
    # Reset device iterator
152
    devices = fs.devices()
153 bffbc23a HaseHarald
154 10f2d97f HaseHarald
    print("multigraph btrfs_device_usage_percent_" + fsid)
155 bffbc23a HaseHarald
156 10f2d97f HaseHarald
    for this_device in devices:
157
        this_dev_info = fs.dev_info(this_device.devid)
158 16d38264 HaseHarald
        if this_dev_info.total_bytes > 0:
159
            usage = 100.0 * this_dev_info.bytes_used / this_dev_info.total_bytes
160
            print("btrfs_percent_" + fsid + "_" + str(this_device.devid) +
161
                  ".value " + str(round(usage, 2)))
162
        else:
163
            print("btrfs_percent_" + fsid + "_" + str(this_device.devid) +
164
                  ".value U")
165
          
166 bffbc23a HaseHarald
167 10f2d97f HaseHarald
    print("")
168 bffbc23a HaseHarald
169 10f2d97f HaseHarald
170
def main():
171
    for path in btrfs.utils.mounted_filesystem_paths():
172
        with btrfs.FileSystem(path) as fs:
173
            if len(sys.argv) > 1 and sys.argv[1] == "config":
174
                munin_config(fs)
175
            else:
176
                munin_values(fs)
177
178
179
if __name__ == "__main__":
180
    main()
181
182
exit(0)