-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_swap.py
executable file
·43 lines (34 loc) · 1.16 KB
/
check_swap.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
#!/usr/bin/env python3
"""Check swap usage & send alert if swap is high."""
import platform
import smtplib
import six
def mail_alert(percent):
"""Send alert if swap is high."""
subject = "swap on %s - %.2f used" % (platform.node(), percent)
sender = "from@fastmail.com"
receivers = "to@fastmail.com"
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_swap():
"""Check swap usage."""
THRESHOLD = 0.20
try:
with open("/proc/swaps") as infile:
next(infile)
for line in infile:
stats = line.split()
size = stats[2]
used = stats[3]
percent_used = float(used) / float(size)
if percent_used > THRESHOLD:
mail_alert(percent_used)
except IOError:
print("File", "/proc/swaps", "not found")
if __name__ == "__main__":
check_swap()