Skip to content

Instantly share code, notes, and snippets.

@olore
Created May 10, 2010 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olore/395973 to your computer and use it in GitHub Desktop.
Save olore/395973 to your computer and use it in GitHub Desktop.
#A tool for displaying memcached key value pairs
#Inspired by: http://code.sixapart.com/svn/memcached/trunk/server/scripts/memcached-tool
#See also: http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt
require 'socket'
require 'getoptlong'
@host = "localhost"
@sock = TCPSocket.open(@host, 11211)
@buckets = []
def get_opts
opts = GetoptLong.new(
[ '--host', '-h', GetoptLong::REQUIRED_ARGUMENT ],
[ '--key', '-k', GetoptLong::REQUIRED_ARGUMENT ]
)
opts.each do |opt, arg|
case opt
when '--host'
@host = arg
when '--key'
@key = arg
end
end
end
def main
get_opts
return get_value(@key) if @key
get_buckets
get_bucket_keys
@sock.close
@buckets.each do |bucket|
puts "Bucket #{bucket[:number]}"
bucket[:keys].each do |key|
puts "\t#{key}"
end
end
end
def get_buckets
@sock.puts "stats items"
while (line = @sock.gets) !~ /END/
line.chop!
#STAT items:4:number 2
if line =~ /number/
line[/STAT items\:(\d*):number (\d*)/]
bucket = $1
number = $2
@buckets << {:number => bucket, :size => number}
end
end
end
def get_bucket_keys
@buckets.each do |bucket|
num = bucket[:number]
size = bucket[:size]
puts "running stats cachedump #{num} #{size}"
@sock.puts "stats cachedump #{num} #{size}"
keys = []
while (line = @sock.gets) !~ /END/
line.chop!
#ITEM foo [6 b; 123412351 s]
if line =~ /^ITEM/
line[/ITEM (\S+) \[.* (\d+) s\]/]
keys << $1
end
bucket[:keys] = keys
end
end
end
def get_value(key)
@sock.puts "get #{key}\r\n"
while (line = @sock.gets) !~ /END/
puts line
#line[/VALUE (\S+) (\d+) (\d+)/]
#length = $3
end
end
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment