Created
May 10, 2010 12:04
-
-
Save olore/395973 to your computer and use it in GitHub Desktop.
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
#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