def memcached(keys=False, stats=False, clear=False): """List memcached stored keys and server stats""" if 'CACHE_MEMCACHED_SERVERS' in app.config: servers = app.config['CACHE_MEMCACHED_SERVERS'] pp = pprint.PrettyPrinter(indent=4) for server in servers: host, port = server.split(':') mem = MemcachedStats(host, port) print '%s' % '=' * 80 print 'SERVER: %s:%s' % (host, port) if keys: print 'KEYS:' pp.pprint(mem.keys()) if stats: print 'STATS:' pp.pprint(mem.stats()) # clear keys if clear: cache = memcache.Client(servers, debug=0) if cache.flush_all(): print 'Memcached data flushed' else: print 'Could not flush memcached' else: print 'There is no memcached servers in the config files'
def stats(): logging.error("Sample error message") mem = MemcachedStats() stats_all = mem.stats() stats_get_hits_rate = float(mem.stats()['get_hits']) / float( mem.stats()['get_misses']) * 100 stats_memcached_mem_used = mem.stats()['bytes'] var_temp = (float(mem.stats()['bytes']) / 64000000.00) * 100.00 stats_memcached_mem_used_percentage = format(var_temp, '.6f') return render_template( "app.html.j2", placeholder_stats_all=stats_all, placeholder_stats_get_hits_rate=stats_get_hits_rate, placeholder_stats_memcached_mem_used=stats_memcached_mem_used, placeholder_stats_memcached_mem_used_percentage= stats_memcached_mem_used_percentage)
def cache_stats(): config_name = request.args.get('name') cache_config = config.get(config_name) if cache_config is None: return Response({}, 404, content_type='application/json') if cache_config['type'] == 'redis': r = redis.StrictRedis(host=cache_config['host'], port=cache_config['port'], db=0) return Response(json_to_html(r.info()), 200, content_type='application/json') elif cache_config['type'] == 'memcached': cache = MemcachedStats(cache_config['host'], cache_config['port']) return Response(json_to_html(cache.stats()), 200, content_type='application/json')
interval = 3 def percent_change_in_interval(a0, a1, b0, b1): a_delta = a1 - a0 b_delta = b1 - b0 try: return float(a_delta) / (a_delta + b_delta) except: return '-' print "Interval is", interval, "seconds. Ctrl-c to quit." t = Terminal() mc = MemcachedStats() time_0 = mc.stats() hits_0 = int(time_0['get_hits']) misses_0 = int(time_0['get_misses']) time_a = time_0 while True: try: time.sleep(interval) print t.clear() time_b = mc.stats() hits_a = int(time_a['get_hits']) hits_b = int(time_b['get_hits']) misses_a = int(time_a['get_misses']) misses_b = int(time_b['get_misses'])
from memcached_stats import MemcachedStats import httplib import json import time def report_to_elasticsearch(host, port, data): server = httplib.HTTPConnection(host, port) headers = {'Content-type': 'application/json'} server.request('POST', '/datastore-memcache/appscale', json.dumps(data), headers) response = server.getresponse() print 'Operation completed with status:', response.status if __name__ == '__main__': mem = MemcachedStats() result = { 'timestamp' : int(time.time() * 1000) } data = mem.stats() for k,v in data.items(): try: result[k] = int(v) continue except ValueError: pass try: result[k] = float(v) except ValueError: result[k] = v report_to_elasticsearch('128.111.179.159', 9200, result)