#!/usr/bin/env python3 import emails import os import reports table_data=[ ['Name', 'Amount', 'Value'], ['elderberries', 10, 0.45], ['figs', 5, 3], ['apples', 4, 2.75], ['durians', 1, 25], ['bananas', 5, 1.99], ['cherries', 23, 5.80], ['grapes', 13, 2.48], ['kiwi', 4, 0.49]] reports.generate("/tmp/report.pdf", "A Complete Inventory of My Fruit", "This is all my fruit.", table_data) sender = "*****@*****.**" receiver = "{}@example.com".format(os.environ.get('USER')) subject = "List of Fruits" body = "Hi\n\nI'm sending an attachment with all my fruit." message = emails.generate(sender, receiver, subject, body, "/tmp/report.pdf") emails.send(message)
# Check Available memory available_memory = psutil.virtual_memory().available / (1024 * 1024) # Check localhost localhost = socket.gethostbyname('localhost') # Do the checks error = False if usage > 80: message = "CPU usage is over 80%" error = True if free_space < 20: message = "Available disk space is less than 20%" error = True if available_memory < 500: message = "Available memory is less than 500MB" error = True if localhost != '127.0.0.1': message = "localhost cannot be resolved to 127.0.0.1" error = True if error is True: sender = "*****@*****.**" recipient = "{}@example.com".format(os.environ.get('USER')) subject = "Error - " + message body = "Please check your system and resolve the issue as soon as possible." email = emails.generate_no_attachment(sender, recipient, subject, body) emails.send(email)
#!/usr/bin/env python3 import emails import os import reports table_data = [['Name', 'Amount', 'Value'], ['elderberries', 10, 0.45], ['figs', 5, 3], ['apples', 4, 2.75], ['durians', 1, 25], ['bananas', 5, 1.99], ['cherries', 23, 5.80], ['grapes', 13, 2.48], ['kiwi', 4, 0.49]] reports.generate("report.pdf", "A Complete Inventory of My Fruit", "This is all my fruit.", table_data) """ sender = "*****@*****.**" receiver = "{}@example.com".format(os.environ.get('USER')) subject = "List of Fruits" body = "Hi\n\nI'm sending an attachment with all my fruit." message = emails.generate(sender, receiver, subject, body, "/tmp/report.pdf") emails.send(message) """
import socket import psutil import emails sender = '*****@*****.**' recipient = '*****@*****.**' body = 'Please check your system and resolve the issue as soon as possible.' cpu = psutil.cpu_percent() mem = psutil.virtual_memory().available disk = psutil.disk_usage('/').percent # CPU usage is over 80% - Error - CPU usage is over 80% if cpu >= 80: subject = 'Error - CPU usage is over 80%' emails.send(emails.generate_email(sender, recipient, subject, body)) # available memory is less than 500MB - Error - Available memory is less than 500MB if mem <= 500 * 1024 * 1024: subject = 'Error - Available memory is less than 500MB' emails.send(emails.generate_email(sender, recipient, subject, body)) # Available disk space is lower than 20% - Error - Available disk space is less than 20% if disk >= 80: subject = 'Error - Available disk space is less than 20%' emails.send(emails.generate_email(sender, recipient, subject, body)) # hostname "localhost" cannot be resolved to "127.0.0.1" - Error - localhost cannot be resolved to 127.0.0.1 try: socket.gethostbyname('localhost') except socket.error:
#!/usr/bin/env python3 import os import reports import datetime import emails def contents(dir): dir = "supplier-data/descriptions/" list_files = os.listdir(dir) paragraph ="" for files in list_files: if files.endswith(".txt"): with open(dir + files) as text_file: contents = text_file.read().split("\n") format = { "name": contents[0].strip(), "weight": contents[1].strip()} paragraph+= "name: " + format["name"] + "<br/" + "weight: " + format["weight"] + "<br/><br/>" return paragraph if __name__ == "__main__": dir = "supplier-data/descriptions/" #generate title with today's date title ="Processed Update on {}".format(datetime.date.today()) #generate report paragraph = contents(dir) reports.generate("/tmp/processed.pdf", title, paragraph) #generate and send email emails.generate(From, To, Subject line, E-mail Body, Attachment) gen_email = emails.generate("*****@*****.**", "*****@*****.**", "Upload Completed- Online Fruit Store", "All fruits are uploaded to our website successfully. A detailed list is attached to this email.", "/tmp/processed.pdf") emails.send(gen_email)
from datetime import datetime, date import os import emails import reports def getfruitlist(): data_folder = os.path.join(os.path.expanduser("~"), "supplier-data/descriptions") list_of_files = os.listdir(data_folder) data = "" for file in list_of_files: if file.startswith("."): continue with open(os.path.join(data_folder, file), "r") as current_file: name = current_file.readline().strip() weight = int(current_file.readline().strip()) data = data + name + "<br/>" + weight + "<br/>" + "<br/>" return data if __name__ == "__main__": filename = "processed.pdf" title = "Processed Update on {}".format(date.today().strftime("%B %d, %Y")) data = getfruitlist() reports.generate(filename,title,data) msg = emails.generate("*****@*****.**", "<user>@example.com", "Upload Completed - Online Fruit Store", "All fruits are uploaded to our website successfully. A detailed list is attached to this email.", "processed.pdf") emails.send(msg)
import os from psutil import cpu_percent, virtual_memory from shutil import disk_usage from emails import generate_no_attachment, send du = disk_usage('/') free_percent = du.free / du.total * 100 cpu = cpu_percent(1) mem = virtual_memory() mem_limit = 500 * 1024 * 1024 body = 'Please check your system and resolve the issue as soon as possible.' if mem.available < mem_limit: message = generate_no_attachment( '*****@*****.**', '<user>@example.com', 'Error - Available memory is less than 500MB', body) send(message) if free_percent < 20: message = generate_no_attachment( '*****@*****.**', '<user>@example.com', 'Error - Available disk space is less than 20%', body) send(message) if cpu > 80: message = generate_no_attachment('*****@*****.**', '<user>@example.com', 'Error - CPU usage is over 80%', body) send(message) response = os.system("ping -c 1 " + 'localhost') if response != 0: message = generate_no_attachment( '*****@*****.**', '<user>@example.com', 'Error - localhost cannot be resolved to 127.0.0.1', body)
import os import datetime import reports import emails source_text_folder = os.path.abspath('./supplier-data/descriptions') files = os.listdir(source_text_folder) data_list = [] for file in files: with open(os.path.join(source_text_folder, file), 'r') as f: name = f.readline().strip() weight = f.readline().strip().split()[0] + ' lbs' f.close() data_list.append('name: {}<br/>weight: {}'.format(name, weight)) pdf_header = 'Processed Update on {}'.format( datetime.date.today().strftime('%B %d, %Y')) pdf_body = '<br/><br/>'.join(data_list) if __name__ == '__main__': reports.generate_report('/tmp/processed.pdf', pdf_header, pdf_body) sender = '*****@*****.**' recipient = '*****@*****.**' subject = 'Upload Completed - Online Fruit Store' body = 'All fruits are uploaded to our website successfully. A detailed list is attached to this email.' attachment_path = '/tmp/processed.pdf' emails.send( emails.generate_email(sender, recipient, subject, body, attachment_path))
cpu, memory, disk, localhost_ip = datas.gather() result = datas.evaluate(cpu, memory, disk, localhost_ip) try: mail_file = sys.argv[1] credentials = [] now = datetime.datetime.now().ctime() with open(mail_file, 'r') as f: # save credentials in a list, split them by newline and ignore the # last empty string then save them to credentials = f.read().split('\n')[:-1] server, port, login, password, recipient = credentials f.close() if result != 0 and result != 1: subject = "Monitor PC Alert" body = now + " - " + result message = emails.generate(login, recipient, subject, body) emails.send(message, server, port, login, password) elif result == 1: print("{} - Error, something went wrong".format(now)) else: print("{} - Everything is OK".format(now)) except Exception as e: raise e