Created
January 4, 2022 02:53
-
-
Save Kimjunkuk/3993fb188504bfb764717f8ff97224e6 to your computer and use it in GitHub Desktop.
Health check
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import socket | |
| import shutil | |
| import psutil | |
| import emails | |
| def check_localhost(): | |
| localhost=socket.gethostbyname('localhost') | |
| return localhost=="127.0.0.1" | |
| def check_disk_usage(disk): | |
| du=shutil.disk_usage(disk) | |
| free=du.free / du.total* 100 | |
| return free > 20 | |
| def check_memory_usage(): | |
| mu=psutil.virtual_memory().available | |
| total = mu/(1024.0 ** 2) | |
| return total > 500 | |
| def check_cpu_usage(): | |
| usage=psutil.cpu_percent(1) | |
| return usage<80 | |
| def send_email(subject): | |
| email = emails.generate_email("automation@example.com", "student-01-a8fcc4800a7c@example.com", subject,"Please check your system and resolve the issue as soon as possible.", "") #문제 | |
| emails.send_email(email) | |
| if not check_cpu_usage(): | |
| subject="Error - CPU usage is over 80%" | |
| print(subject) | |
| send_email(subject) #문제 | |
| if not check_memory_usage(): | |
| subject="Error - Available memory is less than 500MB" | |
| print(subject) | |
| if not check_disk_usage('/'): | |
| subject="Error - Available disk space is less than 20%" | |
| print(subject) | |
| send_email(subject) | |
| if not check_localhost(): | |
| subject="Error - localhost cannot be resolved to 127.0.0.1" | |
| print(subject) | |
| send_email(subject) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment