-
-
Save taiansu/1270353 to your computer and use it in GitHub Desktop.
WebFaction Memory Usage Script
This file contains 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/env ruby | |
# Memory usage script for WebFaction customers adapted to attempt to | |
# draw username from the pwd | |
# Nick Trew <[email protected]> | |
# 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