memcache.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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.info table td dl,
  391. div.info table td dl dt {
  392. display: inline;
  393. }
  394. div.info table td dl dt {
  395. font-weight: bold;
  396. }
  397. div.info table td dl dd {
  398. display: inline-block;
  399. margin-right: 2em;
  400. margin-left: 0.5em;
  401. width: 12em;
  402. }
  403. div.info table td div.slab-dump {
  404. -moz-column-count: 3;
  405. -moz-column-gap: 20px;
  406. -webkit-column-count: 3;
  407. -webkit-column-gap: 20px;
  408. column-count: 3;
  409. column-gap: 20px;
  410. }
  411. div.info table td a {
  412. display: block;
  413. }
  414. div.graph { margin-bottom:1em }
  415. div.graph h2 { background:rgb(204,204,204);; color:black; font-size:1em; margin:0; padding:0.1em 1em 0.1em 1em; }
  416. div.graph table { border:solid rgb(204,204,204) 1px; color:black; font-weight:normal; width:100%; }
  417. div.graph table td.td-0 { background:rgb(238,238,238); }
  418. div.graph table td.td-1 { background:rgb(221,221,221); }
  419. div.graph table td { padding:0.2em 1em 0.4em 1em; }
  420. div.div1,div.div2 { margin-bottom:1em; width:35em; }
  421. div.div3 { position:absolute; left:40em; top:1em; width:580px; }
  422. //div.div3 { position:absolute; left:37em; top:1em; right:1em; }
  423. div.sorting { margin:1.5em 0em 1.5em 2em }
  424. .center { text-align:center }
  425. .aright { position:absolute;right:1em }
  426. .right { text-align:right }
  427. .ok { color:rgb(0,200,0); font-weight:bold}
  428. .failed { color:rgb(200,0,0); font-weight:bold}
  429. span.box {
  430. border: black solid 1px;
  431. border-right:solid black 2px;
  432. border-bottom:solid black 2px;
  433. padding:0 0.5em 0 0.5em;
  434. margin-right:1em;
  435. }
  436. span.green { background:#60F060; padding:0 0.5em 0 0.5em}
  437. span.red { background:#D06030; padding:0 0.5em 0 0.5em }
  438. div.authneeded {
  439. background:rgb(238,238,238);
  440. border:solid rgb(204,204,204) 1px;
  441. color:rgb(200,0,0);
  442. font-size:1.2em;
  443. font-weight:bold;
  444. padding:2em;
  445. text-align:center;
  446. }
  447. input {
  448. background:rgb(153,153,204);
  449. border:solid rgb(102,102,153) 2px;
  450. color:white;
  451. font-weight:bold;
  452. margin-right:1em;
  453. padding:0.1em 0.5em 0.1em 0.5em;
  454. }
  455. //-->
  456. </style>
  457. </head>
  458. <body>
  459. <div class="head">
  460. <h1 class="memcache">
  461. <span class="logo"><a href="http://pecl.php.net/package/memcache">memcache</a></span>
  462. <span class="nameinfo">memcache.php by <a href="http://livebookmark.net">Harun Yayli</a></span>
  463. </h1>
  464. <hr class="memcache">
  465. </div>
  466. <div class=content>
  467. EOB;
  468. return $header;
  469. }
  470. function getFooter(){
  471. global $VERSION;
  472. $footer = '</div><!-- Based on apc.php '.$VERSION.'--></body>
  473. </html>
  474. ';
  475. return $footer;
  476. }
  477. function getMenu(){
  478. global $PHP_SELF;
  479. echo "<ol class=menu>";
  480. if ($_GET['op']!=4){
  481. echo <<<EOB
  482. <li><a href="$PHP_SELF&op={$_GET['op']}">Refresh Data</a></li>
  483. EOB;
  484. }
  485. else {
  486. echo <<<EOB
  487. <li><a href="$PHP_SELF&op=2}">Back</a></li>
  488. EOB;
  489. }
  490. echo
  491. menu_entry(1,'View Host Stats'),
  492. menu_entry(2,'Variables');
  493. echo <<<EOB
  494. </ol>
  495. <br/>
  496. EOB;
  497. }
  498. // TODO, AUTH
  499. $_GET['op'] = !isset($_GET['op'])? '1':$_GET['op'];
  500. $PHP_SELF= isset($_SERVER['PHP_SELF']) ? htmlentities(strip_tags($_SERVER['PHP_SELF'],'')) : '';
  501. $PHP_SELF=$PHP_SELF.'?';
  502. $time = time();
  503. // sanitize _GET
  504. foreach($_GET as $key=>$g){
  505. $_GET[$key]=htmlentities($g);
  506. }
  507. // singleout
  508. // when singleout is set, it only gives details for that server.
  509. if (isset($_GET['singleout']) && $_GET['singleout']>=0 && $_GET['singleout'] <count($MEMCACHE_SERVERS)){
  510. $MEMCACHE_SERVERS = array($MEMCACHE_SERVERS[$_GET['singleout']]);
  511. }
  512. // display images
  513. if (isset($_GET['IMG'])){
  514. $memcacheStats = getMemcacheStats();
  515. $memcacheStatsSingle = getMemcacheStats(false);
  516. if (!graphics_avail()) {
  517. exit(0);
  518. }
  519. function fill_box($im, $x, $y, $w, $h, $color1, $color2,$text='',$placeindex='') {
  520. global $col_black;
  521. $x1=$x+$w-1;
  522. $y1=$y+$h-1;
  523. imagerectangle($im, $x, $y1, $x1+1, $y+1, $col_black);
  524. if($y1>$y) imagefilledrectangle($im, $x, $y, $x1, $y1, $color2);
  525. else imagefilledrectangle($im, $x, $y1, $x1, $y, $color2);
  526. imagerectangle($im, $x, $y1, $x1, $y, $color1);
  527. if ($text) {
  528. if ($placeindex>0) {
  529. if ($placeindex<16)
  530. {
  531. $px=5;
  532. $py=$placeindex*12+6;
  533. imagefilledrectangle($im, $px+90, $py+3, $px+90-4, $py-3, $color2);
  534. imageline($im,$x,$y+$h/2,$px+90,$py,$color2);
  535. imagestring($im,2,$px,$py-6,$text,$color1);
  536. } else {
  537. if ($placeindex<31) {
  538. $px=$x+40*2;
  539. $py=($placeindex-15)*12+6;
  540. } else {
  541. $px=$x+40*2+100*intval(($placeindex-15)/15);
  542. $py=($placeindex%15)*12+6;
  543. }
  544. imagefilledrectangle($im, $px, $py+3, $px-4, $py-3, $color2);
  545. imageline($im,$x+$w,$y+$h/2,$px,$py,$color2);
  546. imagestring($im,2,$px+2,$py-6,$text,$color1);
  547. }
  548. } else {
  549. imagestring($im,4,$x+5,$y1-16,$text,$color1);
  550. }
  551. }
  552. }
  553. function fill_arc($im, $centerX, $centerY, $diameter, $start, $end, $color1,$color2,$text='',$placeindex=0) {
  554. $r=$diameter/2;
  555. $w=deg2rad((360+$start+($end-$start)/2)%360);
  556. if (function_exists("imagefilledarc")) {
  557. // exists only if GD 2.0.1 is avaliable
  558. imagefilledarc($im, $centerX+1, $centerY+1, $diameter, $diameter, $start, $end, $color1, IMG_ARC_PIE);
  559. imagefilledarc($im, $centerX, $centerY, $diameter, $diameter, $start, $end, $color2, IMG_ARC_PIE);
  560. imagefilledarc($im, $centerX, $centerY, $diameter, $diameter, $start, $end, $color1, IMG_ARC_NOFILL|IMG_ARC_EDGED);
  561. } else {
  562. imagearc($im, $centerX, $centerY, $diameter, $diameter, $start, $end, $color2);
  563. imageline($im, $centerX, $centerY, $centerX + cos(deg2rad($start)) * $r, $centerY + sin(deg2rad($start)) * $r, $color2);
  564. imageline($im, $centerX, $centerY, $centerX + cos(deg2rad($start+1)) * $r, $centerY + sin(deg2rad($start)) * $r, $color2);
  565. imageline($im, $centerX, $centerY, $centerX + cos(deg2rad($end-1)) * $r, $centerY + sin(deg2rad($end)) * $r, $color2);
  566. imageline($im, $centerX, $centerY, $centerX + cos(deg2rad($end)) * $r, $centerY + sin(deg2rad($end)) * $r, $color2);
  567. imagefill($im,$centerX + $r*cos($w)/2, $centerY + $r*sin($w)/2, $color2);
  568. }
  569. if ($text) {
  570. if ($placeindex>0) {
  571. imageline($im,$centerX + $r*cos($w)/2, $centerY + $r*sin($w)/2,$diameter, $placeindex*12,$color1);
  572. imagestring($im,4,$diameter, $placeindex*12,$text,$color1);
  573. } else {
  574. imagestring($im,4,$centerX + $r*cos($w)/2, $centerY + $r*sin($w)/2,$text,$color1);
  575. }
  576. }
  577. }
  578. $size = GRAPH_SIZE; // image size
  579. $image = imagecreate($size+50, $size+10);
  580. $col_white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
  581. $col_red = imagecolorallocate($image, 0xD0, 0x60, 0x30);
  582. $col_green = imagecolorallocate($image, 0x60, 0xF0, 0x60);
  583. $col_black = imagecolorallocate($image, 0, 0, 0);
  584. imagecolortransparent($image,$col_white);
  585. switch ($_GET['IMG']){
  586. case 1: // pie chart
  587. $tsize=$memcacheStats['limit_maxbytes'];
  588. $avail=$tsize-$memcacheStats['bytes'];
  589. $x=$y=$size/2;
  590. $angle_from = 0;
  591. $fuzz = 0.000001;
  592. foreach($memcacheStatsSingle as $serv=>$mcs) {
  593. $free = $mcs['STAT']['limit_maxbytes']-$mcs['STAT']['bytes'];
  594. $used = $mcs['STAT']['bytes'];
  595. if ($free>0){
  596. // draw free
  597. $angle_to = ($free*360)/$tsize;
  598. $perc =sprintf("%.2f%%", ($free *100) / $tsize) ;
  599. fill_arc($image,$x,$y,$size,$angle_from,$angle_from + $angle_to ,$col_black,$col_green,$perc);
  600. $angle_from = $angle_from + $angle_to ;
  601. }
  602. if ($used>0){
  603. // draw used
  604. $angle_to = ($used*360)/$tsize;
  605. $perc =sprintf("%.2f%%", ($used *100) / $tsize) ;
  606. fill_arc($image,$x,$y,$size,$angle_from,$angle_from + $angle_to ,$col_black,$col_red, '('.$perc.')' );
  607. $angle_from = $angle_from+ $angle_to ;
  608. }
  609. }
  610. break;
  611. case 2: // hit miss
  612. $hits = ($memcacheStats['get_hits']==0) ? 1:$memcacheStats['get_hits'];
  613. $misses = ($memcacheStats['get_misses']==0) ? 1:$memcacheStats['get_misses'];
  614. $total = $hits + $misses ;
  615. fill_box($image, 30,$size,50,-$hits*($size-21)/$total,$col_black,$col_green,sprintf("%.1f%%",$hits*100/$total));
  616. fill_box($image,130,$size,50,-max(4,($total-$hits)*($size-21)/$total),$col_black,$col_red,sprintf("%.1f%%",$misses*100/$total));
  617. break;
  618. }
  619. header("Content-type: image/png");
  620. imagepng($image);
  621. exit;
  622. }
  623. echo getHeader();
  624. echo getMenu();
  625. switch ($_GET['op']) {
  626. case 1: // host stats
  627. $phpversion = phpversion();
  628. $memcacheStats = getMemcacheStats();
  629. $memcacheStatsSingle = getMemcacheStats(false);
  630. $mem_size = $memcacheStats['limit_maxbytes'];
  631. $mem_used = $memcacheStats['bytes'];
  632. $mem_avail= $mem_size-$mem_used;
  633. $startTime = time()-array_sum($memcacheStats['uptime']);
  634. $curr_items = $memcacheStats['curr_items'];
  635. $total_items = $memcacheStats['total_items'];
  636. $hits = ($memcacheStats['get_hits']==0) ? 1:$memcacheStats['get_hits'];
  637. $misses = ($memcacheStats['get_misses']==0) ? 1:$memcacheStats['get_misses'];
  638. $sets = $memcacheStats['cmd_set'];
  639. $req_rate = sprintf("%.2f",($hits+$misses)/($time-$startTime));
  640. $hit_rate = sprintf("%.2f",($hits)/($time-$startTime));
  641. $miss_rate = sprintf("%.2f",($misses)/($time-$startTime));
  642. $set_rate = sprintf("%.2f",($sets)/($time-$startTime));
  643. echo <<< EOB
  644. <div class="info div1"><h2>General Cache Information</h2>
  645. <table cellspacing=0><tbody>
  646. <tr class=tr-1><td class=td-0>PHP Version</td><td>$phpversion</td></tr>
  647. EOB;
  648. echo "<tr class=tr-0><td class=td-0>Memcached Host". ((count($MEMCACHE_SERVERS)>1) ? 's':'')."</td><td>";
  649. $i=0;
  650. if (!isset($_GET['singleout']) && count($MEMCACHE_SERVERS)>1){
  651. foreach($MEMCACHE_SERVERS as $server){
  652. echo ($i+1).'. <a href="'.$PHP_SELF.'&singleout='.$i++.'">'.$server.'</a><br/>';
  653. }
  654. }
  655. else{
  656. echo '1.'.$MEMCACHE_SERVERS[0];
  657. }
  658. if (isset($_GET['singleout'])){
  659. echo '<a href="'.$PHP_SELF.'">(all servers)</a><br/>';
  660. }
  661. echo "</td></tr>\n";
  662. echo "<tr class=tr-1><td class=td-0>Total Memcache Cache</td><td>".bsize($memcacheStats['limit_maxbytes'])."</td></tr>\n";
  663. echo <<<EOB
  664. </tbody></table>
  665. </div>
  666. <div class="info div1"><h2>Memcache Server Information</h2>
  667. EOB;
  668. foreach($MEMCACHE_SERVERS as $server){
  669. echo '<table cellspacing=0><tbody>';
  670. 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>';
  671. 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>';
  672. echo '<tr class=tr-1><td class=td-0>Uptime</td><td>',duration($memcacheStatsSingle[$server]['STAT']['time']-$memcacheStatsSingle[$server]['STAT']['uptime']),'</td></tr>';
  673. echo '<tr class=tr-0><td class=td-0>Memcached Server Version</td><td>'.$memcacheStatsSingle[$server]['STAT']['version'].'</td></tr>';
  674. echo '<tr class=tr-1><td class=td-0>Used Cache Size</td><td>',bsize($memcacheStatsSingle[$server]['STAT']['bytes']),'</td></tr>';
  675. echo '<tr class=tr-0><td class=td-0>Total Cache Size</td><td>',bsize($memcacheStatsSingle[$server]['STAT']['limit_maxbytes']),'</td></tr>';
  676. echo '</tbody></table>';
  677. }
  678. echo <<<EOB
  679. </div>
  680. <div class="graph div3"><h2>Host Status Diagrams</h2>
  681. <table cellspacing=0><tbody>
  682. EOB;
  683. $size='width='.(GRAPH_SIZE+50).' height='.(GRAPH_SIZE+10);
  684. echo <<<EOB
  685. <tr>
  686. <td class=td-0>Cache Usage</td>
  687. <td class=td-1>Hits &amp; Misses</td>
  688. </tr>
  689. EOB;
  690. echo
  691. graphics_avail() ?
  692. '<tr>'.
  693. "<td class=td-0><img alt=\"\" $size src=\"$PHP_SELF&IMG=1&".(isset($_GET['singleout'])? 'singleout='.$_GET['singleout'].'&':'')."$time\"></td>".
  694. "<td class=td-1><img alt=\"\" $size src=\"$PHP_SELF&IMG=2&".(isset($_GET['singleout'])? 'singleout='.$_GET['singleout'].'&':'')."$time\"></td></tr>\n"
  695. : "",
  696. '<tr>',
  697. '<td class=td-0><span class="green box">&nbsp;</span>Free: ',bsize($mem_avail).sprintf(" (%.1f%%)",$mem_avail*100/$mem_size),"</td>\n",
  698. '<td class=td-1><span class="green box">&nbsp;</span>Hits: ',$hits.sprintf(" (%.1f%%)",$hits*100/($hits+$misses)),"</td>\n",
  699. '</tr>',
  700. '<tr>',
  701. '<td class=td-0><span class="red box">&nbsp;</span>Used: ',bsize($mem_used ).sprintf(" (%.1f%%)",$mem_used *100/$mem_size),"</td>\n",
  702. '<td class=td-1><span class="red box">&nbsp;</span>Misses: ',$misses.sprintf(" (%.1f%%)",$misses*100/($hits+$misses)),"</td>\n";
  703. echo <<< EOB
  704. </tr>
  705. </tbody></table>
  706. <br/>
  707. <div class="info"><h2>Cache Information</h2>
  708. <table cellspacing=0><tbody>
  709. <tr class=tr-0><td class=td-0>Current Items(total)</td><td>$curr_items ($total_items)</td></tr>
  710. <tr class=tr-1><td class=td-0>Hits</td><td>{$hits}</td></tr>
  711. <tr class=tr-0><td class=td-0>Misses</td><td>{$misses}</td></tr>
  712. <tr class=tr-1><td class=td-0>Request Rate (hits, misses)</td><td>$req_rate cache requests/second</td></tr>
  713. <tr class=tr-0><td class=td-0>Hit Rate</td><td>$hit_rate cache requests/second</td></tr>
  714. <tr class=tr-1><td class=td-0>Miss Rate</td><td>$miss_rate cache requests/second</td></tr>
  715. <tr class=tr-0><td class=td-0>Set Rate</td><td>$set_rate cache requests/second</td></tr>
  716. </tbody></table>
  717. </div>
  718. EOB;
  719. break;
  720. case 2: // variables
  721. $m=0;
  722. $cacheItems= getCacheItems();
  723. $items = $cacheItems['items'];
  724. $totals = $cacheItems['counts'];
  725. $maxDump = MAX_ITEM_DUMP;
  726. foreach($items as $server => $entries) {
  727. echo <<< EOB
  728. <div class="info"><table cellspacing=0><tbody>
  729. <tr><th colspan="2">$server</th></tr>
  730. <tr><th>Slab Id</th><th>Info</th></tr>
  731. EOB;
  732. foreach($entries as $slabId => $slab) {
  733. $dumpUrl = $PHP_SELF.'&op=2&server='.(array_search($server,$MEMCACHE_SERVERS)).'&dumpslab='.$slabId;
  734. echo
  735. "<tr class=tr-$m>",
  736. "<td class=td-0><center>",'<a href="',$dumpUrl,'">',$slabId,'</a>',"</center></td>",
  737. "<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>';
  738. if ((isset($_GET['dumpslab']) && $_GET['dumpslab']==$slabId) && (isset($_GET['server']) && $_GET['server']==array_search($server,$MEMCACHE_SERVERS))){
  739. echo "<br/><b>Items: item</b><br/><div class=\"slab-dump\">";
  740. $items = dumpCacheSlab($server,$slabId,$slab['number']);
  741. // maybe someone likes to do a pagination here :)
  742. $i=1;
  743. foreach($items['ITEM'] as $itemKey=>$itemInfo){
  744. $itemInfo = trim($itemInfo,'[ ]');
  745. echo '<a href="',$PHP_SELF,'&op=4&server=',(array_search($server,$MEMCACHE_SERVERS)),'&key=',base64_encode($itemKey).'">',$itemKey,'</a>';
  746. }
  747. }
  748. echo "</div><!-- .slab-dump--></td></tr>";
  749. $m=1-$m;
  750. }
  751. echo <<<EOB
  752. </tbody></table>
  753. </div><hr/>
  754. EOB;
  755. }
  756. break;
  757. break;
  758. case 4: //item dump
  759. if (!isset($_GET['key']) || !isset($_GET['server'])){
  760. echo "No key set!";
  761. break;
  762. }
  763. // I'm not doing anything to check the validity of the key string.
  764. // probably an exploit can be written to delete all the files in key=base64_encode("\n\r delete all").
  765. // somebody has to do a fix to this.
  766. $theKey = htmlentities(base64_decode($_GET['key']));
  767. $theserver = $MEMCACHE_SERVERS[(int)$_GET['server']];
  768. list($h,$p) = explode(':',$theserver);
  769. $r = sendMemcacheCommand($h,$p,'get '.$theKey);
  770. echo <<<EOB
  771. <div class="info"><table cellspacing=0><tbody>
  772. <tr><th>Server<th>Key</th><th>Value</th><th>Delete</th></tr>
  773. EOB;
  774. echo "<tr><td class=td-0>",$theserver,"</td><td class=td-0>",$theKey,
  775. " <br/>flag:",$r['VALUE'][$theKey]['stat']['flag'],
  776. " <br/>Size:",bsize($r['VALUE'][$theKey]['stat']['size']),
  777. "</td><td>",chunk_split($r['VALUE'][$theKey]['value'],40),"</td>",
  778. '<td><a href="',$PHP_SELF,'&op=5&server=',(int)$_GET['server'],'&key=',base64_encode($theKey),"\">Delete</a></td>","</tr>";
  779. echo <<<EOB
  780. </tbody></table>
  781. </div><hr/>
  782. EOB;
  783. break;
  784. case 5: // item delete
  785. if (!isset($_GET['key']) || !isset($_GET['server'])){
  786. echo "No key set!";
  787. break;
  788. }
  789. $theKey = htmlentities(base64_decode($_GET['key']));
  790. $theserver = $MEMCACHE_SERVERS[(int)$_GET['server']];
  791. list($h,$p) = explode(':',$theserver);
  792. $r = sendMemcacheCommand($h,$p,'delete '.$theKey);
  793. echo 'Deleting '.$theKey.':'.$r;
  794. break;
  795. case 6: // flush server
  796. $theserver = $MEMCACHE_SERVERS[(int)$_GET['server']];
  797. $r = flushServer($theserver);
  798. echo 'Flush '.$theserver.":".$r;
  799. break;
  800. }
  801. echo getFooter();
  802. ?>