decodeCMemDump_v2.2.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!php.exe -q
  2. <?php
  3. /**
  4. * 解码CMEM的dump文件, 输出为key作为文件名的txt文件,value作为其文本内容.
  5. * @author gwang email:wanggangzero@qq.com
  6. * @copyright ? 2015-6-8, SJZ LoyalSoft Corporation & gwang. All rights reserved.
  7. * @version 2017.04.27 更新了解析规则, 对方发来的数据带有了4个字节的类型信息如果是0则没有压缩,2采用压缩.
  8. * 这比原来的数据友好多了, 全部数据完美解析.
  9. * 2015-6-8 created
  10. * @description
  11. * CMEM有两中模式二进制和字符串.一般都用字符串模式,概因效率相差不多.
  12. * 本脚本内解码算法亦以字符串模式为基础.
  13. * 原理: php写入Memcache的字符串对象(c格式的即字节数组)将会被转换为hex字符串
  14. * 再写入memcache, memcache的压缩标志 MEMCACHE_COMPRESSED 指定使用zlib压缩.
  15. * 如果自己的cmem库中使用了压缩,那么需要在将hex还原为字符串后再解压缩一遍还原为
  16. * 原来的数组.
  17. * addition: 即便使用了 MEMCACHE_COMPRESSED 标志位, memcache规定,
  18. * 压缩比大于0.8的不采用压缩value
  19. * 将不被压缩. 因此,下文代码中针对这一特殊规定做了处理.
  20. */
  21. namespace loyalsoft;
  22. # 导入主代码库
  23. include_once __DIR__ . '/../../main.php';
  24. # 设置时区为中国区(东8区)
  25. date_default_timezone_set("PRC");
  26. set_time_limit(0); # cli 不限定执行时间
  27. //
  28. // <editor-fold defaultstate="collapsed" desc=" hack for hex2bin">
  29. if (!function_exists('hex2bin')) { // first include in php 5.4
  30. function hex2bin($str) {
  31. return pack('H*', $str);
  32. // safer方法:2个字节一转换,确保最大限度完成转换.
  33. $sbin = "";
  34. $len = strlen($str);
  35. for ($i = 0; $i < $len; $i += 2) {
  36. $sbin .= pack("H*", substr($str, $i, 2));
  37. }
  38. return $sbin;
  39. }
  40. }
  41. // </editor-fold>
  42. //
  43. /**
  44. * 全局
  45. * @staticvar CRedisUtil $gRedis
  46. * @return \CRedisUtil
  47. */
  48. function gRedis() {
  49. static $gRedis;
  50. if ($gRedis == null) {
  51. $gRedis = new CRedisUtil();
  52. $host = '192.168.10.51';
  53. $port = '6666';
  54. $pwd = 'wanggang1985';
  55. $gRedis->conn($host, $port, $pwd);
  56. }
  57. return $gRedis;
  58. }
  59. function output($key, $value) {
  60. if (strpos($key, '-info') !== FALSE) { // userinfo
  61. $userinfo = JsonUtil::decode($value);
  62. if (property_exists($userinfo, 'user') //
  63. && property_exists($userinfo->user, 'ts') //
  64. && (day() - day($userinfo->user->ts) > 60)) { // 距上次登录60天以上了
  65. // uid, zoneid, tsday, info(bin2hex(gzcompress())
  66. $uid = $userinfo->user->oId;
  67. $zoneid = $userinfo->zoneid;
  68. $tsday = day($userinfo->user->ts);
  69. $info = base64_encode(gzcompress($value));
  70. $SQL = sprintf("call mgodpay.insertUserInfo('%s',%d,%d,'%s');", $uid, $zoneid, $tsday, $info);
  71. daoInst()->exec($SQL);
  72. echo $key . "\r\n";
  73. return;
  74. }
  75. }
  76. $redis = gRedis();
  77. $redis->set($key, $value);
  78. // $handle = fopen(__DIR__ . "/datas4/" . "$key.txt", "w");
  79. // fwrite($handle, $value);
  80. // fclose($handle);
  81. }
  82. if ($argc <= 1 # 未传参数的情况下
  83. || $argv[1] == "-h" || $argv[1] == "\\h") { # 或者是输入\h -h 查询帮助
  84. echo " usage: php.exe $argv[0] dumpfilename \n";
  85. echo " output: a txt file that use key as filename,value as contents.\n";
  86. echo "\t(attension: to avoid too many output in console, filter is suggested.)\n";
  87. } else {
  88. $n = 1;
  89. $filename = $argv[1];
  90. $zoneid = intval(str_replace('cmem_zone', '', $filename));
  91. if (!file_exists($filename)) {
  92. echo " file $filename not exist!";
  93. } else {
  94. $handle = fopen($filename, "rb"); # 打开文件
  95. if ($handle) {
  96. while (( $line = fgets($handle)) !== false) {
  97. $strarr = explode(" ", $line);
  98. $key = hex2bin($strarr[0]);
  99. // echo "key: $key \n";
  100. $sec = $strarr[1];
  101. $type = (int) substr($sec, 0, 8);
  102. $value = hex2bin(substr($sec, 8));
  103. if ($type == 2) {
  104. $value = gzuncompress($value);
  105. // echo "\n\ndecoded value: \n\n$value\n\n";
  106. }
  107. // echo "type: $type\n\ndecoded value: \n\n$value\n\n";
  108. // echo $key." : ".$value ."\n";
  109. output("$key-zone$zoneid", $value);
  110. if ($n++ % 50 == 0) {
  111. echo "dealed $n records!\n";
  112. // break;
  113. }
  114. }
  115. if (!feof($handle)) {
  116. // echo "Error: unexpected fgets() fail\n" ;
  117. }
  118. fclose($handle);
  119. }
  120. }
  121. }