memcache-client-stats 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG ADDED
@@ -0,0 +1,4 @@
1
+ = 1.0
2
+ === 3rd July, 2006
3
+
4
+ * Initial checkin.
data/COPYING ADDED
@@ -0,0 +1,18 @@
1
+ Copyright (c) 2006 Layton Wedgeworth
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to
5
+ deal in the Software without restriction, including without limitation the
6
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7
+ sell copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16
+ THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,99 @@
1
+ == memcache-client-stats
2
+
3
+ Rubyforge Project:
4
+
5
+ http://rubyforge.org/projects/memcacheclientstats
6
+
7
+ == About
8
+
9
+ memcache-client-stats is an add on to memcache-client that adds the ability to query memcache server stats from the client.
10
+
11
+ == Installing memcache-client-stats
12
+
13
+ Just install the gem
14
+
15
+ $ sudo gem install memcache-client-stats
16
+
17
+ == Using memcache-client-stats
18
+
19
+ Create a new MemCache instance
20
+ >> require 'memcache_client_stats'
21
+ >> CACHE = MemCache.new :c_threshold => 10_000,
22
+ :compression => true,
23
+ :debug => false,
24
+ :namespace => 'my_namespace',
25
+ :readonly => false,
26
+ :urlencode => false
27
+ >> CACHE.servers = ['192.168.0.100:11211', '192.168.0.101:11211']
28
+
29
+
30
+ You can then query the cluster for the stats. You can request all the stats for all the servers in the memcache cluster:
31
+
32
+ >> CACHE.stats
33
+ => {"192.168.0.100:11211" =>
34
+ {"bytes" => "211986835",
35
+ "pid" => "1942",
36
+ "connection_structures" => "75",
37
+ "time" => "1151612629",
38
+ "limit_maxbytes" => "268435456",
39
+ "cmd_get" => "6484010",
40
+ "version" => "1.1.12",
41
+ "bytes_written" => "930827121",
42
+ "cmd_set" => "6274655",
43
+ "get_misses" => "1930031",
44
+ "total_connections" => "115",
45
+ "curr_connections" => "17",
46
+ "curr_items" => "1040445",
47
+ "uptime" => "2486978",
48
+ "get_hits" => "4553979",
49
+ "total_items" => "6274655",
50
+ "rusage_system" => "781.900865",
51
+ "rusage_user" => "392.372521",
52
+ "bytes_read" => "1476127655
53
+ },
54
+ "192.168.0.101:11211" =>
55
+ {"bytes" => "210778808",
56
+ "pid" => "9993",
57
+ "connection_structures" => "237",
58
+ "time" => "1151612630",
59
+ "limit_maxbytes" => "268435456",
60
+ "cmd_get" => "23683207",
61
+ "version" => "1.1.12",
62
+ "bytes_written" => "3407983178",
63
+ "cmd_set" => "22942962",
64
+ "get_misses" => "5708419",
65
+ "total_connections" => "1948",
66
+ "curr_connections" => "17",
67
+ "curr_items" => "1073593",
68
+ "uptime" => "5782293",
69
+ "get_hits" => "17974788",
70
+ "total_items" => "22942962",
71
+ "rusage_system" => "2279.002428",
72
+ "rusage_user" => "1287.636472",
73
+ "bytes_read" => "5160467749
74
+ }
75
+ }
76
+
77
+ Or you can request a single stat from all servers:
78
+
79
+ >> CACHE.stats('bytes')
80
+ => {"192.168.0.53:11211" = >"211985845", "192.168.0.50:11211" => "210776435"}
81
+
82
+ You can also have the client summarize the statistic across the cluster:
83
+
84
+ >> CACHE.summed_stat('curr_items')
85
+ => 2114039
86
+ >> CACHE.summed_stat('bytes') / 1.megabyte
87
+ => 403
88
+
89
+ === Using memcache-client-stats with Rails
90
+
91
+ memcache-client-stats includes the <tt>MemCache.load_config</tt> method that will automatically load the memcache configuration and initialize the client
92
+ based on a .yml configuration file (think database.yml for memcache-client). An example file is available at examples/memcache.yml.
93
+
94
+ You can configure Rails to use memcache-client by adding the following to config/environment.rb or config/environment/*.rb
95
+
96
+ require 'memcache_client_stats'
97
+ CACHE = MemCache.load_config("#{RAILS_ROOT}/config/memcache.yml")
98
+ config.action_controller.session_store = :mem_cache_store
99
+ ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS.merge! :cache => CACHE
data/Rakefile ADDED
@@ -0,0 +1,67 @@
1
+ require 'rake'
2
+ require 'rake/clean'
3
+ require 'rake/gempackagetask'
4
+ require 'rake/rdoctask'
5
+ require 'fileutils'
6
+ include FileUtils
7
+
8
+ NAME = "memcache-client-stats"
9
+ VERS = "1.0.0"
10
+ TITLE = "memcache-client-stats Documentation"
11
+ CLEAN.include ['**/.*.sw?', '*.gem', '.config']
12
+ RDOC_OPTS = ['--quiet', '--title', TITLE,
13
+ "--opname", "index.html",
14
+ "--line-numbers",
15
+ "--main", "README",
16
+ "--inline-source"]
17
+
18
+ desc "Packages up memcache-client-stats."
19
+ task :default => [:rdoc, :package]
20
+ task :package => [:clean]
21
+
22
+ task :doc => [:rdoc]
23
+
24
+ Rake::RDocTask.new do |rdoc|
25
+ rdoc.rdoc_dir = 'doc/rdoc'
26
+ rdoc.options += RDOC_OPTS
27
+ rdoc.main = "README"
28
+ rdoc.title = TITLE
29
+ rdoc.rdoc_files.add ['README', 'CHANGELOG', 'COPYING', 'lib/memcache_client_stats.rb', 'lib/memcache_config.rb',
30
+ 'lib/memcache_server.rb', 'lib/memcache_stats.rb']
31
+ end
32
+
33
+ spec =
34
+ Gem::Specification.new do |s|
35
+ s.name = NAME
36
+ s.version = VERS
37
+ s.platform = Gem::Platform::RUBY
38
+ s.has_rdoc = true
39
+ s.extra_rdoc_files = ["README", "CHANGELOG", "COPYING"]
40
+ s.rdoc_options += RDOC_OPTS + ['--exclude', '^(examples)\/']
41
+ s.summary = "Query MemCache server stats through memcache-client"
42
+ s.description = s.summary
43
+ s.author = "Layton Wedgeworth"
44
+
45
+ s.add_dependency('memcache-client')
46
+ s.required_ruby_version = '>= 1.8.2'
47
+
48
+ s.files = %w(COPYING README Rakefile) +
49
+ Dir.glob("{bin,doc,lib}/**/*") +
50
+ Dir.glob("examples/**/*.rb")
51
+
52
+ s.require_path = "lib"
53
+ end
54
+
55
+ Rake::GemPackageTask.new(spec) do |p|
56
+ p.need_tar = true
57
+ p.gem_spec = spec
58
+ end
59
+
60
+ task :install do
61
+ sh %{rake package}
62
+ sh %{sudo gem install pkg/#{NAME}-#{VERS}}
63
+ end
64
+
65
+ task :uninstall => [:clean] do
66
+ sh %{sudo gem uninstall #{NAME}}
67
+ end
@@ -0,0 +1,5 @@
1
+ require_gem 'memcache-client'
2
+
3
+ require 'memcache_config'
4
+ require 'memcache_server'
5
+ require 'memcache_stats'
@@ -0,0 +1,16 @@
1
+ class MemCache
2
+ # Loads the configuration from the given filename and creates a new MemCache
3
+ # instance that uses the options and servers from the configuration file.
4
+ #
5
+ # See examples/memcache.yml for an example configuration file.
6
+ def MemCache.load_config(filename = nil, environment = nil)
7
+ filename ||= "#{RAILS_ROOT}/config/memcache.yml"
8
+ environment ||= RAILS_ENV
9
+
10
+ config = YAML.load_file(filename)[environment]
11
+ instance = MemCache.new(config['options'])
12
+ instance.servers = config['servers']
13
+
14
+ instance
15
+ end
16
+ end
@@ -0,0 +1,40 @@
1
+ class MemCache
2
+ # Extends the MemCache::Server object so that is is able to request stats.
3
+ class Server
4
+ # Returns a unique key for the server.
5
+ def key
6
+ "#{host}:#{port}"
7
+ end
8
+
9
+ # Returns the open socket or raises a MemCache::MemCacheError if one is not available.
10
+ def socket!
11
+ socket or raise MemCacheError, "No connection to server"
12
+ end
13
+
14
+ # Returns a hash of <tt>{stat_key => value, ...}</tt> for all of the Server stats.
15
+ def stats
16
+ sock = socket!
17
+
18
+ stats = {}
19
+ begin
20
+ sock.write "stats\r\n"
21
+
22
+ # Loop through the status which are sent back in the format of:
23
+ # STAT <name> <value>\r\n
24
+ # and ends with
25
+ # END\r\n
26
+ while (text = sock.gets)
27
+ break if text =~ /^END/
28
+
29
+ s, name, value = text.split(/ /)
30
+ stats[name] = value[0..-3]
31
+ end
32
+ rescue SystemCallError, IOError => err
33
+ server.close
34
+ raise MemCacheError, err.message
35
+ end
36
+
37
+ stats
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,32 @@
1
+ class MemCache
2
+ # Creates a hash for the stat(s) for all of the active servers.
3
+ #
4
+ # If <tt>key</tt> is <tt>nil</tt> then the returned hash will be in the form of
5
+ # <tt>{server1 => {stat_key => value, ...}, server2 => {stat_key => value, ...}, ...}</tt>
6
+ # for all of the servers and stats.
7
+ #
8
+ # If <tt>key</tt> is not <tt>nil</tt> then the returned hash will be in the form of
9
+ # <tt>{server1 => value, server2 => value, ...}</tt> for all servers for the single requested stat.
10
+ def stats(key = nil)
11
+ stats = {}
12
+
13
+ @mutex.synchronize do
14
+ raise MemCacheError, "No active servers" unless self.active?
15
+ @servers.each {|server| stats[server.key] = server.stats}
16
+ end
17
+
18
+ unless key.nil?
19
+ new_stats = {}
20
+ stats.each_pair {|k,v| new_stats[k] = v[key]}
21
+ stats = new_stats
22
+ end
23
+
24
+ stats
25
+ end
26
+
27
+ # Returns a summed value for the requested stat. This is useful for determining things such
28
+ # as the global number of <tt>curr_items</tt> or <tt>bytes</tt>.
29
+ def summed_stat(key)
30
+ stats(key).values.inject(0) {|sum, value| sum + value.to_i}
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.11
3
+ specification_version: 1
4
+ name: memcache-client-stats
5
+ version: !ruby/object:Gem::Version
6
+ version: 1.0.0
7
+ date: 2006-07-03 00:00:00 -07:00
8
+ summary: Query MemCache server stats through memcache-client
9
+ require_paths:
10
+ - lib
11
+ email:
12
+ homepage:
13
+ rubyforge_project:
14
+ description: Query MemCache server stats through memcache-client
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.8.2
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ authors:
29
+ - Layton Wedgeworth
30
+ files:
31
+ - COPYING
32
+ - README
33
+ - Rakefile
34
+ - lib/memcache_stats.rb
35
+ - lib/memcache_client_stats.rb
36
+ - lib/memcache_config.rb
37
+ - lib/memcache_server.rb
38
+ - CHANGELOG
39
+ test_files: []
40
+
41
+ rdoc_options:
42
+ - --quiet
43
+ - --title
44
+ - memcache-client-stats Documentation
45
+ - --opname
46
+ - index.html
47
+ - --line-numbers
48
+ - --main
49
+ - README
50
+ - --inline-source
51
+ - --exclude
52
+ - ^(examples)\/
53
+ extra_rdoc_files:
54
+ - README
55
+ - CHANGELOG
56
+ - COPYING
57
+ executables: []
58
+
59
+ extensions: []
60
+
61
+ requirements: []
62
+
63
+ dependencies:
64
+ - !ruby/object:Gem::Dependency
65
+ name: memcache-client
66
+ version_requirement:
67
+ version_requirements: !ruby/object:Gem::Version::Requirement
68
+ requirements:
69
+ - - ">"
70
+ - !ruby/object:Gem::Version
71
+ version: 0.0.0
72
+ version: