-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhealth_check.py
More file actions
47 lines (34 loc) · 1.23 KB
/
Copy pathhealth_check.py
File metadata and controls
47 lines (34 loc) · 1.23 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python3
import shutil, psutil, socket, emails, os
def check_cpu_usage():
usage = psutil.cpu_percent(1)
return usage <= 80
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_localhost():
localhost = socket.gethostbyname('localhost')
return localhost == '127.0.0.1'
def send_email(subject):
sender = "automation@example.com"
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)
emails.send_email(message)
if not check_cpu_usage():
subject = "Error - CPU usage is over 80%"
send_email(subject)
if not check_disk_usage('/'):
subject = "Error - Available disk space is less than 20%"
send_email(subject)
if not check_memory_usage():
subject = "Error - Available memory is less than 500MB"
send_email(subject)
if not check_localhost():
subject = "Error - localhost cannot be resolved to 127.0.0.1"
send_email(subject)