def add_server(self, server): """ Adds a host at runtime to client """ # Uncomment this to protect the Client from adding a server in case # there's no reliable consistent hash algorithm such as MODULO """ if not self.consistent_hash_manager: raise Exception("The current consistent hash algorithm (\"%s\") is" " not reliable for adding a new server" "" % self.hash_algorithm) """ # Create a new host entry server = memcache._Host( server, self.debug, dead_retry=self.dead_retry, socket_timeout=self.socket_timeout, flush_on_reconnect=self.flush_on_reconnect ) # Add this to our server choices self.servers.append(server) """This for statement will ensure that a server with a bigger weight will have more copies into the buckets increasing it's probability to be retrieved. """ for i in range(server.weight): self.buckets.append(server) # Adds this node to the circle if self.consistent_hash_manager: self.consistent_hash_manager.add_node(server)
def _node_stats(host): """ Returns a dict with information about a given memcache instance """ host = memcache._Host(host) host.connect() host.send_cmd("stats") stats = {} while 1: line = host.readline().split(None, 2) if line[0] == "END": break _, 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 stats[key] = value host.close_socket() return stats
def _query_memcache_server(location): try: host = memcache._Host(location) host.connect() host.send_cmd("stats") stats = MemcachedStats() while True: line = host.readline().split(None, 2) if line[0] == "END": break stat, key, value = line try: value = int(value) if key == "uptime": value = timedelta(seconds=value) elif key == "time": value = datetime.fromtimestamp(value) except ValueError: pass setattr(stats, key, value) host.close_socket() return stats except Exception: return None
def add_server(self, server): """ Adds a host at runtime to client """ # Uncomment this to protect the Client from adding a server in case # there's no reliable consistent hash algorithm such as MODULO """ if not self.consistent_hash_manager: raise Exception("The current consistent hash algorithm (\"%s\") is" " not reliable for adding a new server" "" % self.hash_algorithm) """ # Create a new host entry server = memcache._Host(server, self.debug, dead_retry=self.dead_retry, socket_timeout=self.socket_timeout, flush_on_reconnect=self.flush_on_reconnect) # Add this to our server choices self.servers.append(server) """This for statement will ensure that a server with a bigger weight will have more copies into the buckets increasing it's probability to be retrieved. """ for i in range(server.weight): self.buckets.append(server) # Adds this node to the circle if self.consistent_hash_manager: self.consistent_hash_manager.add_node(server)
def get_memcached_stats(server): if not memcache_installed: return {} host = memcache._Host(server) host.connect() host.send_cmd("stats") stats = {} while True: try: stat, key, value = host.readline().split(None, 2) except ValueError: break 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 stats[key] = value host.close_socket() try: stats['hit_rate'] = 100 * stats['get_hits'] / stats['cmd_get'] except ZeroDivisionError: stats['hit_rate'] = stats['get_hits'] return stats
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 ))
def cache_view(request): """ Tries to import memcache, fails out if it can't and raises a 404. Also raises a 404 in the case of unauthenticated users, or memcache not being used. Graciously borrowed from: http://effbot.org/zone/django-memcached-view.htm """ 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() return render_to_response( 'memcached_status.html', dict( stats=stats, hit_rate=100 * stats.get_hits / stats.cmd_get, time=datetime.datetime.now(), # server time ))
def view(request): try: import memcache except ImportError: raise http.Http404 if not request.user.is_staff: raise http.Http404 # get first memcached URI mre = re.compile("memcached://([.\w]+:\d+)") m = mre.match(settings.CACHE_BACKEND) if not m: raise http.Http404 host = memcache._Host(m.group(1)) host.connect() # TODO: Chequear si está corriendo el servidor y correr el script de carga # TODO: de ser necesario. 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() try: hit_rate = 100 * stats.get_hits / stats.cmd_get except: hit_rate = 100 return render_to_response( 'memcached/memcached_status.html', dict( stats=stats, hit_rate=hit_rate, time=datetime.datetime.now(), # server time ))
def memcached_status(request): # http://effbot.org/zone/django-memcached-view.htm try: import memcache except ImportError: raise Http404 #if not (request.user.is_authenticated() and # request.user.is_staff): # raise Http404 # get first memcached URI m = re.match("([.\w]+:\d+)", settings.CACHES['default']['LOCATION']) if not m: raise 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() try: hit_rate = 100 * stats.get_hits / stats.cmd_get except: hit_rate = 0 return render_to_response( 'cache/memcache_status.html', dict( stats=stats, hit_rate=hit_rate, time=datetime.datetime.now(), # server time ))
def cache_status(request, ): from django import http from django.shortcuts import render_to_response from django.conf import settings import datetime, re 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() return render_to_response( 'memcached_status.html', dict( stats=stats, hit_rate=100 * stats.get_hits / stats.cmd_get, time=datetime.datetime.now(), # server time ))
def memcached_status(request): # http://effbot.org/zone/django-memcached-view.htm try: import memcache except ImportError: raise Http404 #if not (request.user.is_authenticated() and # request.user.is_staff): # raise Http404 # get first memcached URI m = re.match( "([.\w]+:\d+)", settings.CACHES['default']['LOCATION']) if not m: raise 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() try: hit_rate = 100 * stats.get_hits / stats.cmd_get except: hit_rate = 0 return render_to_response( 'cache/memcache_status.html', dict( stats=stats, hit_rate=hit_rate, time=datetime.datetime.now(), # server time ))
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 server_list(request): template = 'status.html' try: import memcache except: raise Http404 stats = {} cache_servers = {} dict_vars = {} servers = None # It assumes that you're using a right configuration try: servers = settings.CACHES['default']['LOCATION'] except: return render_to_response(template, dict_vars, RequestContext(request)) for server in servers: host = memcache._Host(server) host.connect() if host == 0: raise Http404 try: host.send_cmd("stats") except: break while True: line = host.readline().split(None) if line[0] == "END": break try: val = int(line[2]) if line[1] == "uptime": value = datetime.timedelta(seconds=val) elif line[1] == "time": value = datetime.datetime.fromtimestamp(val) except Exception: pass stats[line[1]] = line[2] stats['hit_rate'] = 0 if int(stats['cmd_get']) != 0: stats['hit_rate'] = 100 * int(stats['get_hits']) / int( stats['cmd_get']) stats['right_now'] = datetime.datetime.now() limit_maxbytes = int(stats['bytes']) if int(stats['limit_maxbytes']) > 0: limit_maxbytes = int(stats['limit_maxbytes']) stats['full'] = (100 * int(stats['bytes']) / limit_maxbytes) # Add info. for this host to list cache_servers[server] = stats # Close connection host.close_socket() dict_vars['servers'] = cache_servers return render_to_response(template, dict_vars, RequestContext(request))
def server_list(request): template = 'status.html' try: import memcache except: raise Http404 stats = {} cache_servers = {} dict_vars = {} servers = None # It assumes that you're using a right configuration try: servers = settings.CACHES['default']['LOCATION'] except: return render_to_response(template, dict_vars, RequestContext(request)) for server in servers: host = memcache._Host(server) host.connect() if host == 0: raise Http404 try: host.send_cmd("stats") except: break while True: line = host.readline().split(None) if line[0] == "END": break try: val = int(line[2]) if line[1] == "uptime": value = datetime.timedelta(seconds=val) elif line[1] == "time": value = datetime.datetime.fromtimestamp(val) except Exception: pass stats[line[1]] = line[2] stats['hit_rate'] = 0 if int(stats['cmd_get']) != 0: stats['hit_rate'] = 100 * int(stats['get_hits']) / int(stats['cmd_get']) stats['right_now'] = datetime.datetime.now() limit_maxbytes = int(stats['bytes']) if int(stats['limit_maxbytes']) > 0: limit_maxbytes = int(stats['limit_maxbytes']) stats['full'] = (100 * int(stats['bytes']) / limit_maxbytes) # Add info. for this host to list cache_servers[server] = stats # Close connection host.close_socket() dict_vars['servers'] = cache_servers return render_to_response(template, dict_vars, RequestContext(request))
def memcache(request): class Stats: pass message = '' stats = None hit_rate = '' try: import memcache except ImportError: message = '"memcache" python module is unavailable, install it please' # get first memcached URI m = re.match("memcached://([.\w]+:\d+)", settings.CACHE_BACKEND) if not m: message = 'Can not locate memcached configuration in settings.py file' else: host = memcache._Host(m.group(1)) host.connect() try: host.send_cmd("stats") except: message = ('Memcached server is unavailable. %s' % settings.CACHE_BACKEND) if not message: 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 = timedelta(seconds=value) elif key == "time": value = datetime.fromtimestamp(value) except ValueError: pass setattr(stats, key, value) hit_rate = 100 * stats.get_hits / stats.cmd_get host.close_socket() return { 'message': message, 'stats': stats, 'hit_rate': hit_rate, 'time': datetime.now() }
def add_server(self, server): """ Adds a host at runtime to client""" # Create a new host entry server = memcache._Host( server, self.debug, dead_retry=self.dead_retry, socket_timeout=self.socket_timeout, flush_on_reconnect=self.flush_on_reconnect ) # Add this to our server choices self.servers.append(server) # Update our buckets self.buckets.append(server) self.consistent_hash.add_machine(server,len(self.servers)-1)
def add_server(self, server): """ Adds a host at runtime to client""" # Create a new host entry server = memcache._Host(server, self.debug, dead_retry=self.dead_retry, socket_timeout=self.socket_timeout, flush_on_reconnect=self.flush_on_reconnect) # Add this to our server choices self.servers.append(server) # Update our buckets self.buckets.append(server) self.consistent_hash.add_machine(server, len(self.servers) - 1)
def memcached_status(request): try: import memcache except ImportError: return HttpResponseRedirect("/") if not (request.user.is_authenticated() and request.user.is_staff): return HttpResponseRedirect("/") # get first memcached URI m = re.match( "memcached://([.\w]+:\d+)", settings.CACHE_BACKEND ) if not m: return HttpResponseRedirect("/") 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() return render_to_response( 'webview/memcached_status.html', dict( stats=stats, hit_rate=100 * stats.get_hits / stats.cmd_get, time=datetime.datetime.now(), # server time ), context_instance=RequestContext(request))
def view(request): try: import memcache except ImportError: raise http.Http404 # get first memcached URI m = re.match( "memcached://([.\w]+:\d+)", settings.CACHE_BACKEND ) if not m: #raise http.Http404 return HttpResponse("No Memcached Server 2") 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() if stats.cmd_get == 0: hit_rate = 'na' else: 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 ))
def add_server(self, server): """ Adds a host at runtime to client """ # Create a new host entry server = memcache._Host( server, self.debug, dead_retry=self.dead_retry, socket_timeout=self.socket_timeout, flush_on_reconnect=self.flush_on_reconnect ) # Add this to our server choices self.servers.append(server) self.buckets.append(server) self.mc_ring.servers.append(server) self.mc_ring.buckets.append(server)
def memcached_status(request): 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() return render( request, 'memcached_status.html', dict( stats=stats, hit_rate=100 * stats.get_hits / stats.cmd_get, time=datetime.datetime.now(), # server time ))
def cache_status(request): """ Fetch memcache status and display it. Taken and modified from: http://effbot.org/zone/django-memcached-view.htm """ try: import memcache except ImportError: return direct_to_template(request, 'memcachedstatus/cache_status.html', {'error': _(u'Memcached library not installed')}) if not MEMCACHE: return direct_to_template(request, 'memcachedstatus/cache_status.html', {'error': _(u'No memcached configured')}) host = memcache._Host(MEMCACHE.group(1)) host.connect() try: host.send_cmd("stats") except AttributeError: return direct_to_template(request, 'memcachedstatus/cache_status.html', {'error': _(u'Memcached not running')}) 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() if stats.cmd_get != 0: context = {'stats': stats, 'hit_rate': 100 * stats.get_hits / stats.cmd_get,} else: context = {'stats': stats} return direct_to_template(request, 'memcachedstatus/cache_status.html', context)
def memcached_status(request): try: import memcache except ImportError: raise Http404 if not request.user.is_authenticated() and request.user.is_superuser: raise Http404 # get first memcached URI m = re.match("memcached://([.\w]+:\d+)", settings.CACHE_BACKEND) if not m: raise 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() return render_to_response( "base/memcached_status.html", { "stats": stats, "hit_rate": 100 * stats.get_hits / stats.cmd_get, "time": datetime.datetime.now(), # server time }, )
def memcached_status(request): try: import memcache except ImportError: raise Http404 if not request.user.is_authenticated() and request.user.is_superuser: raise Http404 # get first memcached URI m = re.match("memcached://([.\w]+:\d+)", settings.CACHE_BACKEND) if not m: raise 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() return render_to_response( template_name='base/memcached_status.html', dictionary={ 'stats': stats, 'hit_rate': 100 * stats.get_hits / stats.cmd_get, 'time': str(datetime.datetime.now()), # server time }, context_instance=RequestContext(request))
def memcache_status(request): """ display memcache server info based on: http://effbot.org/zone/django-memcached-view.htm """ try: import memcache except ImportError: raise http.Http404 stats = {} # get all memcached URIs m = re.match( "memcached://([^/]*)/?$", settings.CACHE_BACKEND ) if m: servers = m.group(1).split(';') if not servers: raise http.Http404 for server in servers: host = memcache._Host(server) host.connect() host.send_cmd("stats") stats[server] = {} while True: 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 stats[server][key] = value stats[server]['hit_rate'] = (100 * stats[server]['get_hits'] / stats[server]['cmd_get']) host.close_socket() return render_to_response('stats/memcached_status.html', {'stats': stats}, context_instance=RequestContext(request))
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))
def add_server(self, server): """ Adds a host at runtime to client """ server_tmp = server # Create a new host entry server = memcache._Host(server, self.debug, dead_retry=self.dead_retry, socket_timeout=self.socket_timeout, flush_on_reconnect=self.flush_on_reconnect) # Add this to our server choices self.servers.append(server) self.buckets.append(server) # Adds this node to the circle self.consistent_hash.add_nodes([server_tmp])
def add_server(self, server): """ Adds a host at runtime to client """ server_tmp = server # Create a new host entry server = memcache._Host( server, self.debug, dead_retry=self.dead_retry, socket_timeout=self.socket_timeout, flush_on_reconnect=self.flush_on_reconnect ) # Add this to our server choices self.servers.append(server) self.buckets.append(server) # Adds this node to the circle self.consistent_hash.add_nodes([server_tmp])
def memcache_status(request): """ display memcache server info based on: http://effbot.org/zone/django-memcached-view.htm """ try: import memcache except ImportError: raise http.Http404 stats = {} # get all memcached URIs m = re.match('memcached://([^/]*)/?$', settings.CACHE_BACKEND) if m: servers = m.group(1).split(';') if not servers: raise http.Http404 for server in servers: host = memcache._Host(server) host.connect() host.send_cmd('stats') stats[server] = {} while True: 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 stats[server][key] = value stats[server]['hit_rate'] = (100 * stats[server]['get_hits'] / stats[server]['cmd_get']) host.close_socket() return render_to_response('stats/memcached_status.html', {'stats': stats}, context_instance=RequestContext(request))
def get_stats(): host = memcache._Host('127.0.0.1:11211') host.connect() host.send_cmd('stats') stats = {} while 1: line = host.readline().split(None, 2) if line[0] == "END": break stat, key, value = line try: value = int(value) except ValueError: pass stats[key] = value host.close_socket() return stats
def view(request): try: import memcache except ImportError: raise http.Http404 if not (request.user.is_authenticated() and request.user.is_staff and hasattr(settings, 'CACHES')): raise http.Http404 host = memcache._Host(settings.CACHES['default']['LOCATION']) 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() return render_to_response( 'memcached_status.html', dict( stats=stats, hit_rate=100 * stats.get_hits / stats.cmd_get, time=datetime.datetime.now(), # server time ))
def cache_status(request, extra_context = None): try: import memcache except ImportError: raise Http404 if not (request.user.is_authenticated() and request.user.is_staff): raise Http404 cache_location = s.CACHES['default']['LOCATION'] host = memcache._Host(cache_location) host.connect() host.send_cmd("stats") class Stats: pass stats = Stats() while True: 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 = timedelta(seconds=value) elif key == "time": value = datetime.fromtimestamp(value) except ValueError: pass setattr(stats, key, value) host.close_socket() context = { 'stats': stats, 'time': datetime.now() } if stats.cmd_get: context['hit_rate'] = 100 * stats.get_hits / stats.cmd_get return context
def memcached_status(request): cachesettings = settings.CACHES # get first memcached URI if 'default' in cachesettings and 'LOCATION' in cachesettings: host = memcache._Host(cachesettings['default']['LOCATION']) host.connect() host.send_cmd("stats") class Stats: def __init__(self): pass stats = Stats() while 1: line = host.readline().split(None, 2) if line[0] == "END": break _, key, value = line try: # convert to native type, if possible value = int(value) if key == "uptime": value = timedelta(seconds=value) elif key == "time": value = datetime.fromtimestamp(value) except ValueError: pass setattr(stats, key, value) host.close_socket() return render_to_response( 'memcached/memcached_status.html', dict( stats=stats, hit_rate=100 * stats.get_hits / stats.cmd_get, miss_rate=100 * stats.get_misses / stats.cmd_get, time=datetime.now(), # server time ))
def memcached_status(request): try: import memcache except ImportError: raise http.Http404 host = memcache._Host(settings.CACHE_HOST) if 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() return render_to_response( 'memcached_status.html', dict( stats=stats, hit_rate=100 * stats.get_hits / max(stats.cmd_get, 1), time=datetime.datetime.now(), # server time )) else: raise http.Http404
def cache_status(request, extra_context=None): try: import memcache except ImportError: raise Http404 if not (request.user.is_authenticated() and request.user.is_staff): raise Http404 cache_location = s.CACHES['default']['LOCATION'] host = memcache._Host(cache_location) host.connect() host.send_cmd("stats") class Stats: pass stats = Stats() while True: 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 = timedelta(seconds=value) elif key == "time": value = datetime.fromtimestamp(value) except ValueError: pass setattr(stats, key, value) host.close_socket() context = {'stats': stats, 'time': datetime.now()} if stats.cmd_get: context['hit_rate'] = 100 * stats.get_hits / stats.cmd_get return context
def handle_noargs(self, **options): verbose = False if not settings.CACHE_BACKEND.startswith('memcached://'): print "you are not configured to use memcched as your django cache backend" else: m = re.search(r'//(.+:\d+)', settings.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" % (settings.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
def handle_noargs(self, **options): verbose = False if not settings.CACHE_BACKEND.startswith('memcached://'): print "you are not configured to use memcched as your django cache backend" else: m = re.search(r'//(.+:\d+)', settings.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" % (settings.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
def set_servers(self, servers): """Set the pool of servers used by this client. @param servers: an array of servers. Servers can be passed in two forms: 1. Strings of the form C{"host:port"}, which implies a default weight of 1. 2. Tuples of the form C{("host:port", weight)}, where C{weight} is an integer weight value. """ #self.servers = [] ###add by tzh start _virtualNodes = {} _max_vitual_nodes = 250 _virtualNodeArray = range(_max_vitual_nodes) self.buckets = [] _servers = [] for s in servers: _host = memcache._Host(s, self.debug, dead_retry=self.dead_retry, socket_timeout=self.socket_timeout, flush_on_reconnect=self.flush_on_reconnect) address = '' if isinstance(s,tuple): address = s[0] else: address = s for i in _virtualNodeArray: key = '%s-node%d' % (address, i) node_code = newmemcache_hash(key) if _virtualNodes.has_key(node_code): raise Exception('has same server!') _virtualNodes[node_code] = _host self.buckets.append(node_code) self.buckets.sort() _servers.append(_host) self._virtualNodes = _virtualNodes self.servers = _servers
def cache_status(request): from django import http import memcache host = memcache._Host(settings.CACHES['default']['LOCATION']) host.connect() host.send_cmd("stats") stats = {} while 1: try: line = host.readline().split(None, 2) except socket.timeout: break 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 stats[key] = value host.close_socket() total_get = (stats.get('get_hits', 0) + stats.get('get_misses', 0)) ctx = dict( stats=stats, items=sorted(stats.iteritems()), hit_rate=(100 * stats.get('get_hits', 0) / total_get) if total_get else 0, total_get=total_get, time=datetime.datetime.now() ) return render_to_response("rundb/configure/cache_status.html", ctx)
def set_server(): ip = request.POST.get('ip') port = request.POST.get('port') num = int(request.POST.get("num", -1)) result = {} if num < 0 or num >= len(MEMCACHE_CLIENT) or not ip or not port: result['status'] = False result['msg'] = "参数错误" else: try: host = memcache._Host("%s:%s" % (ip, port)) i = 0 while i < len(MEMCACHE_CLIENT[num].servers): MEMCACHE_CLIENT[num].servers.pop() MEMCACHE_CLIENT[num].servers.append(host) result['status'] = True result['msg'] = '' except (ValueError, TypeError): result['status'] = False result['msg'] = "参数错误" except Exception, e: result['status'] = False result['msg'] = str(e)
def getMemcachedStats(): try: if not hasattr(settings, 'CACHE_BACKEND'): return None m = re.match("memcached://([.\w]+:\d+)", settings.CACHE_BACKEND) if not m: return None host = memcache._Host(m.group(1)) host.connect() host.send_cmd("stats") stats = MemcachedStats() while True: line = host.readline().split(None, 2) if line[0] == "END": break stat, key, value = line try: value = int(value) if key == "uptime": value = timedelta(seconds=value) elif key == "time": value = datetime.fromtimestamp(value) except ValueError: pass setattr(stats, key, value) host.close_socket() return stats except Exception: return None
def repl(cache_host): h = memcache._Host(cache_host) h.connect() while True: try: command = str(raw_input(PROMPT % cache_host)) except EOFError: print "bye" break if not command: continue if command.split()[0] in UNSUPPORTED_COMMANDS: print "Not supported" continue if command == "quit": break h.send_cmd(command) l = ""; while not message_ends(command, l): l = h.readline() print l h.close_socket()
def add_server(self, server): """ Adds a host at runtime to client """ # when no reliable (modulo) consistent hash algorithm if not self.consistent_hash_manager: raise Exception("The current consistent hash algorithm (\"%s\") is" " not reliable for adding a new server" "" % self.hash_algorithm) # Create a new host entry server = memcache._Host( server, self.debug, dead_retry=self.dead_retry, socket_timeout=self.socket_timeout, flush_on_reconnect=self.flush_on_reconnect ) # Add this to our server choices self.servers.append(server) for _i in range(server.weight): self.buckets.append(server) # Adds this node to the circle if self.consistent_hash_manager: self.consistent_hash_manager.add_node(server)
def repl(cache_host): h = memcache._Host(cache_host) h.connect() while True: try: command = str(raw_input(PROMPT % cache_host)) except EOFError: print "bye" break if not command: continue if command.split()[0] in UNSUPPORTED_COMMANDS: print "Not supported" continue if command == "quit": break h.send_cmd(command) l = "" while not message_ends(command, l): l = h.readline() print l h.close_socket()
import memcache import re import sys from 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)
def cache_status(request): 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('127.0.0.1:11211') 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() shell_cmd_stats_items = subprocess.Popen( ['echo "stats items" | nc 127.0.0.1 11211'], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) shell_cmd_stats_items_output, error = shell_cmd_stats_items.communicate() shell_cmd_stats_items_output = shell_cmd_stats_items_output.splitlines( )[:-1] shell_cmd_stats_item_no_list = list( set([ x.replace(' ', '').split(':')[1] for x in shell_cmd_stats_items_output ])) # print shell_cmd_stats_item_no_list all_items_list = [] for each_item in shell_cmd_stats_item_no_list: shell_cmd_stats_each_item = subprocess.Popen([ 'echo "stats cachedump ' + each_item + ' 100" | nc 127.0.0.1 11211' ], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) shell_cmd_stats_each_item_output, error = shell_cmd_stats_each_item.communicate( ) # print shell_cmd_stats_each_item_output shell_cmd_stats_each_item_output = shell_cmd_stats_each_item_output.splitlines( )[:-1] all_items_list += [ x.split(":")[2] for x in shell_cmd_stats_each_item_output ] # print all_items_list return render_to_response( 'ndf/memcached_status.html', dict( stats=stats, hit_rate=100 * stats.get_hits / stats.cmd_get, time=datetime.datetime.now(), # server time all_cached_items=all_items_list, ))