NewsBlur/utils/archive/memcached_status.py

49 lines
1.1 KiB
Python
Raw Normal View History

2010-02-05 17:30:59 -05:00
import re
import sys
2024-04-24 09:50:42 -04:00
import memcache
2010-02-05 17:30:59 -05:00
from settings import CACHE_BACKEND
2024-04-24 09:43:56 -04:00
# gfranxman
2010-02-05 17:30:59 -05:00
verbose = False
2024-04-24 09:43:56 -04:00
if not CACHE_BACKEND.startswith("memcached://"):
2020-06-19 02:29:40 -04:00
print("you are not configured to use memcched as your django cache backend")
2010-02-05 17:30:59 -05:00
else:
2024-04-24 09:43:56 -04:00
m = re.search(r"//(.+:\d+)", CACHE_BACKEND)
cache_host = m.group(1)
2010-02-05 17:30:59 -05:00
2024-04-24 09:43:56 -04:00
h = memcache._Host(cache_host)
2010-02-05 17:30:59 -05:00
h.connect()
2024-04-24 09:43:56 -04:00
h.send_cmd("stats")
2010-02-05 17:30:59 -05:00
stats = {}
2024-04-24 09:43:56 -04:00
pat = re.compile(r"STAT (\w+) (\w+)")
2010-02-05 17:30:59 -05:00
2024-04-24 09:43:56 -04:00
l = ""
while l.find("END") < 0:
2010-02-05 17:30:59 -05:00
l = h.readline()
if verbose:
2020-06-19 02:29:40 -04:00
print(l)
2024-04-24 09:43:56 -04:00
m = pat.match(l)
if m:
stats[m.group(1)] = m.group(2)
2010-02-05 17:30:59 -05:00
h.close_socket()
if verbose:
2020-06-19 02:29:40 -04:00
print(stats)
2010-02-05 17:30:59 -05:00
2024-04-24 09:43:56 -04:00
items = int(stats["curr_items"])
bytes = int(stats["bytes"])
limit_maxbytes = int(stats["limit_maxbytes"]) or bytes
current_conns = int(stats["curr_connections"])
2010-02-05 17:30:59 -05:00
2024-04-24 09:43:56 -04:00
print("MemCache status for %s" % (CACHE_BACKEND))
print("%d items using %d of %d" % (items, bytes, limit_maxbytes))
print("%5.2f%% full" % (100.0 * bytes / limit_maxbytes))
print("%d connections being handled" % (current_conns))
print()