Skip to content

Instantly share code, notes, and snippets.

@timtrueman
Created September 9, 2010 18:27
Show Gist options
  • Save timtrueman/572282 to your computer and use it in GitHub Desktop.
Save timtrueman/572282 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
#encoding:utf-8
from telnetlib import Telnet
import time
import sys
"""
Usage
To get all values just call the script:
python memcached_stats.py
To get a single value specify a key:
python memcached_stats.py uptime
"""
def memcached_stats(key=None):
t = Telnet("127.0.0.1",11211)
t.open("127.0.0.1", 11211) # Not sure how to avoid giving the host/port twice, maybe I'm an idiot.
t.write("stats\n")
time.sleep(0.2)
stats = t.read_very_eager()
stats = stats.split("\r\n")
del stats[-1] # remove "" from the end so we can process without a special case
del stats[-1] # remove "END" from the end so we can process without a special case
new_stats = []
for stat in stats:
s = stat.split()
new_stats.append("%s: %s" % (s[1], s[2]))
if s[1] == key:
print s[2]
if key == None:
print ", ".join(new_stats)
if __name__ == '__main__':
if len(sys.argv) > 1:
memcached_stats(key=sys.argv[1])
else:
memcached_stats()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment