-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
179 lines (149 loc) · 5.74 KB
/
views.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import datetime
from django import http
from django.core.urlresolvers import reverse
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template.context import RequestContext
from django.core.cache import cache
"""
Aimed at extracting information about a memcached server and the keys available.
test_memcache extends functionalities provided in memcache.py
To know more about memcached visit http://memcached.org/
"""
def memcache_flush( request ):
if not (request.user.is_authenticated() and request.user.is_staff):
raise http.Http404
if request.method == 'POST':
try:
cache.clear()
except Exception:
pass
return HttpResponseRedirect( reverse( 'memcache_home' ) )
def memcache_showkeys(request):
"""
Displays all the keys and the size for the memcache server IP entered, takes by default 127.0.0.1:11211.
Requires user login with a staff status set true.
"""
if not (request.user.is_authenticated() and request.user.is_staff):
raise http.Http404
if request.method == "POST":
try:
import memcache
except ImportError:
raise http.Http404
connection_string = '127.0.0.1:11211'
host = memcache._Host(connection_string)
host.connect()
#getting all keys and size
keyno_dict = {}
host.send_cmd("stats items")
while 1:
line = host.readline().split(None, 2)
if line[0] == "END":
break
if 'number' in line[1]:
keyno = line[1].split(':')[1]
item_size = line[2]
keyno_dict[keyno] = item_size
key_size_dict = {}
for x,y in keyno_dict.items():
cmd = 'stats cachedump ' + x +' '+ y
host.send_cmd(cmd)
while 1:
temp = host.readline().split(None, 2)
if temp[0] == "END":
break
keyname = temp[1]
keysize = temp[2].split()[0].replace('[','')
try:
key_size_dict[keyname] = int(keysize)
except:
key_size_dict[keyname] = 0
host.close_socket()
import operator
soted_key_value_dict =sorted(key_size_dict.iteritems(), key=operator.itemgetter(1))
return render_to_response('show_keys.html', locals(), context_instance = RequestContext(request) )
else:
return HttpResponseRedirect(reverse(memcache_home))
def memcache_home(request):
"""
Enables user to enter IP address and port number for memcache server and returns the
cache status(information about the complete cache).
Requires user login with a staff status set true.
"""
if not (request.user.is_authenticated() and request.user.is_staff):
raise http.Http404
return render_to_response("memcache_home.html", context_instance = RequestContext(request) )
def memcache_keyinfo(request):
"""
Displays key status(cache info like cache_hits, memory usage..)
Requires user login with a staff status set true.
"""
if not (request.user.is_authenticated() and request.user.is_staff):
raise http.Http404
if request.method == 'POST':
if request.POST.get('key_name', None):
key = request.POST['key_name']
if cache.get(key, None):
data = cache.get(key)
key_in_cache = True
else:
key_in_cache = False
data = 'This key is not in cache.'
return render_to_response("key_info.html", locals(), context_instance = RequestContext(request))
else:
return HttpResponseRedirect(reverse(memcache_home))
else:
return HttpResponseRedirect(reverse(memcache_home))
def delete_key(request):
if not (request.user.is_authenticated() and request.user.is_staff):
raise http.Http404
if request.method == 'POST':
if request.POST.get('key_name', None):
key = request.POST['key_name']
if cache.get(key, None):
cache.delete(key)
return HttpResponseRedirect(reverse(memcache_home))
else:
return HttpResponseRedirect(reverse(memcache_home))
def server_statics(request):
if not (request.user.is_authenticated() and request.user.is_staff):
raise http.Http404
if request.method == 'POST':
try:
import memcache
except ImportError:
raise http.Http404
connection_string = '127.0.0.1:11211'
host = memcache._Host(connection_string)
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)
try:
stats=stats
hit_rate=100 * stats.get_hits / stats.cmd_get
time=datetime.datetime.now()
host.close_socket()
except :
host.close_socket()
return HttpResponseRedirect(reverse('memcache_home'))
return render_to_response('server_status.html', locals(), context_instance = RequestContext(request) )
else:
return HttpResponseRedirect(reverse(memcache_home))