Webfaction Memory Usage Script.
Posted by Richard Cooper | Filed under Random
This is a nice little script that I found on the Webfaction forums. It will let you know how much memory you are using so that you can avoid going over your limit. The advantage of this script is that it will discount the per-user Apache threads from the main Webfaction Apache instance, which are not counted towards your memory usage.
#!/usr/local/bin/python2.5 """ mem.py - A script which calculates, formats, and displays a customer's memory usage """ import subprocess import sys CMD = "ps -o rss,command -u %s | grep -v peruser | awk '{sum += $1} END {print sum / 1024}'" MEM = {} def main(): proc = subprocess.Popen('groups', shell=True, stdout=subprocess.PIPE) proc.wait() stdout = proc.stdout.read() for user in stdout.split(): proc = subprocess.Popen(CMD % user, shell=True, stdout=subprocess.PIPE) proc.wait() MEM[user] = int(float(proc.stdout.read())) print print 'Total Memory Usage: %i MB' % sum(MEM.values()) print for user in sorted(MEM.keys()): print user.ljust(15), str(MEM[user]).rjust(3), 'MB' print print 'Note: "Total Memory Usage" is only valid when you execute mem using your\ account\'s primary SSH user.' print if __name__ == '__main__': main()
Download it here copy it to your server, rename it to mem.py and make it executable.
mv mem.txt mem.py chmod +x mem.py
You can then call it using:
python2.5 ./mem.py