memcache.php 25 KB

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