Skip to content

Instantly share code, notes, and snippets.

@samuel
Created March 23, 2011 00:56
Show Gist options
  • Save samuel/882430 to your computer and use it in GitHub Desktop.
Save samuel/882430 to your computer and use it in GitHub Desktop.
Memcached introspection
#!/usr/bin/env python
from __future__ import division
import socket
class LineSocket(object):
def connect(self, host, port):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((host, port))
self.buffer = ""
def close(self):
self.sock.close()
self.sock = None
def sendline(self, line):
self.sock.sendall(line+"\r\n")
def recvline(self):
while True:
if "\n" in self.buffer:
ret, self.buffer = self.buffer.split("\n", 1)
return ret.strip()
self.buffer += self.sock.recv(1024*10)
def __iter__(self):
return self
def next(self):
return self.recvline()
# def dump(hosts):
# sock = LineSocket()
# sock.connect(host, port)
# sock.sendline("stats items")
#
# items = {}
# totalitems = 0
# for line in sock:
# if line == "END":
# break
# if "number" in line:
# bucket = line.split(':')[1]
# count = int(line.split(' ')[-1])
# totalitems += count
# items[bucket] = count
#
# for bucket, count in items.items():
# sock.sendline("stats cachedump %s %s 1" % (bucket, count))
# for line in sock:
# if line == "END":
# break
# key = line.split(' ')[1]
# print key
#
# sock.close()
def get_stats(host, port):
sock = LineSocket()
sock.connect(host, port)
stats = {"items": {}}
items = stats["items"]
for cmd in ("stats items", "stats slabs"):
sock.sendline(cmd)
for line in sock:
if line == "END":
break
if ':' not in line:
name, value = line.split(' ')[1:]
stats[name] = int(value)
else:
line = line.split(' ')
bucket, stat = line[1].split(':')[-2:]
value = int(line[-1])
items.setdefault(int(bucket), {})[stat] = value
sock.sendline("stats")
for line in sock:
if line == "END":
break
line = line.split(" ")
if line[1] == "version":
stats[line[1]] = line[2]
else:
stats[line[1]] = float(line[2]) if "." in line[2] else int(line[2])
sock.close()
return stats
def main(hosts):
total_pages = 0
total_bytes = 0
total_items = 0
for host in hosts:
host, port = (host + ":11211").split(":")[:2]
stats = get_stats(host, int(port))
# import pprint
# pprint.pprint(stats)
# print
for bucket, info in stats["items"].items():
total_pages += info["total_pages"]
total_bytes += stats["bytes"]
total_items += stats["curr_items"]
print "total pages: %.2f GB" % (total_pages / 1024)
print "total bytes: %.2f GB" % (total_bytes / (1024*1024*1024))
print "total items:", total_items
if __name__ == "__main__":
import sys
hosts = sys.argv[1].split(",")
main(hosts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment