memcache.php 28 KB

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