Debian Codegraph @

examples/nagios/plugins/check_btrfs

raw symbols SCIP
1 #!/usr/bin/python3
2 #
3 # Copyright (C) 2016 Hans van Kranenburg <[email protected]>
4 #
5 # This file is part of python-btrfs.
6 #
7 # python-btrfs is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Lesser General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # python-btrfs is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public License
18 # along with python-btrfs. If not, see <http://www.gnu.org/licenses/>.
19
20
21 import argparse
22 import btrfs
23 import os
24 import sys
25
26 STATE_OK = 0
27 STATE_WARNING = 1
28 STATE_CRITICAL = 2
29 STATE_UNKNOWN = 3
30
31 state_str_map = {
32 STATE_OK: 'OK',
33 STATE_WARNING: 'WARNING',
34 STATE_CRITICAL: 'CRITICAL',
35 STATE_UNKNOWN: 'UNKNOWN',
36 }
37
38
39 def get_args():
40 parser = argparse.ArgumentParser("Check BTRFS filesystem usage")
41 parser.add_argument('-awg', '--allocated-warning-gib', type=int, default=0,
42 help="Exit with WARNING status if less than the specified amount of "
43 "disk space (in GiB) is unallocated")
44 parser.add_argument('-acg', '--allocated-critical-gib', type=int, default=0,
45 help="Exit with CRITICAL status if less than the specified amount of "
46 "disk space (in GiB) is unallocated")
47 parser.add_argument('-awp', '--allocated-warning-percent', type=int, default=100,
48 help="Exit with WARNING status if more than the specified percent of "
49 "disk space is allocated")
50 parser.add_argument('-acp', '--allocated-critical-percent', type=int, default=100,
51 help="Exit with CRITICAL status if more than the specified percent of "
52 "disk space is allocated")
53 parser.add_argument('-m', '--mountpoint', required=True,
54 help="Path to the BTRFS mountpoint")
55 args = parser.parse_args()
56
57 if not os.path.exists(args.mountpoint):
58 print("BTRFS mountpoint does not exist: {}".format(args.mountpoint))
59 return STATE_CRITICAL, args
60
61 if not os.access(args.mountpoint, os.R_OK):
62 print("Mountpoint is not accessible: {}".format(args.mountpoint))
63 return STATE_CRITICAL, args
64
65 if args.allocated_warning_gib < 0:
66 print("Allocated GiB warning threshold must be a positive integer value: {}".format(
67 args.allocated_warning_gib))
68 return STATE_CRITICAL, args
69 if args.allocated_critical_gib < 0:
70 print("Allocated GiB critical threshold must be a positive integer value: {}".format(
71 args.allocated_critical_gib))
72 return STATE_CRITICAL, args
73
74 if args.allocated_warning_percent < 0 or args.allocated_warning_percent > 100:
75 print("Allocated warning percentage must be between 0 and 100: {}".format(
76 args.allocated_warning_percent))
77 return STATE_CRITICAL, args
78 if args.allocated_critical_percent < 0 or args.allocated_critical_percent > 100:
79 print("Allocated critical percentage must be between 0 and 100: {}".format(
80 args.allocated_critical_percent))
81 return STATE_CRITICAL, args
82
83 return STATE_OK, args
84
85
86 def check_usage(args, fs):
87 warning = False
88 critical = False
89 msg = []
90
91 GiB = btrfs.utils.SZ_1G
92 bups = btrfs.utils.pretty_size
93 usage = fs.usage()
94 mixed_groups = fs.mixed_groups()
95
96 if not mixed_groups:
97 # Keep it a bit simple and only report remaining free space for data
98 unused_left = usage.free_data
99 else:
100 unused_left = usage.free_mixed
101
102 # Allocations are always in the physical address space.
103 # Usage is always in virtual address space.
104 if usage.allocatable_left < args.allocated_critical_gib * GiB:
105 msg.append("Critical: Unallocated left: {} (Unused left: {})".format(
106 bups(usage.allocatable_left), bups(unused_left)))
107 critical = True
108 elif usage.allocatable_left < args.allocated_warning_gib * GiB:
109 msg.append("Warning: Unallocated left: {} (Unused left: {})".format(
110 bups(usage.allocatable_left), bups(unused_left)))
111 warning = True
112
113 allocated_pct = int(round((usage.allocated * 100) / usage.allocatable))
114 # Again, be conservative, and only use remaining data space.
115 used_pct = int(round((usage.virtual_used * 100) /
116 (usage.virtual_used + usage.free_data)))
117
118 if allocated_pct >= args.allocated_critical_percent:
119 msg.append("Critical: Allocated: {}% (Used: {}%)".format(allocated_pct, used_pct))
120 critical = True
121 elif allocated_pct >= args.allocated_warning_percent:
122 msg.append("Warning: Allocated: {}% (Used: {}%)".format(allocated_pct, used_pct))
123 warning = True
124
125 summary = []
126 summary.append("Physical size: {}".format(bups(usage.total)))
127 summary.append("Allocatable: {}".format(bups(usage.allocatable)))
128 summary.append("Allocated: {} ({}%)".format(bups(usage.allocated), allocated_pct))
129 summary.append("Unallocatable: {} (Reclaimable: {})".format(
130 bups(usage.unallocatable_soft), bups(usage.unallocatable_reclaimable)))
131 summary.append("Virtual used: {} ({}%)".format(bups(usage.virtual_used), used_pct))
132
133 if critical:
134 return STATE_CRITICAL, msg, summary
135 if warning:
136 return STATE_WARNING, msg, summary
137 return STATE_OK, msg, summary
138
139
140 def check_dev_stats(fs):
141 state = STATE_OK
142 msg = []
143 summary = []
144 devices = list(fs.devices())
145 for device in devices:
146 stats = fs.dev_stats(device.devid)
147 device_msg = ["{}: {}".format(counter, value)
148 for counter, value in stats.counters.items()
149 if value != 0]
150 if len(device_msg) > 0:
151 msg.append("Device {}: {}".format(device.devid, ", ".join(device_msg)))
152 state = STATE_CRITICAL
153 summary.append("{} device(s)".format(len(devices)))
154 if state != STATE_OK:
155 return state, msg, summary
156 return state, msg, summary
157
158
159 def main():
160 args_state, args = get_args()
161 if args_state != STATE_OK:
162 return args_state
163
164 with btrfs.FileSystem(args.mountpoint) as fs:
165 state = STATE_OK
166 msg = []
167 summary = []
168
169 usage_state, usage_msg, usage_summary = check_usage(args, fs)
170 state = max(state, usage_state)
171 msg.extend(usage_msg)
172 summary.extend(usage_summary)
173
174 dev_stats_state, dev_stats_msg, dev_stats_summary = check_dev_stats(fs)
175 state = max(state, dev_stats_state)
176 msg.extend(dev_stats_msg)
177 summary.extend(dev_stats_summary)
178
179 if len(msg) > 0:
180 print("BTRFS {}: ".format(state_str_map[state]), end='')
181 print(", ".join(msg))
182 else:
183 print("BTRFS OK")
184 print(", ".join(summary))
185 return state
186
187
188 if __name__ == "__main__":
189 sys.exit(main())