Skip to content

Instantly share code, notes, and snippets.

@matt-
Created Apr 3, 2013
Embed
What would you like to do?
#!/usr/bin/env ruby
require 'net/telnet'
require 'curses'
require 'optparse'
options = {}
options[:port] = 11211
options[:host] = 'localhost'
OptionParser.new do |opts|
opts.banner = "Usage: mem.rb [options]"
opts.on('-p', '--port PORT', 'Port') { |v| options[:port] = v }
opts.on('-h', '--host HOST', 'Port') { |v| options[:host] = v }
end.parse!
cache_dump_limit = 500
def init_screen
Curses.noecho # do not show typed keys
Curses.init_screen
Curses.stdscr.keypad(true) # enable arrow keys
begin
yield
ensure
Curses.close_screen
end
end
def write(line, text)
Curses.setpos(line, 0)
Curses.addstr(text);
end
localhost = Net::Telnet::new("Host" => options[:host], "Port" => options[:port], "Timeout" => 3)
init_screen do
loop do
Curses.clear
i=0
write(i+=1,"Expires At\t\t\t\tCache Key")
write(i+=1,'-'* 80)
slab_ids = []
localhost.cmd("String" => "stats items", "Match" => /^END/) do |c|
matches = c.scan(/STAT items:(\d+):/)
slab_ids += matches
end
slab_ids=slab_ids.flatten.uniq
slab_ids.each do |slab_id|
localhost.cmd("String" => "stats cachedump #{slab_id} #{cache_dump_limit}", "Match" => /^END/) do |c|
matches = c.scan(/^ITEM (.+?) \[(\d+) b; (\d+) s\]$/).each do |key_data|
(cache_key, bytes, expires_time) = key_data
humanized_expires_time = Time.at(expires_time.to_i).to_s
write(i+=1, "[#{humanized_expires_time}]\t#{cache_key}")
end
end
end
write(i+=1,"\n")
Curses.refresh
sleep 5
end
end
localhost.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment