-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_load.py
executable file
·40 lines (31 loc) · 1.06 KB
/
check_load.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
#!/usr/bin/env python3
"""Check load and email alert if load is high."""
import multiprocessing
import platform
import smtplib
import six
def mail_alert(load):
"""Email alert if load is high."""
subject = "load on %s -> %.2f" % (platform.node(), load)
sender = "[email protected]"
receivers = "[email protected]"
message = "Subject: %s\r\nFrom: %s\r\nTo: %s\r\n\r\n" % (subject, sender, receivers)
try:
server = smtplib.SMTP("localhost")
server.sendmail(sender, receivers, message)
six.print_("Successfully sent email")
except smtplib.SMTPException:
six.print_("Error: unable to send email")
def check_load():
"""Check load."""
try:
with open("/proc/loadavg") as infile:
for line in infile:
stats = line.split()
load_avg = float(stats[2])
if load_avg > multiprocessing.cpu_count():
mail_alert(load_avg)
except IOError:
print("File", "/proc/loadavg", "not found")
if __name__ == "__main__":
check_load()