-
Notifications
You must be signed in to change notification settings - Fork 0
/
disk_monitor.py
58 lines (53 loc) · 2.29 KB
/
disk_monitor.py
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
49
50
51
52
53
54
55
56
57
58
import psutil
import asyncio
import platform
import logging
class DiskMonitor:
def __init__(self, discord_handler):
self.discord_handler = discord_handler
self.platform = platform.system()
self.drives_file = self.get_drives_file()
self.drives = {}
self.previous_states = {}
self.load_drives()
def get_drives_file(self):
if self.platform == 'Linux':
return 'drivesLinux.txt'
elif self.platform == 'Windows':
return 'drivesWindows.txt'
else:
logging.error(f"Unsupported platform: {self.platform}")
raise Exception(f"Unsupported platform: {self.platform}")
def load_drives(self):
try:
with open(self.drives_file, 'r') as f:
for line in f:
parts = line.strip().split()
if len(parts) == 2:
path, limit = parts
self.drives[path] = int(limit)
self.previous_states[path] = False
else:
logging.warning(f"Invalid line in {self.drives_file}: '{line.strip()}'")
logging.info(f"Loaded drives from {self.drives_file}")
except FileNotFoundError:
logging.error(f"Drives file not found: {self.drives_file}")
async def start(self):
while True:
await self.check_drives()
await asyncio.sleep(60) # Check every minute
async def check_drives(self):
for path, limit in self.drives.items():
try:
usage = psutil.disk_usage(path)
percent_used = usage.percent
over_limit = percent_used > limit
if over_limit and not self.previous_states[path]:
self.previous_states[path] = True
message = f"Disk usage on '{path}' is above {limit}% ({percent_used:.2f}%)."
await self.discord_handler.send_message(message)
logging.info(f"Disk usage on '{path}' exceeded limit ({percent_used:.2f}% used)")
elif not over_limit and self.previous_states[path]:
self.previous_states[path] = False
except Exception as e:
logging.error(f"Error checking disk {path}: {e}")