-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiskusage.py
More file actions
44 lines (39 loc) · 1.34 KB
/
diskusage.py
File metadata and controls
44 lines (39 loc) · 1.34 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
import shutil
import smtplib
from email.mime.text import MIMEText
import socket
from datetime import datetime
import subprocess
# Configuration
THRESHOLD = 80
EMAIL = "your_email@example.com"
SUBJECT = f"Disk Usage Alert on {socket.gethostname()}"
# Function to get disk usage info
def get_disk_usage():
partitions = []
df_output = subprocess.check_output(["df", "-h"]).decode("utf-8").splitlines()
for line in df_output:
if line.startswith("/dev/"):
parts = line.split()
usage_percent = int(parts[4].strip('%'))
mount_point = parts[5]
partitions.append((mount_point, usage_percent))
return partitions
# Function to send email
def send_email(subject, message, to_email):
msg = MIMEText(message)
msg["Subject"] = subject
msg["From"] = "alert@yourdomain.com"
msg["To"] = to_email
try:
# Adjust SMTP settings as per your environment
with smtplib.SMTP("localhost") as server:
server.send_message(msg)
except Exception as e:
print(f"Failed to send email: {e}")
# Main logic
for mount_point, usage in get_disk_usage():
if usage >= THRESHOLD:
message = f"Warning: Disk usage on {mount_point} has reached {usage}% on {socket.gethostname()} at {datetime.now()}"
print(message)
send_email(SUBJECT, message, EMAIL)