-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisk_usage.py
More file actions
48 lines (42 loc) · 1.31 KB
/
disk_usage.py
File metadata and controls
48 lines (42 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python3
import os
import psutil
import sys
import socket
def check_reboot():
"""Check if a reboot is required."""
return os.path.exists('/run/reboot-required') or os.path.exists('/var/run/reboot-required')
def check_disk_usage(disk, min_gb, min_percentage):
"""Check if there is enough free disk space."""
disk_usage = psutil.disk_usage(disk)
min_bytes = min_gb * 1024 * 1024 * 1024
if disk_usage.free < min_bytes or disk_usage.percent > (100 - min_percentage):
return True
return False
def check_root_full():
"""Check if the root partition is full."""
return check_disk_usage(os.path.abspath(os.sep), 2, 10)
def check_no_network():
"""Check if there is no network connectivity."""
try:
socket.create_connection(("www.google.com", 80))
return False
except:
return True
def main():
check = [
(check_reboot, "Reboot is required"),
(check_root_full, "Root partition is full."),
(check_no_network, "No network connectivity.")
]
every_check = True
for func, msg in check:
if func():
print(msg)
every_check = False
if every_check:
print("Everything is within the acceptable range.")
sys.exit(1)
print("Everything is good.")
sys.exit(0)
main()