From e409d441ccbea0bcb0bb565c33414db1c2a80e80 Mon Sep 17 00:00:00 2001 From: Bogdanova Olga Date: Sat, 17 Apr 2021 16:39:13 +0300 Subject: [PATCH] Added memcached stats --- .../controllers/api/CacheApiController.php | 75 +++++++++++++++++++ DebugPlugin/openapi/cache.yml | 24 ++++++ 2 files changed, 99 insertions(+) diff --git a/DebugPlugin/controllers/api/CacheApiController.php b/DebugPlugin/controllers/api/CacheApiController.php index b9999a6..57945ed 100644 --- a/DebugPlugin/controllers/api/CacheApiController.php +++ b/DebugPlugin/controllers/api/CacheApiController.php @@ -62,4 +62,79 @@ public function get_flush() { return false; } } + + public function get_extendedstats(array $query) { + $this->permission('Garden.Settings.Manage'); + $in = $this->schema([ + 'type:s' => 'The type of statistics to fetch(stats, detail, cachedump, slabs, items, sizes)', + 'slabid:i?' => 'Slab ID', + 'limit:i?' => 'Limit the number of entries to dump' + ], 'in')->setDescription('Get server statistics'); + + $query = $in->validate($query); + $type = $query['type']; + $slabID = $query['slabid']; + $limit = $query['limit']; + + if(Gdn_Cache::activeCache()) { + $pos = strrpos(getenv('MEMCACHED_SERVER'), ':'); + $server = substr(getenv('MEMCACHED_SERVER'), 0, $pos); + $port = substr(getenv('MEMCACHED_SERVER'), $pos+1); + switch ($type) { + case 'slabs': + return $this->sendMemcacheCommand($server, $port,'stats slabs'); + case 'stats': + return $this->sendMemcacheCommand($server, $port,'stats'); + case 'items': + return $this->sendMemcacheCommand($server, $port,'stats items'); + case 'sizes': + return $this->sendMemcacheCommand($server, $port,'stats sizes'); + case 'detail_on': + return $this->sendMemcacheCommand($server, $port,'stats detail on'); + case 'detail_off': + return $this->sendMemcacheCommand($server, $port,'stats detail off'); + case 'detail_dump': + return $this->sendMemcacheCommand($server, $port,'stats detail dump'); + case 'cachedump': + if(!$slabID) { + return 'Missing slabid'; + } + $limit = isset($limit)? $limit:100; + return $this->sendMemcacheCommand($server, $port,'stats cachedump '.$slabID.' '.$limit); + default: + return 'Not supported'; + } + } else { + return 'Cached disabled'; + } + } + + function sendMemcacheCommand($server,$port,$command){ + + $s = @fsockopen($server,$port); + if (!$s){ + die("Cant connect to:".$server.':'.$port); + } + + fwrite($s, $command."\r\n"); + + $buf=''; + while ((!feof($s))) { + $buf .= fgets($s, 256); + if (strpos($buf,"END\r\n")!==false){ // stat says end + break; + } + if (strpos($buf,"DELETED\r\n")!==false || strpos($buf,"NOT_FOUND\r\n")!==false){ // delete says these + break; + } + if (strpos($buf,"OK\r\n")!==false){ + break; + } + if (strpos($buf,"ERROR\r\n")!==false){ + break; + } + } + fclose($s); + return $buf; + } } \ No newline at end of file diff --git a/DebugPlugin/openapi/cache.yml b/DebugPlugin/openapi/cache.yml index 7c799b7..17befd3 100644 --- a/DebugPlugin/openapi/cache.yml +++ b/DebugPlugin/openapi/cache.yml @@ -29,6 +29,30 @@ paths: tags: - Cache summary: Invalidate all items in the cache + /cache/extendedstats: + get: + parameters: + - description: The type of statistics to fetch + in: query + name: type + schema: + type: string + - description: Slab ID + in: query + name: slabid + schema: + type: int + - description: Limit + in: query + name: limit + schema: + type: int + responses: + '200': + description: Memcached stats + tags: + - Cache + summary: Get Memcached stats components: schemas: Records: