Skip to content

Added memcached stats #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions DebugPlugin/controllers/api/CacheApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
24 changes: 24 additions & 0 deletions DebugPlugin/openapi/cache.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down