rvasilev
9/2/2010 - 4:07 PM

passenger_memory_stats.rb

#!/usr/bin/env ruby
# -*- coding: utf-8 -*-

# put in /etc/munin/plugins and restart munin-node
# by Dan Manges,
# http://www.dcmanges.com/blog/rails-application-visualization-with-munin

# NOTE: you might need to add munin to allow passwordless sudo for
# passenger-memory-stats

# 2010-09-02
# Modified by Manuel Vázquez Acosta
# Reports also total memory, used, and cached to get an 
# integrated view of the passenger memory consumption

def get_gen(what)
  status = `cat /proc/meminfo | grep ^#{what}`
  unless $?.success?
    return nil
  end
  status =~ /(\d+)/
  return $1.to_i * 1024
end

def get_memtotal
  return get_gen "MemTotal"
end

def get_cached
  return get_gen "Cached"
end

def get_free
  return get_gen "MemFree"
end

def get_used
  return get_memtotal() - get_free()
end
 
def output_config
  memtotal = get_memtotal
  puts <<-END
graph_category Applications
graph_title Passenger Memory Stats
graph_vlabel memory in MB
graph_args --base 1024 --lower-limit 0 --upper-limit #{memtotal}

memory.label memory
used.label used
cached.label cached
total.label total
END
  exit 0
end
 
def output_values
  status = `sudo /usr/bin/passenger-memory-stats | tail -1` 
  unless $?.success?
    $stderr.puts "failed executing passenger-memory-stats"
    exit 1
  end
  status =~ /(\d+\.\d+)/
  # Times 1024 since passenger-memory-stats reports MB
  puts "memory.value #{$1.to_i * 1024 * 1024}" 
  puts "used.value #{get_used}"
  puts "cached.value #{get_cached}"
  puts "total.value #{get_memtotal}"
end

if ARGV[0] == "config"
  output_config
else
  output_values
end