-
Notifications
You must be signed in to change notification settings - Fork 0
/
heath_check.py
56 lines (44 loc) · 1.58 KB
/
heath_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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#! /usr/bin/env python3
import os
import shutil
import psutil
import socket
from emails import generate_error_report, send_email
def check_cpu_usage():
"""Verifies that there's enough unused CPU"""
usage = psutil.cpu_percent(1)
return usage > 80
def check_disk_usage(disk):
"""Verifies that there's enough free space on disk"""
du = shutil.disk_usage(disk)
free = du.free / du.total * 100
return free > 20
def check_available_memory():
"""available memory in linux-instance, in byte"""
available_memory = psutil.virtual_memory().available / (1024 * 1024)
return available_memory > 500
def check_localhost():
"""check localhost is correctly configured on 127.0.0.1"""
localhost = socket.gethostbyname('localhost')
return localhost == '127.0.0.1'
if check_cpu_usage():
error_message = "CPU usage is over 80%"
elif not check_disk_usage('/'):
error_message = "Available disk space is less than 20%"
elif not check_available_memory():
error_message = "Available memory is less than 500MB"
elif not check_localhost():
error_message = "localhost cannot be resolved to 127.0.0.1"
else:
pass
# send email if any error reported
if __name__ == "__main__":
try:
sender = "[email protected]"
receiver = "{}@example.com".format(os.environ.get('USER'))
subject = "Error - {}".format(error_message)
body = "Please check your system and resolve the issue as soon as possible"
message = generate_error_report(sender, receiver, subject, body)
send_email(message)
except NameError:
pass