Skip to content

Instantly share code, notes, and snippets.

@carobaldino
Last active August 27, 2022 16:12
Show Gist options
  • Save carobaldino/2bd7a65263af9a4ca81419c124eba94e to your computer and use it in GitHub Desktop.
Save carobaldino/2bd7a65263af9a4ca81419c124eba94e to your computer and use it in GitHub Desktop.
Python check file system usage
#!/usr/bin/python
import os;
import yagmail;
## functions
def fs_data_raw():
df_raw = os.popen("df -h --output=source,pcent,target | awk 'NR>1'").read().strip().split("\n")
return df_raw
def check_threshold(string, limit):
src, usg_raw, mounted_on = string.split()
usg = int(usg_raw.replace('%', '')) #remove trailing % and convert to int
if (usg >= limit):
message = f"File System {src} mounted on {mounted_on} is above threshold: {usg}% used"
print(message) #log
email_content.append(message)
def send_email(e_to, e_from, e_subject, e_content):
email = yagmail.SMTP(e_from, oauth2_file="~/oauth_client_secret.json")
if (logs_exists()):
email.send(
to = e_to,
subject = e_subject,
contents = e_content,
)
else:
print("No file system have reached the threshold")
def logs_exists():
return len(email_content) > 1
## main
threshold = 50
email_to = "carobaldino@frba.utn.edu.ar"
email_from = "caro.baldino@gmail.com"
email_subject = "Testing Python XX"
email_content = [f'The following file systems have reached the threshold set of {threshold}%:'] #First line of the email
for row in fs_data_raw():
check_threshold(row, threshold)
send_email(email_to, email_from, email_subject, email_content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment