Forked from dmitriykuptsov/gist:4f6faadbcd7b3415d202
Last active
August 29, 2015 14:21
-
-
Save msampathkumar/36ffc7a0d0651e6b137b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| import subprocess | |
| import re | |
| # Get process info | |
| ps = subprocess.Popen(['ps', '-caxm', '-orss,comm'], stdout=subprocess.PIPE).communicate()[0] | |
| vm = subprocess.Popen(['vm_stat'], stdout=subprocess.PIPE).communicate()[0] | |
| # Iterate processes | |
| processLines = ps.split('\n') | |
| sep = re.compile('[\s]+') | |
| rssTotal = 0 # kB | |
| for processLine in processLines: | |
| rowText = processLine.strip() | |
| rowElements = sep.split(rowText) | |
| try: | |
| rss = float(rowElements[0]) * 1024 | |
| except: | |
| rss = 0 # ignore... | |
| rssTotal += rss | |
| # Process vm_stat | |
| vmLines = vm.split('\n') | |
| sep = re.compile(':[\s]+') | |
| vmStats = {} | |
| for vmLine in vmLines[:-2]: | |
| rowText = vmLine.strip() | |
| rowElements = sep.split(rowText) | |
| vmStats[(rowElements[0])] = int(rowElements[1].strip('\.')) * 4096 | |
| order = ["Pages wired down", "Pages active", "Pages inactive", "Pages free"] | |
| for item in order: | |
| print "%s:\t\t%d MB" % ( item, vmStats[item]/1024/1024) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Beautifying with PEP Standards for Python