0

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

Jquinn
  • 1
  • 2
  • 2

1 Answers1

0

It's likely that the email you're trying to send contains special characters. When it contains special characters (such as \r) then it will change it to an attachment.

This stack overflow post talks quite a bit about that.

They claim that LANG="en_US.UTF8" ; export LANG will solve your problem.

Oliver I
  • 436
  • 1
  • 3
  • 12