├── .gitignore ├── README.md ├── setup.py └── src └── memcached_stats.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | *.swp 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # python-memcached-stats 2 | 3 | Python class to gather stats and slab keys from memcached via the memcached telnet interface 4 | 5 | ## Usage 6 | 7 | Basic usage: 8 | 9 | from memcached_stats import MemcachedStats 10 | mem = MemcachedStats() 11 | 12 | By default, it connects to localhost on port 11211. If you need to specify a host and/or port: 13 | 14 | mem = MemcachedStats('1.2.3.4', '11211') 15 | 16 | Retrieve a dict containing the current stats from memcached: 17 | 18 | >>> mem.stats() 19 | {'accepting_conns': '1', 20 | 'auth_cmds': '0', 21 | 'auth_errors': '0', 22 | ... } 23 | 24 | Retrieve a list of keys currently in use: 25 | 26 | >>> mem.keys() 27 | ['key-1', 28 | 'key-2', 29 | 'key-3', 30 | ... ] 31 | 32 | ## List the keys 33 | 34 | If you just want to list some of the keys in memcached, run this from the command line: 35 | 36 | python -m memcached_stats 37 | 38 | ip defaults to 127.0.0.1 and port defaults to 11211. 39 | 40 | ## Installation 41 | 42 | pip install 'git+git://github.com/dlrust/python-memcached-stats.git' 43 | 44 | ## License 45 | 46 | python-memcached-stats is released under the [MIT license](http://creativecommons.org/licenses/MIT/). 47 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | from distutils.core import setup 4 | 5 | setup( 6 | name='python-memcached-stats', 7 | version='0.1', 8 | description='Python class to gather stats and slab keys from memcached via the memcached telnet interface', 9 | author='Daniel Rust', 10 | url='http://github.com/dlrust/python-memcached-stats', 11 | package_dir={'': 'src'}, 12 | py_modules=[ 13 | 'memcached_stats', 14 | ], 15 | ) 16 | -------------------------------------------------------------------------------- /src/memcached_stats.py: -------------------------------------------------------------------------------- 1 | import re, telnetlib, sys 2 | 3 | class MemcachedStats: 4 | 5 | _client = None 6 | _key_regex = re.compile(r'ITEM (.*) \[(.*); (.*)\]') 7 | _slab_regex = re.compile(r'STAT items:(.*):number') 8 | _stat_regex = re.compile(r"STAT (.*) (.*)\r") 9 | 10 | def __init__(self, host='localhost', port='11211', timeout=None): 11 | self._host = host 12 | self._port = port 13 | self._timeout = timeout 14 | 15 | @property 16 | def client(self): 17 | if self._client is None: 18 | self._client = telnetlib.Telnet(self._host, self._port, 19 | self._timeout) 20 | return self._client 21 | 22 | def command(self, cmd): 23 | ' Write a command to telnet and return the response ' 24 | self.client.write(("%s\n" % cmd).encode('ascii')) 25 | return self.client.read_until(b'END').decode('ascii') 26 | 27 | def key_details(self, sort=True, limit=100): 28 | ' Return a list of tuples containing keys and details ' 29 | cmd = 'stats cachedump %s %s' 30 | keys = [key for id in self.slab_ids() 31 | for key in self._key_regex.findall(self.command(cmd % (id, limit)))] 32 | if sort: 33 | return sorted(keys) 34 | else: 35 | return keys 36 | 37 | def keys(self, sort=True, limit=100): 38 | ' Return a list of keys in use ' 39 | return [key[0] for key in self.key_details(sort=sort, limit=limit)] 40 | 41 | def slab_ids(self): 42 | ' Return a list of slab ids in use ' 43 | return self._slab_regex.findall(self.command('stats items')) 44 | 45 | def stats(self): 46 | ' Return a dict containing memcached stats ' 47 | return dict(self._stat_regex.findall(self.command('stats'))) 48 | 49 | def main(argv=None): 50 | if not argv: 51 | argv = sys.argv 52 | host = argv[1] if len(argv) >= 2 else '127.0.0.1' 53 | port = argv[2] if len(argv) >= 3 else '11211' 54 | import pprint 55 | m = MemcachedStats(host, port) 56 | pprint.pprint(m.keys()) 57 | 58 | if __name__ == '__main__': 59 | main() 60 | --------------------------------------------------------------------------------