Permalink
Browse files

Add memcached_top binary

  • Loading branch information...
1 parent 5350a1c commit 9338d2dbbee9acb6b71f7e01e681445f1b2966e5 @mperham committed Mar 5, 2010
Showing with 66 additions and 3 deletions.
  1. +1 −0 History.rdoc
  2. +1 −0 Rakefile
  3. +56 −0 bin/memcached_top
  4. +8 −3 memcache-client.gemspec
View
1 History.rdoc
@@ -2,6 +2,7 @@
* Add support for EventMachine-based connections.
* Add support for raw values in get_multi
+ * Add memcached_top binary for gathering server statistics
= 1.7.8 (2010-02-03)
View
1 Rakefile
@@ -14,6 +14,7 @@ begin
s.has_rdoc = true
s.files = FileList["[A-Z]*", "{lib,test}/**/*", 'performance.txt']
s.test_files = FileList["test/test_*.rb"]
+ s.executables = ['memcached_top']
end
Jeweler::GemcutterTasks.new
rescue LoadError
View
56 bin/memcached_top
@@ -0,0 +1,56 @@
+#!/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
View
11 memcache-client.gemspec
@@ -5,13 +5,15 @@
Gem::Specification.new do |s|
s.name = %q{memcache-client}
- s.version = "1.7.8"
+ s.version = "1.8.0"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Eric Hodel", "Robert Cottrell", "Mike Perham"]
- s.date = %q{2010-02-03}
+ s.date = %q{2010-03-05}
+ s.default_executable = %q{memcached_top}
s.description = %q{A Ruby library for accessing memcached.}
s.email = %q{mperham@gmail.com}
+ s.executables = ["memcached_top"]
s.extra_rdoc_files = [
"LICENSE.txt",
"README.rdoc"
@@ -25,18 +27,21 @@ Gem::Specification.new do |s|
"VERSION.yml",
"lib/continuum_native.rb",
"lib/memcache.rb",
+ "lib/memcache/event_machine.rb",
"lib/memcache_util.rb",
"performance.txt",
"test/test_benchmark.rb",
+ "test/test_event_machine.rb",
"test/test_mem_cache.rb"
]
s.homepage = %q{http://github.com/mperham/memcache-client}
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
- s.rubygems_version = %q{1.3.5}
+ s.rubygems_version = %q{1.3.6}
s.summary = %q{A Ruby library for accessing memcached.}
s.test_files = [
"test/test_benchmark.rb",
+ "test/test_event_machine.rb",
"test/test_mem_cache.rb"
]

5 comments on commit 9338d2d

@mperham
Owner

Neat, I hadn't seen that before. My script is designed to make it easy to integrate memcached stats into Cacti/Nagios/SNMP-type systems where you can turn key:value pairs into graphs.

@defunkt

Just thought the similar names might be confusing.

@mperham
Owner

I named it (and stole it) from the starling_top script in starling.

@jnewland

Both of these versions memcached-top are new to me. Nice stuff.

I'm using Scout to monitor lotsa memcached servers:

http://github.com/jnewland/scout-plugins/blob/master/memcached_stats/memcached_stats.rb

Please sign in to comment.