I'm attempting to write a program that will monitor the disk usage and send an e-mail if it exceeds the threshold. This is what I have so far:
#!/usr/bin/python
import os,psutil,smtplib
from email.mime.text import MIMEText
from subprocess import Popen, PIPE
THRESHOLD = 90
partitions = psutil.disk_partitions(all=True)
message = 'WARNING: DISK OVER ' + str(THRESHOLD) + '% FULL'
#for loop to iterate through disks and monitor usage
for p in partitions:
diskuse = (psutil.disk_usage(p.mountpoint).percent)
if psutil.disk_usage(p.mountpoint).percent >= THRESHOLD:
msg = MIMEText(message)
msg["From"] = "****@***.com"
msg["To"] = "****@***.com"
msg["Subject"] = "DISK(S) OVER THRESHOLD"
P = Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=PIPE,universal_newlines=True)
P.communicate(msg.as_string())
When I try to run the program as it is here, the email will be sent but rather than sending a text email, i receive a file named ATT00001. I have tried running the program without diskuse
and it worked the way I expected but I cannot figure out why it won't allow me to send both message
and diskuse