Login

Memcache Status

Author:
gfranxman
Posted:
February 28, 2007
Language:
Python
Version:
Pre .96
Score:
6 (after 6 ratings)

I use this script to keep track of the cache usage for various sites. It presently only supports memcached because that's all that I use, but leave a comment with patches for other systems and I'll add them.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import memcache
import re
import sys
from django.conf.settings import CACHE_BACKEND
#gfranxman

verbose = False

if not CACHE_BACKEND.startswith( 'memcached://' ):
    print "you are not configured to use memcched as your django cache backend"
else:
    m = re.search( r'//(.+:\d+)', CACHE_BACKEND )
    cache_host =  m.group(1)

    h = memcache._Host( cache_host )
    h.connect()
    h.send_cmd( 'stats' )

    stats = {}

    pat = re.compile( r'STAT (\w+) (\w+)' )

    l = '' ;
    while l.find( 'END' ) < 0 :
        l = h.readline()
        if verbose:
            print l
        m = pat.match( l )
        if m :
            stats[ m.group(1) ] =  m.group(2)


    h.close_socket()

    if verbose:
        print stats

    items = int( stats[ 'curr_items' ] )
    bytes = int( stats[ 'bytes' ] )
    limit_maxbytes = int( stats[ 'limit_maxbytes' ] ) or bytes
    current_conns = int( stats[ 'curr_connections' ] )

    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

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.