Sha256: 1a51499d9b1160269018afea8ce5f2a44325309ef537c8fc703c5ceade7444ed
Contents?: true
Size: 1.23 KB
Versions: 7
Compression:
Stored size: 1.23 KB
Contents
#!/usr/bin/env ruby require 'optparse' require 'ostruct' require 'socket' @options = OpenStruct.new @options.hostname = 'localhost' @options.port = 11211 op = OptionParser.new do |opts| opts.banner = "View memcached server statistics\nUsage: #{$0} [options]" opts.separator "General Options:" opts.on("-h HOSTNAME", "--hostname=HOSTNAME", "Hostname [default: localhost]") do |h| @options.hostname = h end opts.on("-p PORT", "--port=PORT", Integer, "Port [default: 11211]") do |p| @options.port = p end opts.on_tail("--help", "Show this message") do puts opts exit end end op.parse! def stats_data data = '' sock = TCPSocket.new(@options.hostname, @options.port) sock.print("stats\r\n") sock.flush # memcached does not close the socket once it is done writing # the stats data. We need to read line by line until we detect # the END line and then stop/close on our side. stats = sock.gets while true data += stats break if stats.strip == 'END' stats = sock.gets end sock.close data end def parse(stats_data) stats = [] stats_data.each_line do |line| stats << "#{$1}: #{$2}" if line =~ /STAT (\w+) (\S+)/ end stats.sort end stats = parse(stats_data) stats.each do |stat| puts stat end
Version data entries
7 entries across 7 versions & 2 rubygems