-
Notifications
You must be signed in to change notification settings - Fork 0
/
mstat.py
68 lines (54 loc) · 1.65 KB
/
mstat.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# memcached_status.py
# from http://effbot.org/zone/django-memcached-view.htm
from django import http
from django.shortcuts import render_to_response
from django.conf import settings
import datetime, re
def view(request):
"""
Draw up a horrible little page shewing current memcache stats. 404s if memcache isn't installed.
"""
try:
import memcache
except ImportError:
raise http.Http404
if not (request.user.is_authenticated() and
request.user.is_staff):
raise http.Http404
# get first memcached URI
m = re.match(
"memcached://([.\w]+:\d+)", settings.CACHE_BACKEND
)
if not m:
raise http.Http404
host = memcache._Host(m.group(1))
host.connect()
host.send_cmd("stats")
class Stats:
pass
stats = Stats()
while 1:
line = host.readline().split(None, 2)
if line[0] == "END":
break
stat, key, value = line
try:
# convert to native type, if possible
value = int(value)
if key == "uptime":
value = datetime.timedelta(seconds=value)
elif key == "time":
value = datetime.datetime.fromtimestamp(value)
except ValueError:
pass
setattr(stats, key, value)
host.close_socket()
hit_rate = 0
if stats.cmd_get > 0:
hit_rate = 100 * stats.get_hits / stats.cmd_get
return render_to_response(
'memcached_status.html', dict(
stats=stats,
hit_rate=hit_rate,
time=datetime.datetime.now(), # server time
))