Skip to content

Instantly share code, notes, and snippets.

@RecNes
Last active June 20, 2018 08:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RecNes/08010b6020368985630e4df65af9c0f2 to your computer and use it in GitHub Desktop.
Save RecNes/08010b6020368985630e4df65af9c0f2 to your computer and use it in GitHub Desktop.
Disk space usage email alert
#!/usr/bin/env python
"""
Developen on: Python 2.7.13
"""
__author__ = 'Sencer Hamarat'
__license__ = "Creative Commons Attribution-ShareAlike 3.0 Unported License"
__version__ = "1.3"
__maintainer__ = "Sencer Hamarat"
__status__ = "Production"
import datetime
import os
import platform
import smtplib
import socket
import sys
import subprocess
from email.mime.text import MIMEText
# Reciepents list as string.
to = 'user1@example.com,user2@gmail.com,user3@yandex.com' # Email address(es) to send to.
# Sender email account settings:
# You can change below settings to your own account information.
smtp_address = 'smtp.gmail.com' # SMTP server address
smtp_port = 465 # SMTP server connection port
email_account = '' # Sender's account name. (You can skip this setting if account name and mail address are same.)
email_address = 'backup@gmail.com' # Sender's email address. (MUST BE GMAIL ACCOUNT Or you have to change server settings.)
email_password = "****************" # Email account password.
def get_machine_name():
cn = os.getenv('COMPUTERNAME')
pn = platform.node()
hn = socket.gethostname()
return cn if cn else pn if pn else hn
def prepare_mail(sublist):
msg_text = "Free space on the some partitions are low. Please take care.\r\n\r\nPartition Status:\r\n\r\n"
lines_to_send = [["Partition", "Mount Point", "Used Space"]] + [sublist]
col_width = max(len(word) for row in lines_to_send for word in row) + 4 # padding
for row in lines_to_send:
msg_text += "".join(word.ljust(col_width) for word in row)
msg_text += "\r\n"
msg = MIMEText(msg_text, "\n")
msg['Subject'] = 'Disk Usage Alert for %s on %s' % (get_machine_name(), datetime.date.today().strftime('%b %d %Y'))
msg['From'] = email_address
msg['To'] = to
return msg.as_string()
def send_as_mail(sublist):
smtp_server = smtplib.SMTP_SSL(smtp_address, smtp_port) # Server to use.
# smtp_server.ehlo() # Say 'hello' to the remote SMTP server
# smtp_server.starttls() # Start TLS encryption
smtp_server.ehlo() # Say 'hello' to the remote SMTP server
smtp_server.login(email_account if email_account else email_address, email_password) # Log in to server
smtp_server.sendmail(email_address, [to], prepare_mail(sublist)) # Send the message
smtp_server.quit() # Close the smtp server.
df_command = "df -h | grep '/dev/' | awk '{print $1,$6,$5}'" # Linux command to retrieve disk in use percent
proc_terminal = subprocess.Popen(df_command, shell=True, stdout=subprocess.PIPE) # Runs 'df_command' in a shell.
terminal_output = proc_terminal.communicate() # Receive output from 'proc_terminal'.
df_result = terminal_output[0].split() # Split terminal_output.
partition_list = [df_result[n:n+3] for n in range(0, len(df_result), 3)] # Create groups in 3 from fragmented text.
for item in partition_list:
percent = int(item[2].replace('%',''))
if percent >= 51:
send_as_mail(item)
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment