Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding example to parse memory usage #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions examples/example_memory_usage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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']