TestArwMonitor.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3. define('APP_ID', 'test');
  4. define('BASE_ROOT_PATH', str_replace('/test', '', dirname(__FILE__)));
  5. require_once(BASE_ROOT_PATH . '/global.php');
  6. require_once(BASE_CORE_PATH . '/lrlz.php');
  7. require_once(BASE_ROOT_PATH . '/fooder.php');
  8. class TestArwMonitor extends TestCase
  9. {
  10. public static function setUpBeforeClass(): void
  11. {
  12. Base::run_util();
  13. }
  14. public function testMonitor()
  15. {
  16. $str = '/mnt:7.3T,5.7T#/dev/shm:28G,28G#/run/lock:5.0M,5.0M#cur_time:2023-08-24-16-08-02#/home/dongpeng:687M .#/data:7.3T,6.8T';
  17. $convert_time = function ($stime)
  18. {
  19. $val = explode('-', $stime);
  20. $val = "$val[0]-$val[1]-$val[2] $val[3]:$val[4]:$val[5]";
  21. $time = strtotime($val);
  22. return $time;
  23. };
  24. $calc_bytes = function ($str_capacity)
  25. {
  26. $total = strtolower(trim($str_capacity));
  27. $last = $total[-1];
  28. $total = intval($total);
  29. if($last == 't') {
  30. $per = 1024 * 1024 * 1024 * 1024;
  31. }
  32. elseif($last == 'g') {
  33. $per = 1024 * 1024 * 1024;
  34. }
  35. elseif($last == 'm') {
  36. $per = 1024 * 1024;
  37. }
  38. elseif($last == 'k') {
  39. $per = 1024;
  40. }
  41. else {
  42. $per = 1;
  43. }
  44. return $total * $per;
  45. };
  46. $format_detail = function ($detail) use ($calc_bytes)
  47. {
  48. $detail = trim($detail);
  49. $items = explode(',',$detail);
  50. if(count($items) < 2) {
  51. return $detail;
  52. }
  53. $total = strtolower(trim($items[0]));
  54. $avaliable = strtolower(trim($items[1]));
  55. $ratio = $calc_bytes($avaliable) / $calc_bytes($total);
  56. $ratio = number_format($ratio * 100,2);
  57. return "$avaliable / $total = $ratio%";
  58. };
  59. $disk_monitoring_data = function ($str) use ($convert_time,$format_detail)
  60. {
  61. $result = [];
  62. $details = preg_split("/[\s#]+/", $str, 0, PREG_SPLIT_DELIM_CAPTURE);
  63. foreach ($details as $item)
  64. {
  65. $data = explode(':', $item);
  66. if(count($data) != 2) continue;
  67. $dir = trim($data[0]);
  68. if($dir == 'cur_time') {
  69. $val = $convert_time(trim($data[1]));
  70. } else {
  71. $val = $format_detail($data[1]);
  72. }
  73. $result[$dir] = $val;
  74. }
  75. return $result;
  76. };
  77. $details = $disk_monitoring_data($str);
  78. $cur_time = $convert_time('2023-08-24-16-08-02');
  79. $arr = "/mnt:7.3T,5.7T";
  80. $data = preg_split("/[\s:]+/", $arr, 0, PREG_SPLIT_DELIM_CAPTURE);
  81. $key = ltrim($data[0], '/');
  82. $value = $data[1];
  83. // global $config;
  84. // $servers = $config['server'];
  85. // $ins = Cache::getInstance('cacheredis');
  86. // $name = 'disk_monitor';
  87. // $disk_monitoring_data = $ins->hget($name, '');
  88. // $result = [];
  89. // $cur_time = time();
  90. // $ago_time = 0;
  91. //
  92. // array_map(function ($ip, $server_name) use($disk_monitoring_data) {
  93. // $hash_str = $disk_monitoring_data[$ip];
  94. // $item = [];
  95. // if(!empty($hash_str)){
  96. // $hash_str = str_replace('\t', "", $hash_str);
  97. // $arrs = explode('#', $hash_str);
  98. // }
  99. // }, $servers);
  100. }
  101. }
  102. //docker-compose run phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestAccount::testLog)( .*)?$/" --test-suffix TestAccount.php /var/www/html/test