]> code.delx.au - monosys/blob - healthcheck/disk-usage
healthcheck more fixes
[monosys] / healthcheck / disk-usage
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 return False
30
31 return True
32
33 def read_fstab():
34 for line in open("/etc/fstab"):
35 if line.startswith("#") or not line.strip():
36 continue
37 _, path, _ = line.split(maxsplit=2)
38 if path.startswith("/"):
39 yield path
40
41 def main():
42 paths = set(["/"])
43 paths.update(read_fstab())
44 ok = True
45 for path in paths:
46 ok = ok and check_path(path)
47
48 if not ok:
49 sys.exit(1)
50
51 if __name__ == "__main__":
52 main()
53