Skip to content

Commit bd4e4f4

Browse files
authored
Merge pull request #92 from topcoder-platform/issue_memcachedstats
Added memcached stats
2 parents 9d4d8cc + e409d44 commit bd4e4f4

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

DebugPlugin/controllers/api/CacheApiController.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,79 @@ public function get_flush() {
6262
return false;
6363
}
6464
}
65+
66+
public function get_extendedstats(array $query) {
67+
$this->permission('Garden.Settings.Manage');
68+
$in = $this->schema([
69+
'type:s' => 'The type of statistics to fetch(stats, detail, cachedump, slabs, items, sizes)',
70+
'slabid:i?' => 'Slab ID',
71+
'limit:i?' => 'Limit the number of entries to dump'
72+
], 'in')->setDescription('Get server statistics');
73+
74+
$query = $in->validate($query);
75+
$type = $query['type'];
76+
$slabID = $query['slabid'];
77+
$limit = $query['limit'];
78+
79+
if(Gdn_Cache::activeCache()) {
80+
$pos = strrpos(getenv('MEMCACHED_SERVER'), ':');
81+
$server = substr(getenv('MEMCACHED_SERVER'), 0, $pos);
82+
$port = substr(getenv('MEMCACHED_SERVER'), $pos+1);
83+
switch ($type) {
84+
case 'slabs':
85+
return $this->sendMemcacheCommand($server, $port,'stats slabs');
86+
case 'stats':
87+
return $this->sendMemcacheCommand($server, $port,'stats');
88+
case 'items':
89+
return $this->sendMemcacheCommand($server, $port,'stats items');
90+
case 'sizes':
91+
return $this->sendMemcacheCommand($server, $port,'stats sizes');
92+
case 'detail_on':
93+
return $this->sendMemcacheCommand($server, $port,'stats detail on');
94+
case 'detail_off':
95+
return $this->sendMemcacheCommand($server, $port,'stats detail off');
96+
case 'detail_dump':
97+
return $this->sendMemcacheCommand($server, $port,'stats detail dump');
98+
case 'cachedump':
99+
if(!$slabID) {
100+
return 'Missing slabid';
101+
}
102+
$limit = isset($limit)? $limit:100;
103+
return $this->sendMemcacheCommand($server, $port,'stats cachedump '.$slabID.' '.$limit);
104+
default:
105+
return 'Not supported';
106+
}
107+
} else {
108+
return 'Cached disabled';
109+
}
110+
}
111+
112+
function sendMemcacheCommand($server,$port,$command){
113+
114+
$s = @fsockopen($server,$port);
115+
if (!$s){
116+
die("Cant connect to:".$server.':'.$port);
117+
}
118+
119+
fwrite($s, $command."\r\n");
120+
121+
$buf='';
122+
while ((!feof($s))) {
123+
$buf .= fgets($s, 256);
124+
if (strpos($buf,"END\r\n")!==false){ // stat says end
125+
break;
126+
}
127+
if (strpos($buf,"DELETED\r\n")!==false || strpos($buf,"NOT_FOUND\r\n")!==false){ // delete says these
128+
break;
129+
}
130+
if (strpos($buf,"OK\r\n")!==false){
131+
break;
132+
}
133+
if (strpos($buf,"ERROR\r\n")!==false){
134+
break;
135+
}
136+
}
137+
fclose($s);
138+
return $buf;
139+
}
65140
}

DebugPlugin/openapi/cache.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,30 @@ paths:
2929
tags:
3030
- Cache
3131
summary: Invalidate all items in the cache
32+
/cache/extendedstats:
33+
get:
34+
parameters:
35+
- description: The type of statistics to fetch
36+
in: query
37+
name: type
38+
schema:
39+
type: string
40+
- description: Slab ID
41+
in: query
42+
name: slabid
43+
schema:
44+
type: int
45+
- description: Limit
46+
in: query
47+
name: limit
48+
schema:
49+
type: int
50+
responses:
51+
'200':
52+
description: Memcached stats
53+
tags:
54+
- Cache
55+
summary: Get Memcached stats
3256
components:
3357
schemas:
3458
Records:

0 commit comments

Comments
 (0)