Skip to content

Instantly share code, notes, and snippets.

@taiansu
Forked from sxalexander/memusage.rb
Created October 7, 2011 14:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save taiansu/1270353 to your computer and use it in GitHub Desktop.
Save taiansu/1270353 to your computer and use it in GitHub Desktop.
WebFaction Memory Usage Script
#!/usr/bin/env ruby
# Memory usage script for WebFaction customers adapted to attempt to
# draw username from the pwd
# Nick Trew <vxnick@gmail.com>
# 2009-05-25
# START CONFIG #
require 'pathname'
# Put your username here
user = nil
# if no username is defined, attempt to grab it from the pwd
user ||= Pathname.pwd.dirname.to_s.split("/")[2]
puts "Calculating memory usage for:" + user
# Your memory limit (MB)
# This can be found at http://www.webfaction.com/services/hosting
# Enter "Application Memory" for whichever package you're on
memory_limit = 80
# END CONFIG #
# Get the process list and split at newlines
processes = `ps -u #{user} -o pid,rss,command`.split("\n")
# We'll keep a running total of each process' memory usage here
mem_count = 0.00
# Loop through the processes
processes.each_with_index do |line, index|
# Ignore the "PID RSS COMMAND" line
next if index == 0
# Split at the first space we encounter to separate the PID, RSS and COMMAND info
pid, rss, cmd = line.strip.sub(/\s+/, " ").split(/\s/, 3)
# Add to the running total
mem_count += rss.to_i
# Output the process' memory usage and the associated command
puts sprintf('PID:%d using %.2f MB: %s', pid.to_i, (rss.to_f) / 1024, cmd)
end
# Output the total memory usage for all the processes combined with the memory_limit specified above
puts sprintf('Total memory usage: %.2f MB / %d MB', (mem_count.to_f) / 1024, memory_limit)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment