When debugging memcache issues, being able to see the output of the stats command is invaluable. I got tired of manually connecting via telnet every time, so I wrote this little Ruby script to pull the statistics cleanly:
#! /usr/bin/env ruby
require 'socket'
socket = TCPSocket.open('localhost', '11211')
socket.send("stats\r\n", 0)
statistics = []
loop do
data = socket.recv(4096)
if !data || data.length == 0
break
end
statistics << data
if statistics.join.split(/\n/)[-1] =~ /END/
break
end
end
puts statistics.join()
It opens a raw TCP connection to the memcache daemon, sends stats, reads until it sees the END marker, and prints the result. Quick, simple, and saves you from typing telnet localhost 11211 for the hundredth time.