-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathexample_memory_usage.py
61 lines (46 loc) · 2.07 KB
/
example_memory_usage.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
"""Parses the output of "free -c 10 -s 1" """
import parsley
import subprocess
def parse_memory_file(payload):
"""Create a grammar that can parse the output of 'free' when called
with the -c option.
Return the results as a list of dictionaries of dictionaries """
memory_grammar = r"""
memory_reports = memory_report*
memory_report = headers_line mem_line:m buffers_line:b swap_line:s '\n'
-> dict(zip(['memory', 'buffers', 'swap'], [m, b, s]))
headers_line = ws 'total' ws 'used' ws 'free' ws 'shared' ws
'buffers' ws 'cached' '\n'
mem_line = 'Mem:' ws total:t ws used:u ws free:f ws shared:s ws
buffers:b ws cached:c '\n' -> dict([t, u, f, s, b, c])
buffers_line = '-/+ buffers/cache:' ws used:u ws free:f '\n'
-> dict([u, f])
swap_line = 'Swap:' ws total:t ws used:u ws free:f '\n'
-> dict([t, u, f])
num = <digit+>
total = num:n -> ('total', n)
used = num:n -> ('used', n)
free = num:n -> ('free', n)
shared = num:n -> ('shared', n)
buffers = num:n -> ('buffers', n)
cached = num:n -> ('cached', n)
"""
memory_parser = parsley.makeGrammar(memory_grammar, {})
memory_reports = memory_parser(payload).memory_reports()
return memory_reports
def get_memory_usage():
""" Call free and save the output to a file"""
with open('memory_usage.txt', 'w') as memory_file:
p = subprocess.Popen(['free', '-c', '5', '-s', '1'],
stdin=subprocess.PIPE,
stdout=memory_file,
stderr=memory_file)
p.communicate()
if __name__ == '__main__':
get_memory_usage()
with open('memory_usage.txt', 'r') as memory_file:
memory_reports = parse_memory_file(memory_file.read())
for memory_report in memory_reports:
print memory_report['memory']['free']
print memory_report['memory']['used']
print memory_report['swap']['used']