]> code.delx.au - monosys/blob - healthcheck/checkspace
Rename all the things
[monosys] / healthcheck / checkspace
1 #!/usr/bin/env python3
2
3 import os
4 import sys
5
6 def pp_size(size):
7 suffixes = ["", "KiB", "MiB", "GiB"]
8 for i, suffix in enumerate(suffixes):
9 if size < 1024:
10 break
11 size /= 1024
12 return "%.2f %s" % (size, suffix)
13
14
15 def check_path(path):
16 stat = os.statvfs(path)
17 total = stat.f_bsize * stat.f_blocks
18 free = stat.f_bsize * stat.f_bavail
19 warn = False
20
21 if total < 5*1024*1024*1024:
22 if free < total * 0.05:
23 warn = True
24 elif free < 2*1024*1024*1024:
25 warn = True
26
27 if warn:
28 print("WARNING! %s has only %s remaining" % (path, pp_size(free)))
29
30 def read_fstab():
31 for line in open("/etc/fstab"):
32 if line.startswith("#"):
33 continue
34 _, path, _ = line.split(maxsplit=2)
35 if path.startswith("/"):
36 yield path
37
38 def main():
39 paths = set(["/"])
40 paths.update(read_fstab())
41 for path in paths:
42 check_path(path)
43
44 if __name__ == "__main__":
45 main()
46