-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmem_monitor.py
executable file
·64 lines (53 loc) · 1.46 KB
/
mem_monitor.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
57
58
59
60
61
62
63
64
#!/usr/bin/env python
import sys
import re
import smtplib
from email.mime.text import MIMEText
from socket import gethostname
warn_ratio = float(sys.argv[1])
email_to = sys.argv[2:]
once_filename = "/tmp/mem_monitor-once.json"
def check():
pattern = re.compile("""privvmpages""" +
"""\s+(?P<held>\d+)""" +
"""\s+(?P<maxheld>\d+)""" +
"""\s+(?P<barrier>\d+)""" +
"""\s+(?P<limit>\d+)""" +
"""\s+(?P<failcnt>\d+)""")
with open("/proc/user_beancounters", "rb") as data:
match = pattern.search(data.read())
p = dict((k, int(v)) for k, v in match.groupdict().items())
ratio = p["maxheld"] / float(p["limit"])
if p["failcnt"]:
alert("fail", "Memory limit breached")
elif ratio >= warn_ratio:
alert("warn", "Memory %.0f%% full" % (ratio*100))
elif p["maxheld"] >= p["barrier"]:
alert("warn", "memory barier brached")
else:
alert("ok")
def alert(type, message=None):
try:
with open(once_filename, "rb") as once_file:
once = once_file.read()
except:
once = "ok"
if type == once:
return
if type in ["fail", "warn"]:
email(message)
with open(once_filename, "wb") as once_file:
once_file.write(type)
def email(message):
msg = MIMEText("\n".join([
message,
"",
"Host: " + gethostname()]))
email_from = "root@" + gethostname()
msg['Subject'] = "STUNNIMP: " + message
msg['From'] = email_from
msg['To'] = ', '.join(email_to)
s = smtplib.SMTP('localhost')
s.sendmail(email_from, email_to, msg.as_string())
s.quit()
check()