Share Pastebin
Guest
Public paste!

Anthony

By: a guest | May 1st, 2009 | Syntax: PHP | Size: 1.54 KB | Hits: 36 | Expires: Never
Copy text to clipboard
  1. <?php
  2. /*
  3.  This application will connect to the memcached daemons found in the $daemons array and do a raw "stats" command and then
  4.  pump that data out via echo commands.
  5.  
  6.  ant92083 {at} gmail {dot} com
  7.  5/1/09
  8. */
  9.  
  10. $host = 'localhost';
  11. // the array is built port=>type
  12. $daemons = array(
  13.     '11211' => 'page',
  14.     '11212' => 'default',
  15.     '11213' => 'content',
  16.     '11214' => 'filter',
  17.     '11215' => 'menu',
  18.     '11216' => 'views'
  19.     );
  20.  
  21. // loop through the $daemons array
  22.  
  23. echo "<table><tr>";
  24.  
  25. foreach($daemons as $port=>$type)
  26. {
  27.   echo "<td>";
  28.   echo "<h1>{$host}:{$port} $type</h1>";
  29.  
  30.   $socket = fsockopen($host, $port);
  31.   if(!$socket)
  32.   {
  33.     echo "<h2 style='color: red;'>Error getting connection to {$host}:{$port}</h2>";
  34.   }
  35.   else
  36.   {
  37.     // issue our command to the memcached daemon
  38.     $bytes = fwrite($socket, "stats\n");
  39.     if($bytes <= 0)
  40.     {
  41.       echo "<h2 style'color: red;'>Error sending \'stats\' command to {$host}:{$port}</h2>";
  42.     }
  43.     // kill our telnet connection
  44.     $bytes = fwrite($socket, "quit\n");
  45.     if($bytes <= 0)
  46.     {
  47.       echo "<h2 style'color: red;'>Error sending \'quit\' command to {$host}:{$port}</h2>";
  48.     }
  49.  
  50.     while (!feof($socket)) {
  51.       $string = fgets($socket, 1024)."<br>\n";
  52.       $string = str_replace("STAT ", '', $string);
  53.       $string = str_replace("END", '', $string);
  54.  
  55.       $string = explode(' ', $string);
  56.       $string = implode(': ', $string);
  57.       echo $string;
  58.     }
  59.  
  60.  
  61.     fclose($socket);
  62.     echo "</td>";
  63.   }
  64. }
  65.  
  66. echo "</tr></table>";
  67. ?>