memcache.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. <?php
  2. /*
  3. +----------------------------------------------------------------------+
  4. | PHP Version 5 |
  5. +----------------------------------------------------------------------+
  6. | Copyright (c) 1997-2004 The PHP Group |
  7. +----------------------------------------------------------------------+
  8. | This source file is subject to version 3.0 of the PHP license, |
  9. | that is bundled with this package in the file LICENSE, and is |
  10. | available through the world-wide-web at the following url: |
  11. | http://www.php.net/license/3_0.txt. |
  12. | If you did not receive a copy of the PHP license and are unable to |
  13. | obtain it through the world-wide-web, please send a note to |
  14. | license@php.net so we can mail you a copy immediately. |
  15. +----------------------------------------------------------------------+
  16. | Author: Harun Yayli <harunyayli at gmail.com> |
  17. +----------------------------------------------------------------------+
  18. */
  19. $VERSION='$Id: memcache.php,v 1.1.2.3 2008/08/28 18:07:54 mikl Exp $';
  20. define('DATE_FORMAT', 'Y/m/d H:i:s');
  21. define('GRAPH_SIZE', 200);
  22. define('MAX_ITEM_DUMP', 50);
  23. // Allow local configuration: change local values in (this file).local.php
  24. $file = $_SERVER['SCRIPT_FILENAME'];
  25. $local = preg_replace('/(.*)(\.php)$/', '$1.local$2', $file);
  26. if (is_readable($local)) {
  27. // echo "<p>Loading local config from $local</p>";
  28. require $local;
  29. }
  30. if (!defined('ADMIN_USERNAME')) {
  31. define('ADMIN_USERNAME', 'apc');
  32. }
  33. if (!defined('ADMIN_PASSWORD')) {
  34. define('ADMIN_PASSWORD', 'apc');
  35. }
  36. if (empty($MEMCACHE_SERVERS)) {
  37. $MEMCACHE_SERVERS[] = 'localhost:11211'; // add more as an array
  38. $MEMCACHE_SERVERS[] = '127.0.0.1:11211'; // add more as an array
  39. //$MEMCACHE_SERVERS[] = 'mymemcache-server1:11211'; // add more as an array
  40. //$MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an array
  41. }
  42. ////////// END OF DEFAULT CONFIG AREA /////////////////////////////////////////////////////////////
  43. ///////////////// Password protect ////////////////////////////////////////////////////////////////
  44. if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) ||
  45. $_SERVER['PHP_AUTH_USER'] != ADMIN_USERNAME ||$_SERVER['PHP_AUTH_PW'] != ADMIN_PASSWORD) {
  46. Header("WWW-Authenticate: Basic realm=\"Memcache Login\"");
  47. Header("HTTP/1.0 401 Unauthorized");
  48. echo <<<EOB
  49. <html><body>
  50. <h1>Rejected!</h1>
  51. <big>Wrong Username or Password!</big>
  52. </body></html>
  53. EOB;
  54. exit;
  55. }
  56. ///////////MEMCACHE FUNCTIONS /////////////////////////////////////////////////////////////////////
  57. function sendMemcacheCommands($command) {
  58. global $MEMCACHE_SERVERS;
  59. $result = array();
  60. foreach ($MEMCACHE_SERVERS as $server) {
  61. $strs = explode(':', $server);
  62. $host = $strs[0];
  63. $port = $strs[1];
  64. $result[$server] = sendMemcacheCommand($host, $port, $command);
  65. }
  66. return $result;
  67. }
  68. function sendMemcacheCommand($server, $port, $command) {
  69. $s = @fsockopen($server,$port);
  70. if (!$s){
  71. die("Can't connect to:". $server .':'. $port);
  72. }
  73. fwrite($s, $command."\r\n");
  74. $buf = '';
  75. while ((!feof($s))) {
  76. $buf .= fgets($s, 256);
  77. if (strpos($buf, "END\r\n") !== false){ // stat says end
  78. break;
  79. }
  80. if (strpos($buf, "DELETED\r\n") !== false || strpos($buf, "NOT_FOUND\r\n") !== false) { // delete says these
  81. break;
  82. }
  83. if (strpos($buf, "OK\r\n") !== false){ // flush_all says ok
  84. break;
  85. }
  86. }
  87. fclose($s);
  88. return parseMemcacheResults($buf);
  89. }
  90. function parseMemcacheResults($str){
  91. $res = array();
  92. $lines = explode("\r\n", $str);
  93. $cnt = count($lines);
  94. for($i=0 ; $i< $cnt ; $i++) {
  95. $line = $lines[$i];
  96. $l = explode(' ', $line, 3);
  97. if (count($l) == 3) {
  98. $res[$l[0]][$l[1]] = $l[2];
  99. if ($l[0] == 'VALUE') { // next line is the value
  100. $res[$l[0]][$l[1]] = array();
  101. list ($flag, $size) = explode(' ', $l[2]);
  102. $res[$l[0]][$l[1]]['stat'] = array(
  103. 'flag' => $flag,
  104. 'size' => $size,
  105. );
  106. $res[$l[0]][$l[1]]['value'] = $lines[++$i];
  107. }
  108. }
  109. elseif ($line == 'DELETED' || $line == 'NOT_FOUND' || $line == 'OK') {
  110. return $line;
  111. }
  112. }
  113. return $res;
  114. }
  115. function dumpCacheSlab($server, $slabId, $limit) {
  116. list($host, $port) = explode(':',$server);
  117. $resp = sendMemcacheCommand($host, $port, 'stats cachedump '. $slabId .' '. $limit);
  118. return $resp;
  119. }
  120. function flushServer($server) {
  121. list($host, $port) = explode(':', $server);
  122. $resp = sendMemcacheCommand($host, $port, 'flush_all');
  123. return $resp;
  124. }
  125. function getCacheItems() {
  126. $items = sendMemcacheCommands('stats items');
  127. $serverItems = array();
  128. $totalItems = array();
  129. foreach ($items as $server => $itemlist) {
  130. $serverItems[$server] = array();
  131. $totalItems[$server] = 0;
  132. if (!isset($itemlist['STAT'])) {
  133. continue;
  134. }
  135. $iteminfo = $itemlist['STAT'];
  136. foreach($iteminfo as $keyinfo => $value) {
  137. if (preg_match('/items\:(\d+?)\:(.+?)$/', $keyinfo, $matches)) {
  138. $serverItems[$server][$matches[1]][$matches[2]] = $value;
  139. if ($matches[2] == 'number') {
  140. $totalItems[$server] +=$value;
  141. }
  142. }
  143. }
  144. }
  145. return array('items'=>$serverItems,'counts'=>$totalItems);
  146. }
  147. function getMemcacheStats($total = true) {
  148. $resp = sendMemcacheCommands('stats');
  149. if ($total){
  150. $res = array();
  151. foreach($resp as $server => $r) {
  152. foreach ($r['STAT'] as $key => $row) {
  153. if (!isset($res[$key])) {
  154. $res[$key] = null;
  155. }
  156. switch ($key){
  157. case 'pid':
  158. $res['pid'][$server] = $row;
  159. break;
  160. case 'uptime':
  161. $res['uptime'][$server] = $row;
  162. break;
  163. case 'time':
  164. $res['time'][$server] = $row;
  165. break;
  166. case 'version':
  167. $res['version'][$server] = $row;
  168. break;
  169. case 'pointer_size':
  170. $res['pointer_size'][$server] = $row;
  171. break;
  172. case 'rusage_user':
  173. $res['rusage_user'][$server] = $row;
  174. break;
  175. case 'rusage_system':
  176. $res['rusage_system'][$server] = $row;
  177. break;
  178. case 'curr_items':
  179. $res['curr_items'] += $row;
  180. break;
  181. case 'total_items':
  182. $res['total_items'] += $row;
  183. break;
  184. case 'bytes':
  185. $res['bytes'] += $row;
  186. break;
  187. case 'curr_connections':
  188. $res['curr_connections'] += $row;
  189. break;
  190. case 'total_connections':
  191. $res['total_connections'] += $row;
  192. break;
  193. case 'connection_structures':
  194. $res['connection_structures'] += $row;
  195. break;
  196. case 'cmd_get':
  197. $res['cmd_get'] += $row;
  198. break;
  199. case 'cmd_set':
  200. $res['cmd_set'] += $row;
  201. break;
  202. case 'get_hits':
  203. $res['get_hits'] += $row;
  204. break;
  205. case 'get_misses':
  206. $res['get_misses'] += $row;
  207. break;
  208. case 'evictions':
  209. $res['evictions'] += $row;
  210. break;
  211. case 'bytes_read':
  212. $res['bytes_read'] += $row;
  213. break;
  214. case 'bytes_written':
  215. $res['bytes_written'] += $row;
  216. break;
  217. case 'limit_maxbytes':
  218. $res['limit_maxbytes'] += $row;
  219. break;
  220. case 'threads':
  221. $res['rusage_system'][$server]= $row;
  222. break;
  223. }
  224. }
  225. }
  226. return $res;
  227. }
  228. return $resp;
  229. }
  230. function duration($ts) {
  231. global $time;
  232. $years = (int) ((($time - $ts) / (7 * 86400)) / 52.177457);
  233. $rem = (int) (($time-$ts) - ($years * 52.177457 * 7 * 86400));
  234. $weeks = (int) (($rem) / (7*86400));
  235. $days = (int) (($rem) / 86400) - $weeks * 7;
  236. $hours = (int) (($rem) / 3600) - $days * 24 - $weeks * 7 * 24;
  237. $mins = (int) (($rem) / 60) - $hours * 60 - $days * 24 * 60 - $weeks * 7 * 24 * 60;
  238. $str = '';
  239. if ($years == 1) {
  240. $str .= "$years year, ";
  241. }
  242. if ($years > 1) {
  243. $str .= "$years years, ";
  244. }
  245. if ($weeks == 1) {
  246. $str .= "$weeks week, ";
  247. }
  248. if ($weeks > 1) {
  249. $str .= "$weeks weeks, ";
  250. }
  251. if ($days == 1) {
  252. $str .= "$days day,";
  253. }
  254. if ($days > 1) {
  255. $str .= "$days days,";
  256. }
  257. if ($hours == 1) {
  258. $str .= " $hours hour and";
  259. }
  260. if ($hours > 1) {
  261. $str .= " $hours hours and";
  262. }
  263. if ($mins == 1) {
  264. $str .= " 1 minute";
  265. }
  266. else {
  267. $str .= " $mins minutes";
  268. }
  269. return $str;
  270. }
  271. // create graphics
  272. //
  273. function graphics_avail() {
  274. return extension_loaded('gd');
  275. }
  276. function bsize($s) {
  277. foreach (array('', 'Ki', 'Mi', 'Gi', 'Ti') as $i => $k) {
  278. if ($s < 1024) {
  279. break;
  280. }
  281. $s /= 1024;
  282. }
  283. return sprintf("%5.1f %sBytes", $s, $k);
  284. }
  285. // create menu entry
  286. function menu_entry($ob, $title) {
  287. global $PHP_SELF;
  288. if ($ob == $_GET['op']) {
  289. return "<li><a class=\"child_active\" href=\"$PHP_SELF&op=$ob\">$title</a></li>";
  290. }
  291. return "<li><a class=\"active\" href=\"$PHP_SELF&op=$ob\">$title</a></li>";
  292. }
  293. function getHeader() {
  294. $header = <<<EOB
  295. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  296. <html>
  297. <head><title>MEMCACHE INFO</title>
  298. <link rel="stylesheet" type="text/css" href="memcache_ui.css" />
  299. </head>
  300. <body>
  301. <div class="head">
  302. <h1 class="memcache">
  303. <span class="logo"><a href="http://pecl.php.net/package/memcache">memcache</a></span>
  304. <span class="nameinfo">memcache.php by <a href="http://livebookmark.net">Harun Yayli</a></span>
  305. </h1>
  306. <hr class="memcache">
  307. </div>
  308. <div class=content>
  309. EOB;
  310. return $header;
  311. }
  312. function getFooter(){
  313. global $VERSION;
  314. $footer = '</div><!-- Based on apc.php '. $VERSION .'--></body>
  315. </html>
  316. ';
  317. return $footer;
  318. }
  319. function getMenu() {
  320. global $PHP_SELF;
  321. echo "<ol class=menu>";
  322. if ($_GET['op']!=4){
  323. echo <<<EOB
  324. <li><a href="$PHP_SELF&op={$_GET['op']}">Refresh Data</a></li>
  325. EOB;
  326. }
  327. else {
  328. echo <<<EOB
  329. <li><a href="$PHP_SELF&op=2}">Back</a></li>
  330. EOB;
  331. }
  332. echo
  333. menu_entry(1, 'View Host Stats'),
  334. menu_entry(2, 'Variables');
  335. echo <<<EOB
  336. </ol>
  337. <br/>
  338. EOB;
  339. }
  340. function fill_arc($im, $centerX, $centerY, $diameter, $start, $end, $color1, $color2, $text = '', $placeindex = 0) {
  341. $r = $diameter / 2;
  342. $w = deg2rad((360 + $start + ($end - $start) / 2) % 360);
  343. if (function_exists("imagefilledarc")) {
  344. // exists only if GD 2.0.1 is avaliable
  345. imagefilledarc($im, $centerX+1, $centerY+1, $diameter, $diameter, $start, $end, $color1, IMG_ARC_PIE);
  346. imagefilledarc($im, $centerX, $centerY, $diameter, $diameter, $start, $end, $color2, IMG_ARC_PIE);
  347. imagefilledarc($im, $centerX, $centerY, $diameter, $diameter, $start, $end, $color1, IMG_ARC_NOFILL|IMG_ARC_EDGED);
  348. }
  349. else {
  350. imagearc($im, $centerX, $centerY, $diameter, $diameter, $start, $end, $color2);
  351. imageline($im, $centerX, $centerY, $centerX + cos(deg2rad($start)) * $r, $centerY + sin(deg2rad($start)) * $r, $color2);
  352. imageline($im, $centerX, $centerY, $centerX + cos(deg2rad($start+1)) * $r, $centerY + sin(deg2rad($start)) * $r, $color2);
  353. imageline($im, $centerX, $centerY, $centerX + cos(deg2rad($end-1)) * $r, $centerY + sin(deg2rad($end)) * $r, $color2);
  354. imageline($im, $centerX, $centerY, $centerX + cos(deg2rad($end)) * $r, $centerY + sin(deg2rad($end)) * $r, $color2);
  355. imagefill($im,$centerX + $r*cos($w)/2, $centerY + $r*sin($w)/2, $color2);
  356. }
  357. if ($text) {
  358. if ($placeindex>0) {
  359. imageline($im,$centerX + $r*cos($w)/2, $centerY + $r*sin($w)/2,$diameter, $placeindex*12,$color1);
  360. imagestring($im,4,$diameter, $placeindex*12,$text,$color1);
  361. }
  362. else {
  363. imagestring($im,4,$centerX + $r*cos($w)/2, $centerY + $r*sin($w)/2,$text,$color1);
  364. }
  365. }
  366. }
  367. function fill_box($im, $x, $y, $w, $h, $color1, $color2, $text = '', $placeindex = '') {
  368. global $col_black;
  369. $x1 = $x + $w - 1;
  370. $y1 = $y + $h - 1;
  371. imagerectangle($im, $x, $y1, $x1 + 1, $y + 1, $col_black);
  372. if ($y1>$y) {
  373. imagefilledrectangle($im, $x, $y, $x1, $y1, $color2);
  374. }
  375. else {
  376. imagefilledrectangle($im, $x, $y1, $x1, $y, $color2);
  377. }
  378. imagerectangle($im, $x, $y1, $x1, $y, $color1);
  379. if ($text) {
  380. if ($placeindex > 0) {
  381. if ($placeindex < 16) {
  382. $px = 5;
  383. $py = $placeindex * 12 + 6;
  384. imagefilledrectangle($im, $px + 90, $py + 3, $px + 90 - 4, $py - 3, $color2);
  385. imageline($im, $x, $y + $h / 2, $px + 90, $py, $color2);
  386. imagestring($im, 2, $px, $py - 6, $text, $color1);
  387. }
  388. else {
  389. if ($placeindex < 31) {
  390. $px = $x + 40 * 2;
  391. $py = ($placeindex - 15) * 12 + 6;
  392. }
  393. else {
  394. $px = $x + 40 * 2 + 100 * intval(($placeindex - 15) / 15);
  395. $py = ($placeindex % 15) * 12 + 6;
  396. }
  397. imagefilledrectangle($im, $px, $py+3, $px - 4, $py - 3, $color2);
  398. imageline($im, $x + $w, $y + $h / 2, $px, $py, $color2);
  399. imagestring($im, 2, $px + 2, $py - 6, $text, $color1);
  400. }
  401. }
  402. else {
  403. imagestring($im, 4, $x + 5, $y1 - 16, $text, $color1);
  404. }
  405. }
  406. }
  407. //////////////////////////////////////////////////////
  408. function main() {
  409. //
  410. // don't cache this page
  411. //
  412. header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
  413. header("Cache-Control: post-check=0, pre-check=0", false);
  414. header("Pragma: no-cache"); // HTTP/1.0
  415. // TODO, AUTH
  416. global $PHP_SELF;
  417. global $MEMCACHE_SERVERS;
  418. global $time;
  419. $_GET['op'] = !isset($_GET['op']) ? '1' : $_GET['op'];
  420. $PHP_SELF= isset($_SERVER['PHP_SELF']) ? htmlentities(strip_tags($_SERVER['PHP_SELF'],'')) : '';
  421. $PHP_SELF = $PHP_SELF .'?';
  422. $time = time();
  423. // sanitize _GET
  424. foreach ($_GET as $key => $g) {
  425. $_GET[$key] = htmlentities($g);
  426. }
  427. // singleout
  428. // when singleout is set, it only gives details for that server.
  429. if (isset($_GET['singleout']) && $_GET['singleout'] >= 0 && $_GET['singleout'] < count($MEMCACHE_SERVERS)) {
  430. $MEMCACHE_SERVERS = array($MEMCACHE_SERVERS[$_GET['singleout']]);
  431. }
  432. // display images
  433. if (isset($_GET['IMG'])) {
  434. $memcacheStats = getMemcacheStats();
  435. $memcacheStatsSingle = getMemcacheStats(false);
  436. if (!graphics_avail()) {
  437. exit(0);
  438. }
  439. $size = GRAPH_SIZE; // image size
  440. $image = imagecreate($size+50, $size+10);
  441. $col_white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
  442. $col_red = imagecolorallocate($image, 0xD0, 0x60, 0x30);
  443. $col_green = imagecolorallocate($image, 0x60, 0xF0, 0x60);
  444. $col_black = imagecolorallocate($image, 0, 0, 0);
  445. imagecolortransparent($image,$col_white);
  446. switch ($_GET['IMG']){
  447. case 1: // pie chart
  448. $tsize = $memcacheStats['limit_maxbytes'];
  449. $avail = $tsize - $memcacheStats['bytes'];
  450. $x = $y = $size / 2;
  451. $angle_from = 0;
  452. $fuzz = 0.000001;
  453. foreach ($memcacheStatsSingle as $serv => $mcs) {
  454. $free = $mcs['STAT']['limit_maxbytes'] - $mcs['STAT']['bytes'];
  455. $used = $mcs['STAT']['bytes'];
  456. if ($free > 0){
  457. // draw free
  458. $angle_to = ($free*360)/$tsize;
  459. $perc =sprintf("%.2f%%", ($free *100) / $tsize) ;
  460. fill_arc($image, $x, $y, $size, $angle_from, $angle_from + $angle_to, $col_black, $col_green, $perc);
  461. $angle_from = $angle_from + $angle_to ;
  462. }
  463. if ($used > 0) {
  464. // draw used
  465. $angle_to = ($used * 360) / $tsize;
  466. $perc = sprintf("%.2f%%", ($used * 100) / $tsize);
  467. fill_arc($image, $x, $y, $size, $angle_from, $angle_from + $angle_to, $col_black, $col_red, '('. $perc .')');
  468. $angle_from = $angle_from + $angle_to ;
  469. }
  470. }
  471. break;
  472. case 2: // hit miss
  473. $hits = ($memcacheStats['get_hits'] == 0) ? 1 : $memcacheStats['get_hits'];
  474. $misses = ($memcacheStats['get_misses'] == 0) ? 1 : $memcacheStats['get_misses'];
  475. $total = $hits + $misses ;
  476. fill_box($image, 30, $size, 50, - $hits * ($size - 21) / $total, $col_black, $col_green, sprintf("%.1f%%", $hits * 100 / $total));
  477. fill_box($image, 130, $size, 50, -max(4, ($total - $hits) * ($size - 21) / $total), $col_black, $col_red, sprintf("%.1f%%", $misses * 100 / $total));
  478. break;
  479. }
  480. header("Content-type: image/png");
  481. imagepng($image);
  482. exit;
  483. }
  484. echo getHeader();
  485. echo getMenu();
  486. switch ($_GET['op']) {
  487. case 1: // host stats
  488. hostStats();
  489. break;
  490. case 2: // variables
  491. variables();
  492. break;
  493. case 4: //item dump
  494. itemDump();
  495. break;
  496. case 5: // item delete
  497. itemDelete();
  498. break;
  499. case 6: // flush server
  500. $theserver = $MEMCACHE_SERVERS[(int)$_GET['server']];
  501. $r = flushServer($theserver);
  502. echo 'Flush '.$theserver.":".$r;
  503. break;
  504. }
  505. echo getFooter();
  506. }
  507. function itemDelete() {
  508. global $MEMCACHE_SERVERS;
  509. if (!isset($_GET['key']) || !isset($_GET['server'])) {
  510. echo "No key set!";
  511. break;
  512. }
  513. $theKey = htmlentities(base64_decode($_GET['key']));
  514. $theserver = $MEMCACHE_SERVERS[(int)$_GET['server']];
  515. list($h, $p) = explode(':', $theserver);
  516. $r = sendMemcacheCommand($h, $p, 'delete '. $theKey);
  517. echo 'Deleting '. $theKey .':'. $r;
  518. }
  519. function itemDump() {
  520. global $PHP_SELF;
  521. global $MEMCACHE_SERVERS;
  522. if (!isset($_GET['key']) || !isset($_GET['server'])){
  523. echo "No key set!";
  524. break;
  525. }
  526. // I'm not doing anything to check the validity of the key string.
  527. // probably an exploit can be written to delete all the files in key=base64_encode("\n\r delete all").
  528. // somebody has to do a fix to this.
  529. $theKey = htmlentities(base64_decode($_GET['key']));
  530. $theserver = $MEMCACHE_SERVERS[(int) $_GET['server']];
  531. list($h, $p) = explode(':', $theserver);
  532. $r = sendMemcacheCommand($h, $p, 'get '. $theKey);
  533. echo <<<EOB
  534. <div class="info"><table cellspacing=0><tbody>
  535. <tr><th>Server<th>Key</th><th>Value</th><th>Delete</th></tr>
  536. EOB;
  537. echo "<tr><td class=td-0>",$theserver,"</td><td class=td-0>", $theKey,
  538. " <br/>flag:", $r['VALUE'][$theKey]['stat']['flag'],
  539. " <br/>Size:", bsize($r['VALUE'][$theKey]['stat']['size']),
  540. "</td><td>", chunk_split($r['VALUE'][$theKey]['value'], 40), "</td>",
  541. '<td><a href="', $PHP_SELF, '&op=5&server=', (int) $_GET['server'], '&key=', base64_encode($theKey), "\">Delete</a></td></tr>";
  542. echo <<<EOB
  543. </tbody></table>
  544. </div><hr/>
  545. EOB;
  546. }
  547. function hostStats() {
  548. global $PHP_SELF;
  549. global $MEMCACHE_SERVERS;
  550. global $time;
  551. $phpversion = phpversion();
  552. $memcacheStats = getMemcacheStats();
  553. $memcacheStatsSingle = getMemcacheStats(false);
  554. $mem_size = $memcacheStats['limit_maxbytes'];
  555. $mem_used = $memcacheStats['bytes'];
  556. $mem_avail= $mem_size - $mem_used;
  557. $startTime = time() - array_sum($memcacheStats['uptime']);
  558. $curr_items = $memcacheStats['curr_items'];
  559. $total_items = $memcacheStats['total_items'];
  560. $hits = ($memcacheStats['get_hits'] == 0) ? 1 : $memcacheStats['get_hits'];
  561. $misses = ($memcacheStats['get_misses'] == 0) ? 1 : $memcacheStats['get_misses'];
  562. $sets = $memcacheStats['cmd_set'];
  563. $req_rate = sprintf("%.2f", ($hits + $misses) / ($time - $startTime));
  564. $hit_rate = sprintf("%.2f", ($hits ) / ($time - $startTime));
  565. $miss_rate = sprintf("%.2f", ( $misses) / ($time - $startTime));
  566. $set_rate = sprintf("%.2f", ($sets ) / ($time - $startTime));
  567. echo <<< EOB
  568. <div class="info div1"><h2>General Cache Information</h2>
  569. <table cellspacing=0><tbody>
  570. <tr class=tr-1><td class=td-0>PHP Version</td><td>$phpversion</td></tr>
  571. EOB;
  572. echo "<tr class=tr-0><td class=td-0>Memcached Host". ((count($MEMCACHE_SERVERS)>1) ? 's':'')."</td><td>";
  573. $i = 0;
  574. if (!isset($_GET['singleout']) && count($MEMCACHE_SERVERS)>1){
  575. foreach ($MEMCACHE_SERVERS as $server) {
  576. echo ($i + 1). '. <a href="'. $PHP_SELF .'&singleout='. $i++ .'">'. $server. '</a><br/>';
  577. }
  578. }
  579. else {
  580. echo '1.'.$MEMCACHE_SERVERS[0];
  581. }
  582. if (isset($_GET['singleout'])) {
  583. echo '<a href="'. $PHP_SELF .'">(all servers)</a><br/>';
  584. }
  585. echo "</td></tr>\n";
  586. echo "<tr class=tr-1><td class=td-0>Total Memcache Cache</td><td>". bsize($memcacheStats['limit_maxbytes']) ."</td></tr>\n";
  587. echo <<<EOB
  588. </tbody></table>
  589. </div>
  590. <div class="info div1"><h2>Memcache Server Information</h2>
  591. EOB;
  592. foreach ($MEMCACHE_SERVERS as $server) {
  593. echo '<table cellspacing=0><tbody>';
  594. echo '<tr class=tr-1><td class=td-1>'. $server .'</td><td><a href="'. $PHP_SELF .'&server='. array_search($server, $MEMCACHE_SERVERS). '&op=6">[<b>Flush this server</b>]</a></td></tr>';
  595. echo '<tr class=tr-0><td class=td-0>Start Time</td><td>', date(DATE_FORMAT, $memcacheStatsSingle[$server]['STAT']['time'] - $memcacheStatsSingle[$server]['STAT']['uptime']),'</td></tr>';
  596. echo '<tr class=tr-1><td class=td-0>Uptime</td><td>', duration($memcacheStatsSingle[$server]['STAT']['time'] - $memcacheStatsSingle[$server]['STAT']['uptime']),'</td></tr>';
  597. echo '<tr class=tr-0><td class=td-0>Memcached Server Version</td><td>'. $memcacheStatsSingle[$server]['STAT']['version'] .'</td></tr>';
  598. echo '<tr class=tr-1><td class=td-0>Used Cache Size</td><td>', bsize($memcacheStatsSingle[$server]['STAT']['bytes']), '</td></tr>';
  599. echo '<tr class=tr-0><td class=td-0>Total Cache Size</td><td>', bsize($memcacheStatsSingle[$server]['STAT']['limit_maxbytes']), '</td></tr>';
  600. echo '</tbody></table>';
  601. }
  602. echo <<<EOB
  603. </div>
  604. <div class="graph div3"><h2>Host Status Diagrams</h2>
  605. <table cellspacing=0><tbody>
  606. EOB;
  607. $size = 'width='. (GRAPH_SIZE + 50) .' height='. (GRAPH_SIZE + 10);
  608. echo <<<EOB
  609. <tr>
  610. <td class=td-0>Cache Usage</td>
  611. <td class=td-1>Hits &amp; Misses</td>
  612. </tr>
  613. EOB;
  614. echo
  615. graphics_avail()
  616. ? '<tr>'.
  617. "<td class=td-0><img alt=\"\" $size src=\"$PHP_SELF&IMG=1&". (isset($_GET['singleout']) ? 'singleout='. $_GET['singleout'] .'&' : '') . "$time\"></td>".
  618. "<td class=td-1><img alt=\"\" $size src=\"$PHP_SELF&IMG=2&". (isset($_GET['singleout']) ? 'singleout='. $_GET['singleout'] .'&' : '') . "$time\"></td></tr>\n"
  619. : "",
  620. '<tr>',
  621. '<td class=td-0><span class="green box">&nbsp;</span>Free: ', bsize($mem_avail) . sprintf(" (%.1f%%)", $mem_avail * 100 / $mem_size), "</td>\n",
  622. '<td class=td-1><span class="green box">&nbsp;</span>Hits: ', $hits . sprintf(" (%.1f%%)", $hits * 100 / ($hits + $misses)), "</td>\n",
  623. '</tr>',
  624. '<tr>',
  625. '<td class=td-0><span class="red box">&nbsp;</span>Used: ', bsize($mem_used ) . sprintf(" (%.1f%%)", $mem_used * 100 / $mem_size), "</td>\n",
  626. '<td class=td-1><span class="red box">&nbsp;</span>Misses: ', $misses . sprintf(" (%.1f%%)", $misses * 100 / ($hits + $misses)), "</td>\n";
  627. echo <<< EOB
  628. </tr>
  629. </tbody></table>
  630. <br/>
  631. <div class="info"><h2>Cache Information</h2>
  632. <table cellspacing=0><tbody>
  633. <tr class=tr-0><td class=td-0>Current Items(total)</td><td>$curr_items ($total_items)</td></tr>
  634. <tr class=tr-1><td class=td-0>Hits</td><td>{$hits}</td></tr>
  635. <tr class=tr-0><td class=td-0>Misses</td><td>{$misses}</td></tr>
  636. <tr class=tr-1><td class=td-0>Request Rate (hits, misses)</td><td>$req_rate cache requests/second</td></tr>
  637. <tr class=tr-0><td class=td-0>Hit Rate</td><td>$hit_rate cache requests/second</td></tr>
  638. <tr class=tr-1><td class=td-0>Miss Rate</td><td>$miss_rate cache requests/second</td></tr>
  639. <tr class=tr-0><td class=td-0>Set Rate</td><td>$set_rate cache requests/second</td></tr>
  640. </tbody></table>
  641. </div>
  642. EOB;
  643. }
  644. function variables() {
  645. global $PHP_SELF;
  646. global $MEMCACHE_SERVERS;
  647. global $time;
  648. $m = 0;
  649. $cacheItems= getCacheItems();
  650. $items = $cacheItems['items'];
  651. $totals = $cacheItems['counts'];
  652. $maxDump = MAX_ITEM_DUMP;
  653. foreach ($items as $server => $entries) {
  654. echo <<< EOB
  655. <div class="info"><table cellspacing=0><tbody>
  656. <tr><th colspan="2">$server</th></tr>
  657. <tr><th>Slab Id</th><th>Info</th></tr>
  658. EOB;
  659. foreach ($entries as $slabId => $slab) {
  660. $dumpUrl = $PHP_SELF .'&op=2&server='. (array_search($server,$MEMCACHE_SERVERS)) .'&dumpslab='. $slabId;
  661. echo
  662. "<tr class=tr-$m>",
  663. "<td class=td-0><center>",'<a href="', $dumpUrl,'">', $slabId, '</a>', "</center></td>",
  664. "<td class=td-last><dl class=\"slab-header\"><dt>Item count:</dt><dd>", $slab['number'], '</dd><dt>Age:</dt><dd>', duration($time - $slab['age']),'</dd><dt>Evicted:</dt><dd>', ((isset($slab['evicted']) && $slab['evicted'] == 1) ? 'Yes' : 'No'), "</dd></dl>\n";
  665. if ((isset($_GET['dumpslab']) && $_GET['dumpslab'] == $slabId)
  666. && (isset($_GET['server']) && $_GET['server'] == array_search($server, $MEMCACHE_SERVERS))) {
  667. echo "<br/><b>Items: item</b><br/><div class=\"slab-dump\">\n";
  668. $items = dumpCacheSlab($server, $slabId, $slab['number']);
  669. // maybe someone likes to do a pagination here :)
  670. $i = 1;
  671. ksort($items['ITEM']);
  672. foreach ($items['ITEM'] as $itemKey => $itemInfo) {
  673. $itemInfo = trim($itemInfo,'[ ]');
  674. echo '<a href="', $PHP_SELF, '&op=4&server=', (array_search($server, $MEMCACHE_SERVERS)), '&key=', base64_encode($itemKey) .'">', $itemKey, "</a>\n";
  675. }
  676. }
  677. echo "</div><!-- .slab-dump--></td></tr>";
  678. $m = 1 - $m;
  679. }
  680. echo <<<EOB
  681. </tbody></table>
  682. </div><hr/>
  683. EOB;
  684. }
  685. }
  686. main();