Webfaction Memory Usage Script.

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

2 Responses to “Webfaction Memory Usage Script.”

  1. Natan Yellin Says:
    January 17th, 2011 at 19:23

    Thanks for the script. I’ve been using this for a few months now.

  2. Richard Cooper Says:
    January 27th, 2011 at 16:26

    Thanks, Glad it’s of use… I saw the post on your blog about “bad interpreter: No such file or directory” and wondered if it was one of mine! :D

Leave a Reply




Switch to our mobile site