prod_check.memcache.inc 29 KB

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