memcache.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  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. <style type="text/css"><!--
  297. body { background:white; font-size:100.01%; margin:0; padding:0; }
  298. body,p,td,th,input,submit { font-size:0.8em;font-family:arial,helvetica,sans-serif; }
  299. * html body {font-size:0.8em}
  300. * html p {font-size:0.8em}
  301. * html td {font-size:0.8em}
  302. * html th {font-size:0.8em}
  303. * html input {font-size:0.8em}
  304. * html submit {font-size:0.8em}
  305. td { vertical-align:top }
  306. a { color:black; font-weight:none; text-decoration:none; }
  307. a:hover { text-decoration:underline; }
  308. div.content { padding:1em 1em 1em 1em; position:absolute; width:97%; z-index:100; }
  309. h1.memcache { background:rgb(153,153,204); margin:0; padding:0.5em 1em 0.5em 1em; }
  310. * html h1.memcache { margin-bottom:-7px; }
  311. h1.memcache a:hover { text-decoration:none; color:rgb(90,90,90); }
  312. h1.memcache span.logo {
  313. background:rgb(119,123,180);
  314. color:black;
  315. border-right: solid black 1px;
  316. border-bottom: solid black 1px;
  317. font-style:italic;
  318. font-size:1em;
  319. padding-left:1.2em;
  320. padding-right:1.2em;
  321. text-align:right;
  322. display:block;
  323. width:130px;
  324. }
  325. h1.memcache span.logo span.name { color:white; font-size:0.7em; padding:0 0.8em 0 2em; }
  326. h1.memcache span.nameinfo { color:white; display:inline; font-size:0.4em; margin-left: 3em; }
  327. h1.memcache div.copy { color:black; font-size:0.4em; position:absolute; right:1em; }
  328. hr.memcache {
  329. background:white;
  330. border-bottom:solid rgb(102,102,153) 1px;
  331. border-style:none;
  332. border-top:solid rgb(102,102,153) 10px;
  333. height:12px;
  334. margin:0;
  335. margin-top:1px;
  336. padding:0;
  337. }
  338. ol,menu { margin:1em 0 0 0; padding:0.2em; margin-left:1em;}
  339. ol.menu li { display:inline; margin-right:0.7em; list-style:none; font-size:85%}
  340. ol.menu a {
  341. background:rgb(153,153,204);
  342. border:solid rgb(102,102,153) 2px;
  343. color:white;
  344. font-weight:bold;
  345. margin-right:0em;
  346. padding:0.1em 0.5em 0.1em 0.5em;
  347. text-decoration:none;
  348. margin-left: 5px;
  349. }
  350. ol.menu a.child_active {
  351. background:rgb(153,153,204);
  352. border:solid rgb(102,102,153) 2px;
  353. color:white;
  354. font-weight:bold;
  355. margin-right:0em;
  356. padding:0.1em 0.5em 0.1em 0.5em;
  357. text-decoration:none;
  358. border-left: solid black 5px;
  359. margin-left: 0px;
  360. }
  361. ol.menu span.active {
  362. background:rgb(153,153,204);
  363. border:solid rgb(102,102,153) 2px;
  364. color:black;
  365. font-weight:bold;
  366. margin-right:0em;
  367. padding:0.1em 0.5em 0.1em 0.5em;
  368. text-decoration:none;
  369. border-left: solid black 5px;
  370. }
  371. ol.menu span.inactive {
  372. background:rgb(193,193,244);
  373. border:solid rgb(182,182,233) 2px;
  374. color:white;
  375. font-weight:bold;
  376. margin-right:0em;
  377. padding:0.1em 0.5em 0.1em 0.5em;
  378. text-decoration:none;
  379. margin-left: 5px;
  380. }
  381. ol.menu a:hover {
  382. background:rgb(193,193,244);
  383. text-decoration:none;
  384. }
  385. div.info {
  386. background:rgb(204,204,204);
  387. border:solid rgb(204,204,204) 1px;
  388. margin-bottom:1em;
  389. }
  390. div.info h2 {
  391. background:rgb(204,204,204);
  392. color:black;
  393. font-size:1em;
  394. margin:0;
  395. padding:0.1em 1em 0.1em 1em;
  396. }
  397. div.info table {
  398. border:solid rgb(204,204,204) 1px;
  399. border-spacing:0;
  400. width:100%;
  401. }
  402. div.info table th {
  403. background:rgb(204,204,204);
  404. color:white;
  405. margin:0;
  406. padding:0.1em 1em 0.1em 1em;
  407. }
  408. div.info table th a.sortable { color:black; }
  409. div.info table tr.tr-0 { background:rgb(238,238,238); }
  410. div.info table tr.tr-1 { background:rgb(221,221,221); }
  411. div.info table td { padding:0.3em 1em 0.3em 1em; }
  412. div.info table td.td-0 { border-right:solid rgb(102,102,153) 1px; white-space:nowrap; }
  413. div.info table td.td-n { border-right:solid rgb(102,102,153) 1px; }
  414. div.info table td h3 {
  415. color:black;
  416. font-size:1.1em;
  417. margin-left:-0.3em;
  418. }
  419. .td-0 a , .td-n a, .tr-0 a , tr-1 a {
  420. text-decoration:underline;
  421. }
  422. div.info table td dl,
  423. div.info table td dl dt {
  424. display: inline;
  425. }
  426. div.info table td dl dt {
  427. font-weight: bold;
  428. }
  429. div.info table td dl dd {
  430. display: inline-block;
  431. margin-right: 2em;
  432. margin-left: 0.5em;
  433. width: 12em;
  434. }
  435. div.info table td div.slab-dump {
  436. -moz-column-count: 3;
  437. -moz-column-gap: 20px;
  438. -webkit-column-count: 3;
  439. -webkit-column-gap: 20px;
  440. column-count: 3;
  441. column-gap: 20px;
  442. }
  443. div.info table td a {
  444. display: block;
  445. }
  446. div.graph { margin-bottom:1em }
  447. div.graph h2 { background:rgb(204,204,204);; color:black; font-size:1em; margin:0; padding:0.1em 1em 0.1em 1em; }
  448. div.graph table { border:solid rgb(204,204,204) 1px; color:black; font-weight:normal; width:100%; }
  449. div.graph table td.td-0 { background:rgb(238,238,238); }
  450. div.graph table td.td-1 { background:rgb(221,221,221); }
  451. div.graph table td { padding:0.2em 1em 0.4em 1em; }
  452. div.div1,div.div2 { margin-bottom:1em; width:35em; }
  453. div.div3 { position:absolute; left:40em; top:1em; width:580px; }
  454. //div.div3 { position:absolute; left:37em; top:1em; right:1em; }
  455. div.sorting { margin:1.5em 0em 1.5em 2em }
  456. .center { text-align:center }
  457. .aright { position:absolute;right:1em }
  458. .right { text-align:right }
  459. .ok { color:rgb(0,200,0); font-weight:bold}
  460. .failed { color:rgb(200,0,0); font-weight:bold}
  461. span.box {
  462. border: black solid 1px;
  463. border-right:solid black 2px;
  464. border-bottom:solid black 2px;
  465. padding:0 0.5em 0 0.5em;
  466. margin-right:1em;
  467. }
  468. span.green { background:#60F060; padding:0 0.5em 0 0.5em}
  469. span.red { background:#D06030; padding:0 0.5em 0 0.5em }
  470. div.authneeded {
  471. background:rgb(238,238,238);
  472. border:solid rgb(204,204,204) 1px;
  473. color:rgb(200,0,0);
  474. font-size:1.2em;
  475. font-weight:bold;
  476. padding:2em;
  477. text-align:center;
  478. }
  479. input {
  480. background:rgb(153,153,204);
  481. border:solid rgb(102,102,153) 2px;
  482. color:white;
  483. font-weight:bold;
  484. margin-right:1em;
  485. padding:0.1em 0.5em 0.1em 0.5em;
  486. }
  487. //-->
  488. </style>
  489. </head>
  490. <body>
  491. <div class="head">
  492. <h1 class="memcache">
  493. <span class="logo"><a href="http://pecl.php.net/package/memcache">memcache</a></span>
  494. <span class="nameinfo">memcache.php by <a href="http://livebookmark.net">Harun Yayli</a></span>
  495. </h1>
  496. <hr class="memcache">
  497. </div>
  498. <div class=content>
  499. EOB;
  500. return $header;
  501. }
  502. function getFooter(){
  503. global $VERSION;
  504. $footer = '</div><!-- Based on apc.php '. $VERSION .'--></body>
  505. </html>
  506. ';
  507. return $footer;
  508. }
  509. function getMenu() {
  510. global $PHP_SELF;
  511. echo "<ol class=menu>";
  512. if ($_GET['op']!=4){
  513. echo <<<EOB
  514. <li><a href="$PHP_SELF&op={$_GET['op']}">Refresh Data</a></li>
  515. EOB;
  516. }
  517. else {
  518. echo <<<EOB
  519. <li><a href="$PHP_SELF&op=2}">Back</a></li>
  520. EOB;
  521. }
  522. echo
  523. menu_entry(1, 'View Host Stats'),
  524. menu_entry(2, 'Variables');
  525. echo <<<EOB
  526. </ol>
  527. <br/>
  528. EOB;
  529. }
  530. function fill_arc($im, $centerX, $centerY, $diameter, $start, $end, $color1, $color2, $text = '', $placeindex = 0) {
  531. $r = $diameter / 2;
  532. $w = deg2rad((360 + $start + ($end - $start) / 2) % 360);
  533. if (function_exists("imagefilledarc")) {
  534. // exists only if GD 2.0.1 is avaliable
  535. imagefilledarc($im, $centerX+1, $centerY+1, $diameter, $diameter, $start, $end, $color1, IMG_ARC_PIE);
  536. imagefilledarc($im, $centerX, $centerY, $diameter, $diameter, $start, $end, $color2, IMG_ARC_PIE);
  537. imagefilledarc($im, $centerX, $centerY, $diameter, $diameter, $start, $end, $color1, IMG_ARC_NOFILL|IMG_ARC_EDGED);
  538. }
  539. else {
  540. imagearc($im, $centerX, $centerY, $diameter, $diameter, $start, $end, $color2);
  541. imageline($im, $centerX, $centerY, $centerX + cos(deg2rad($start)) * $r, $centerY + sin(deg2rad($start)) * $r, $color2);
  542. imageline($im, $centerX, $centerY, $centerX + cos(deg2rad($start+1)) * $r, $centerY + sin(deg2rad($start)) * $r, $color2);
  543. imageline($im, $centerX, $centerY, $centerX + cos(deg2rad($end-1)) * $r, $centerY + sin(deg2rad($end)) * $r, $color2);
  544. imageline($im, $centerX, $centerY, $centerX + cos(deg2rad($end)) * $r, $centerY + sin(deg2rad($end)) * $r, $color2);
  545. imagefill($im,$centerX + $r*cos($w)/2, $centerY + $r*sin($w)/2, $color2);
  546. }
  547. if ($text) {
  548. if ($placeindex>0) {
  549. imageline($im,$centerX + $r*cos($w)/2, $centerY + $r*sin($w)/2,$diameter, $placeindex*12,$color1);
  550. imagestring($im,4,$diameter, $placeindex*12,$text,$color1);
  551. }
  552. else {
  553. imagestring($im,4,$centerX + $r*cos($w)/2, $centerY + $r*sin($w)/2,$text,$color1);
  554. }
  555. }
  556. }
  557. function fill_box($im, $x, $y, $w, $h, $color1, $color2, $text = '', $placeindex = '') {
  558. global $col_black;
  559. $x1 = $x + $w - 1;
  560. $y1 = $y + $h - 1;
  561. imagerectangle($im, $x, $y1, $x1 + 1, $y + 1, $col_black);
  562. if ($y1>$y) {
  563. imagefilledrectangle($im, $x, $y, $x1, $y1, $color2);
  564. }
  565. else {
  566. imagefilledrectangle($im, $x, $y1, $x1, $y, $color2);
  567. }
  568. imagerectangle($im, $x, $y1, $x1, $y, $color1);
  569. if ($text) {
  570. if ($placeindex > 0) {
  571. if ($placeindex < 16) {
  572. $px = 5;
  573. $py = $placeindex * 12 + 6;
  574. imagefilledrectangle($im, $px + 90, $py + 3, $px + 90 - 4, $py - 3, $color2);
  575. imageline($im, $x, $y + $h / 2, $px + 90, $py, $color2);
  576. imagestring($im, 2, $px, $py - 6, $text, $color1);
  577. }
  578. else {
  579. if ($placeindex < 31) {
  580. $px = $x + 40 * 2;
  581. $py = ($placeindex - 15) * 12 + 6;
  582. }
  583. else {
  584. $px = $x + 40 * 2 + 100 * intval(($placeindex - 15) / 15);
  585. $py = ($placeindex % 15) * 12 + 6;
  586. }
  587. imagefilledrectangle($im, $px, $py+3, $px - 4, $py - 3, $color2);
  588. imageline($im, $x + $w, $y + $h / 2, $px, $py, $color2);
  589. imagestring($im, 2, $px + 2, $py - 6, $text, $color1);
  590. }
  591. }
  592. else {
  593. imagestring($im, 4, $x + 5, $y1 - 16, $text, $color1);
  594. }
  595. }
  596. }
  597. //////////////////////////////////////////////////////
  598. function main() {
  599. //
  600. // don't cache this page
  601. //
  602. header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
  603. header("Cache-Control: post-check=0, pre-check=0", false);
  604. header("Pragma: no-cache"); // HTTP/1.0
  605. // TODO, AUTH
  606. global $PHP_SELF;
  607. global $MEMCACHE_SERVERS;
  608. $_GET['op'] = !isset($_GET['op']) ? '1' : $_GET['op'];
  609. $PHP_SELF= isset($_SERVER['PHP_SELF']) ? htmlentities(strip_tags($_SERVER['PHP_SELF'],'')) : '';
  610. $PHP_SELF = $PHP_SELF .'?';
  611. $time = time();
  612. // sanitize _GET
  613. foreach ($_GET as $key => $g) {
  614. $_GET[$key] = htmlentities($g);
  615. }
  616. // singleout
  617. // when singleout is set, it only gives details for that server.
  618. if (isset($_GET['singleout']) && $_GET['singleout'] >= 0 && $_GET['singleout'] < count($MEMCACHE_SERVERS)) {
  619. $MEMCACHE_SERVERS = array($MEMCACHE_SERVERS[$_GET['singleout']]);
  620. }
  621. // display images
  622. if (isset($_GET['IMG'])) {
  623. $memcacheStats = getMemcacheStats();
  624. $memcacheStatsSingle = getMemcacheStats(false);
  625. if (!graphics_avail()) {
  626. exit(0);
  627. }
  628. $size = GRAPH_SIZE; // image size
  629. $image = imagecreate($size+50, $size+10);
  630. $col_white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
  631. $col_red = imagecolorallocate($image, 0xD0, 0x60, 0x30);
  632. $col_green = imagecolorallocate($image, 0x60, 0xF0, 0x60);
  633. $col_black = imagecolorallocate($image, 0, 0, 0);
  634. imagecolortransparent($image,$col_white);
  635. switch ($_GET['IMG']){
  636. case 1: // pie chart
  637. $tsize = $memcacheStats['limit_maxbytes'];
  638. $avail = $tsize - $memcacheStats['bytes'];
  639. $x = $y = $size / 2;
  640. $angle_from = 0;
  641. $fuzz = 0.000001;
  642. foreach ($memcacheStatsSingle as $serv => $mcs) {
  643. $free = $mcs['STAT']['limit_maxbytes'] - $mcs['STAT']['bytes'];
  644. $used = $mcs['STAT']['bytes'];
  645. if ($free > 0){
  646. // draw free
  647. $angle_to = ($free*360)/$tsize;
  648. $perc =sprintf("%.2f%%", ($free *100) / $tsize) ;
  649. fill_arc($image, $x, $y, $size, $angle_from, $angle_from + $angle_to, $col_black, $col_green, $perc);
  650. $angle_from = $angle_from + $angle_to ;
  651. }
  652. if ($used > 0) {
  653. // draw used
  654. $angle_to = ($used * 360) / $tsize;
  655. $perc = sprintf("%.2f%%", ($used * 100) / $tsize);
  656. fill_arc($image, $x, $y, $size, $angle_from, $angle_from + $angle_to, $col_black, $col_red, '('. $perc .')');
  657. $angle_from = $angle_from + $angle_to ;
  658. }
  659. }
  660. break;
  661. case 2: // hit miss
  662. $hits = ($memcacheStats['get_hits'] == 0) ? 1 : $memcacheStats['get_hits'];
  663. $misses = ($memcacheStats['get_misses'] == 0) ? 1 : $memcacheStats['get_misses'];
  664. $total = $hits + $misses ;
  665. fill_box($image, 30, $size, 50, - $hits * ($size - 21) / $total, $col_black, $col_green, sprintf("%.1f%%", $hits * 100 / $total));
  666. fill_box($image, 130, $size, 50, -max(4, ($total - $hits) * ($size - 21) / $total), $col_black, $col_red, sprintf("%.1f%%", $misses * 100 / $total));
  667. break;
  668. }
  669. header("Content-type: image/png");
  670. imagepng($image);
  671. exit;
  672. }
  673. echo getHeader();
  674. echo getMenu();
  675. switch ($_GET['op']) {
  676. case 1: // host stats
  677. hostStats();
  678. break;
  679. case 2: // variables
  680. variables();
  681. break;
  682. case 4: //item dump
  683. itemDump();
  684. break;
  685. case 5: // item delete
  686. itemDelete();
  687. break;
  688. case 6: // flush server
  689. $theserver = $MEMCACHE_SERVERS[(int)$_GET['server']];
  690. $r = flushServer($theserver);
  691. echo 'Flush '.$theserver.":".$r;
  692. break;
  693. }
  694. echo getFooter();
  695. }
  696. function itemDelete() {
  697. global $MEMCACHE_SERVERS;
  698. if (!isset($_GET['key']) || !isset($_GET['server'])) {
  699. echo "No key set!";
  700. break;
  701. }
  702. $theKey = htmlentities(base64_decode($_GET['key']));
  703. $theserver = $MEMCACHE_SERVERS[(int)$_GET['server']];
  704. list($h, $p) = explode(':', $theserver);
  705. $r = sendMemcacheCommand($h, $p, 'delete '. $theKey);
  706. echo 'Deleting '. $theKey .':'. $r;
  707. }
  708. function itemDump() {
  709. global $PHP_SELF;
  710. global $MEMCACHE_SERVERS;
  711. if (!isset($_GET['key']) || !isset($_GET['server'])){
  712. echo "No key set!";
  713. break;
  714. }
  715. // I'm not doing anything to check the validity of the key string.
  716. // probably an exploit can be written to delete all the files in key=base64_encode("\n\r delete all").
  717. // somebody has to do a fix to this.
  718. $theKey = htmlentities(base64_decode($_GET['key']));
  719. $theserver = $MEMCACHE_SERVERS[(int) $_GET['server']];
  720. list($h, $p) = explode(':', $theserver);
  721. $r = sendMemcacheCommand($h, $p, 'get '. $theKey);
  722. echo <<<EOB
  723. <div class="info"><table cellspacing=0><tbody>
  724. <tr><th>Server<th>Key</th><th>Value</th><th>Delete</th></tr>
  725. EOB;
  726. echo "<tr><td class=td-0>",$theserver,"</td><td class=td-0>", $theKey,
  727. " <br/>flag:", $r['VALUE'][$theKey]['stat']['flag'],
  728. " <br/>Size:", bsize($r['VALUE'][$theKey]['stat']['size']),
  729. "</td><td>", chunk_split($r['VALUE'][$theKey]['value'], 40), "</td>",
  730. '<td><a href="', $PHP_SELF, '&op=5&server=', (int) $_GET['server'], '&key=', base64_encode($theKey), "\">Delete</a></td></tr>";
  731. echo <<<EOB
  732. </tbody></table>
  733. </div><hr/>
  734. EOB;
  735. }
  736. function hostStats() {
  737. global $PHP_SELF;
  738. global $MEMCACHE_SERVERS;
  739. $phpversion = phpversion();
  740. $memcacheStats = getMemcacheStats();
  741. $memcacheStatsSingle = getMemcacheStats(false);
  742. $mem_size = $memcacheStats['limit_maxbytes'];
  743. $mem_used = $memcacheStats['bytes'];
  744. $mem_avail= $mem_size - $mem_used;
  745. $startTime = time() - array_sum($memcacheStats['uptime']);
  746. $curr_items = $memcacheStats['curr_items'];
  747. $total_items = $memcacheStats['total_items'];
  748. $hits = ($memcacheStats['get_hits'] == 0) ? 1 : $memcacheStats['get_hits'];
  749. $misses = ($memcacheStats['get_misses'] == 0) ? 1 : $memcacheStats['get_misses'];
  750. $sets = $memcacheStats['cmd_set'];
  751. $req_rate = sprintf("%.2f", ($hits + $misses) / ($time - $startTime));
  752. $hit_rate = sprintf("%.2f", ($hits ) / ($time - $startTime));
  753. $miss_rate = sprintf("%.2f", ( $misses) / ($time - $startTime));
  754. $set_rate = sprintf("%.2f", ($sets ) / ($time - $startTime));
  755. echo <<< EOB
  756. <div class="info div1"><h2>General Cache Information</h2>
  757. <table cellspacing=0><tbody>
  758. <tr class=tr-1><td class=td-0>PHP Version</td><td>$phpversion</td></tr>
  759. EOB;
  760. echo "<tr class=tr-0><td class=td-0>Memcached Host". ((count($MEMCACHE_SERVERS)>1) ? 's':'')."</td><td>";
  761. $i = 0;
  762. if (!isset($_GET['singleout']) && count($MEMCACHE_SERVERS)>1){
  763. foreach ($MEMCACHE_SERVERS as $server) {
  764. echo ($i + 1). '. <a href="'. $PHP_SELF .'&singleout='. $i++ .'">'. $server. '</a><br/>';
  765. }
  766. }
  767. else {
  768. echo '1.'.$MEMCACHE_SERVERS[0];
  769. }
  770. if (isset($_GET['singleout'])) {
  771. echo '<a href="'. $PHP_SELF .'">(all servers)</a><br/>';
  772. }
  773. echo "</td></tr>\n";
  774. echo "<tr class=tr-1><td class=td-0>Total Memcache Cache</td><td>". bsize($memcacheStats['limit_maxbytes']) ."</td></tr>\n";
  775. echo <<<EOB
  776. </tbody></table>
  777. </div>
  778. <div class="info div1"><h2>Memcache Server Information</h2>
  779. EOB;
  780. foreach ($MEMCACHE_SERVERS as $server) {
  781. echo '<table cellspacing=0><tbody>';
  782. 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>';
  783. 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>';
  784. echo '<tr class=tr-1><td class=td-0>Uptime</td><td>', duration($memcacheStatsSingle[$server]['STAT']['time'] - $memcacheStatsSingle[$server]['STAT']['uptime']),'</td></tr>';
  785. echo '<tr class=tr-0><td class=td-0>Memcached Server Version</td><td>'. $memcacheStatsSingle[$server]['STAT']['version'] .'</td></tr>';
  786. echo '<tr class=tr-1><td class=td-0>Used Cache Size</td><td>', bsize($memcacheStatsSingle[$server]['STAT']['bytes']), '</td></tr>';
  787. echo '<tr class=tr-0><td class=td-0>Total Cache Size</td><td>', bsize($memcacheStatsSingle[$server]['STAT']['limit_maxbytes']), '</td></tr>';
  788. echo '</tbody></table>';
  789. }
  790. echo <<<EOB
  791. </div>
  792. <div class="graph div3"><h2>Host Status Diagrams</h2>
  793. <table cellspacing=0><tbody>
  794. EOB;
  795. $size = 'width='. (GRAPH_SIZE + 50) .' height='. (GRAPH_SIZE + 10);
  796. echo <<<EOB
  797. <tr>
  798. <td class=td-0>Cache Usage</td>
  799. <td class=td-1>Hits &amp; Misses</td>
  800. </tr>
  801. EOB;
  802. echo
  803. graphics_avail()
  804. ? '<tr>'.
  805. "<td class=td-0><img alt=\"\" $size src=\"$PHP_SELF&IMG=1&". (isset($_GET['singleout']) ? 'singleout='. $_GET['singleout'] .'&' : '') . "$time\"></td>".
  806. "<td class=td-1><img alt=\"\" $size src=\"$PHP_SELF&IMG=2&". (isset($_GET['singleout']) ? 'singleout='. $_GET['singleout'] .'&' : '') . "$time\"></td></tr>\n"
  807. : "",
  808. '<tr>',
  809. '<td class=td-0><span class="green box">&nbsp;</span>Free: ', bsize($mem_avail) . sprintf(" (%.1f%%)", $mem_avail * 100 / $mem_size), "</td>\n",
  810. '<td class=td-1><span class="green box">&nbsp;</span>Hits: ', $hits . sprintf(" (%.1f%%)", $hits * 100 / ($hits + $misses)), "</td>\n",
  811. '</tr>',
  812. '<tr>',
  813. '<td class=td-0><span class="red box">&nbsp;</span>Used: ', bsize($mem_used ) . sprintf(" (%.1f%%)", $mem_used * 100 / $mem_size), "</td>\n",
  814. '<td class=td-1><span class="red box">&nbsp;</span>Misses: ', $misses . sprintf(" (%.1f%%)", $misses * 100 / ($hits + $misses)), "</td>\n";
  815. echo <<< EOB
  816. </tr>
  817. </tbody></table>
  818. <br/>
  819. <div class="info"><h2>Cache Information</h2>
  820. <table cellspacing=0><tbody>
  821. <tr class=tr-0><td class=td-0>Current Items(total)</td><td>$curr_items ($total_items)</td></tr>
  822. <tr class=tr-1><td class=td-0>Hits</td><td>{$hits}</td></tr>
  823. <tr class=tr-0><td class=td-0>Misses</td><td>{$misses}</td></tr>
  824. <tr class=tr-1><td class=td-0>Request Rate (hits, misses)</td><td>$req_rate cache requests/second</td></tr>
  825. <tr class=tr-0><td class=td-0>Hit Rate</td><td>$hit_rate cache requests/second</td></tr>
  826. <tr class=tr-1><td class=td-0>Miss Rate</td><td>$miss_rate cache requests/second</td></tr>
  827. <tr class=tr-0><td class=td-0>Set Rate</td><td>$set_rate cache requests/second</td></tr>
  828. </tbody></table>
  829. </div>
  830. EOB;
  831. }
  832. function variables() {
  833. global $PHP_SELF;
  834. global $MEMCACHE_SERVERS;
  835. $m = 0;
  836. $cacheItems= getCacheItems();
  837. $items = $cacheItems['items'];
  838. $totals = $cacheItems['counts'];
  839. $maxDump = MAX_ITEM_DUMP;
  840. foreach ($items as $server => $entries) {
  841. echo <<< EOB
  842. <div class="info"><table cellspacing=0><tbody>
  843. <tr><th colspan="2">$server</th></tr>
  844. <tr><th>Slab Id</th><th>Info</th></tr>
  845. EOB;
  846. foreach ($entries as $slabId => $slab) {
  847. $dumpUrl = $PHP_SELF .'&op=2&server='. (array_search($server,$MEMCACHE_SERVERS)) .'&dumpslab='. $slabId;
  848. echo
  849. "<tr class=tr-$m>",
  850. "<td class=td-0><center>",'<a href="', $dumpUrl,'">', $slabId, '</a>', "</center></td>",
  851. "<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>';
  852. if ((isset($_GET['dumpslab']) && $_GET['dumpslab'] == $slabId)
  853. && (isset($_GET['server']) && $_GET['server'] == array_search($server, $MEMCACHE_SERVERS))) {
  854. echo "<br/><b>Items: item</b><br/><div class=\"slab-dump\">";
  855. $items = dumpCacheSlab($server, $slabId, $slab['number']);
  856. // maybe someone likes to do a pagination here :)
  857. $i = 1;
  858. foreach ($items['ITEM'] as $itemKey => $itemInfo) {
  859. $itemInfo = trim($itemInfo,'[ ]');
  860. echo '<a href="',$PHP_SELF, '&op=4&server=', (array_search($server,$MEMCACHE_SERVERS)), '&key=', base64_encode($itemKey) .'">', $itemKey, '</a>';
  861. }
  862. }
  863. echo "</div><!-- .slab-dump--></td></tr>";
  864. $m = 1 - $m;
  865. }
  866. echo <<<EOB
  867. </tbody></table>
  868. </div><hr/>
  869. EOB;
  870. }
  871. }
  872. main();