-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhealth_check.py
32 lines (22 loc) · 948 Bytes
/
health_check.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env python3
import shutil, psutil, emails, os, socket
def send_email(subject):
sender = "[email protected]"
receiver = "{}@example.com".format(os.environ.get('USER'))
body = "Please check your system and resolve the issue as soon as possible"
message = emails.generate_email(sender, receiver, subject, body)
# print(message)
emails.send_email(message)
cpu_percent = psutil.cpu_percent(interval=1)
disk_usage = shutil.disk_usage("/")
memory_usage = psutil.virtual_memory()
ip = socket.gethostbyname('localhost')
disk_percent_free = (disk_usage.free/disk_usage.total)*100
if ip != "127.0.0.1":
send_email("Error - localhost cannot be resolved to 127.0.0.1")
if disk_percent_free < 20:
send_email("Error - Available disk space is less than 20%")
if memory_usage.free/1000000 < 500:
send_email("Error - Available memory is less than 500MB")
if cpu_percent > 80:
send_email("Error - CPU usage is over 80%")