memcache.php 29 KB

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