Guest User

Untitled

a guest
Jan 21st, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. <?php
  2.  
  3. function sendMemcacheCommands($command){
  4. $MEMCACHE_SERVERS = variable_get('memcache_servers', array('127.0.0.1:11211' => 'default'));
  5. $result = array();
  6.  
  7. foreach (array_keys($MEMCACHE_SERVERS) as $server){
  8. $strs = explode(':',$server);
  9. $host = $strs[0];
  10. $port = $strs[1];
  11. $result[$server] = sendMemcacheCommand($host,$port,$command);
  12. }
  13. return $result;
  14. }
  15.  
  16. function sendMemcacheCommand($server,$port,$command){
  17. $s = @fsockopen($server,$port);
  18. if (!$s){
  19. die("Cant connect to:".$server.':'.$port);
  20. }
  21.  
  22. fwrite($s, $command."\r\n");
  23.  
  24. $buf='';
  25. while ((!feof($s))) {
  26. $buf .= fgets($s, 256);
  27. if (strpos($buf,"END\r\n")!==false){ // stat says end
  28. break;
  29. }
  30. if (strpos($buf,"DELETED\r\n")!==false || strpos($buf,"NOT_FOUND\r\n")!==false){ // delete says these
  31. break;
  32. }
  33. if (strpos($buf,"OK\r\n")!==false){ // flush_all says ok
  34. break;
  35. }
  36. }
  37. fclose($s);
  38. return parseMemcacheResults($buf);
  39. }
  40.  
  41. function parseMemcacheResults($str){
  42. $res = array();
  43. $lines = explode("\r\n",$str);
  44. $cnt = count($lines);
  45. for($i=0; $i< $cnt; $i++){
  46. $line = $lines[$i];
  47. $l = explode(' ',$line,3);
  48. if (count($l)==3){
  49. $res[$l[0]][$l[1]]=$l[2];
  50. if ($l[0]=='VALUE'){ // next line is the value
  51. $res[$l[0]][$l[1]] = array();
  52. list ($flag,$size)=explode(' ',$l[2]);
  53. $res[$l[0]][$l[1]]['stat']=array('flag'=>$flag,'size'=>$size);
  54. $res[$l[0]][$l[1]]['value']=$lines[++$i];
  55. }
  56. }
  57. elseif ($line=='DELETED' || $line=='NOT_FOUND' || $line=='OK'){
  58. return $line;
  59. }
  60. }
  61. return $res;
  62.  
  63. }
  64.  
  65. function getCacheItems(){
  66. $items = sendMemcacheCommands('stats items');
  67. $serverItems = array();
  68. $totalItems = array();
  69. foreach ($items as $server=>$itemlist){
  70. $serverItems[$server] = array();
  71. $totalItems[$server]=0;
  72. if (!isset($itemlist['STAT'])){
  73. continue;
  74. }
  75.  
  76. $iteminfo = $itemlist['STAT'];
  77.  
  78. foreach ($iteminfo as $keyinfo=>$value){
  79. if (preg_match('/items\:(\d+?)\:(.+?)$/',$keyinfo,$matches)){
  80. $serverItems[$server][$matches[1]][$matches[2]] = $value;
  81. if ($matches[2]=='number'){
  82. $totalItems[$server] +=$value;
  83. }
  84. }
  85. }
  86. }
  87. return array('items'=>$serverItems,'counts'=>$totalItems);
  88. }
  89.  
  90. function dumpCacheSlab($server, $slabId, $limit){
  91. list($host, $port) = explode(':',$server);
  92. $resp = sendMemcacheCommand($host,$port,'stats cachedump '.$slabId.' '.$limit);
  93. return $resp;
  94. }
  95.  
  96. function get_all_memcache_keys() {
  97. $cacheItems= getCacheItems();
  98. $items = $cacheItems['items'];
  99. $keys = array();
  100. foreach ($items as $server => $entries) {
  101. foreach ($entries as $slabId => $slab) {
  102. $items = dumpCacheSlab($server,$slabId,$slab['number']);
  103. foreach (array_keys($items['ITEM']) as $item) {
  104. $keys[] = urldecode($item);
  105. }
  106. }
  107. }
  108. sort($keys);
  109. return $keys;
  110. }
  111.  
  112.  
  113. get_all_memcache_keys();
Add Comment
Please, Sign In to add comment