Преглед изворни кода

update service disk monitoring page Styles And optimize the backend code and calculate percentages

dong пре 1 година
родитељ
комит
6e384bbfda
4 измењених фајлова са 230 додато и 62 уклоњено
  1. 72 47
      admin/control/arw_monitor.php
  2. 13 7
      admin/templates/default/arw.monitor.php
  3. 129 0
      test/TestArwMonitor.php
  4. 16 8
      util/arw_monitor.sh

+ 72 - 47
admin/control/arw_monitor.php

@@ -13,60 +13,85 @@ class arw_monitorControl extends SystemControl
         $servers = $config['server'];
         $ins = Cache::getInstance('cacheredis');
         $name = 'disk_monitor';
-        $hash_data = $ins->hget($name, '');
-        $result = [];
-        $cur_time = time();
+        $monitoring_data = $ins->hget($name, '');
         $ago_time = 0;
 
-        $time_check = function($stat_str, $cur_time) {
-            $value = explode('-', $stat_str);
-            $value = "$value[0]-$value[1]-$value[2] $value[3]:$value[4]:$value[5]";
-            $stat_time = strtotime($value);
-            if($stat_time == false) return false;
+        $convert_time = function ($stime)
+        {
+            $val = explode('-', $stime);
+            $val = "$val[0]-$val[1]-$val[2] $val[3]:$val[4]:$val[5]";
+            $time = strtotime($val);
+            return date("Y-m-d H:i:s", $time);
+        };
+
+        $calc_bytes = function ($str_capacity)
+        {
+            $total = strtolower(trim($str_capacity));
+            $last = $total[-1];
 
-            $check_seconds = 1800;
-            $diff_time = $cur_time - $stat_time;
-            if($diff_time > $check_seconds) {
-                return false;
-            }else{
-                return $diff_time;
+            $total = intval($total);
+            if($last == 't') {
+                $per = 1024 * 1024 * 1024 * 1024;
+            }
+            elseif($last == 'g') {
+                $per = 1024 * 1024 * 1024;
+            }
+            elseif($last == 'm') {
+                $per = 1024 * 1024;
+            }
+            elseif($last == 'k') {
+                $per = 1024;
             }
+            else {
+                $per = 1;
+            }
+
+            return $total * $per;
         };
-        foreach ($servers as $ip => $server_name) {
-            $hash_str = $hash_data[$ip];
-            $item = [];
-            if (!empty($hash_str)) {
-                $hash_str = str_replace('\t', "", $hash_str);
-                $hash_str = ltrim($hash_str, '#');
-                $arrs = explode('#', $hash_str);
-                foreach ($arrs as $arr) {
-                    $data = explode(':', $arr);
-                    $key = ltrim($data[0], '_');
-                    $value = $data[1];
-                    if ($key == 'cur_time') {
-                        $diff_time = $time_check($value, $cur_time);
-                        if ($diff_time == false) {
-                            $value = '定时更新未正常工作';
-                        } else {
-                            $ago_time = $diff_time;
-                        }
-                    } elseif (strpos($key, 'dev') !== false) {
-                        $key = str_replace('_', "/", $key);
-                        $value = explode(',', $value);
-                        if(strpos($value[0], 'T') !== false) $value[0] = $value[0] * 1024 . 'G';
-                        if(strpos($value[1], 'T') !== false) $value[1] = $value[1] * 1024 . 'G';
-                        $percent = $value[1]/$value[0]*100;
-                        $value = "{$value[1]}/{$value[0]} (" . round($percent,2). "%)";
-                    } else {
-                        $key = str_replace('_', "/", $key);
-                        $value = rtrim($value, '.');
-                    }
-                    $item[$key] = $value;
-                    ksort($item);
+
+        $format_detail = function ($detail) use ($calc_bytes)
+        {
+            $detail = trim($detail);
+            $items = explode(',',$detail);
+            if(count($items) < 2) {
+                return $detail;
+            }
+
+            $total = strtolower(trim($items[0]));
+            $avaliable = strtolower(trim($items[1]));
+
+            $ratio = $calc_bytes($avaliable) / $calc_bytes($total);
+
+            $ratio = number_format($ratio * 100,2);
+
+            return "$avaliable / $total = $ratio%";
+        };
+
+        $disk_monitoring_data = function ($str)  use ($convert_time,$format_detail)
+        {
+            $result = [];
+            $details = preg_split("/[\s#]+/", $str, 0, PREG_SPLIT_DELIM_CAPTURE);
+            foreach ($details as $item)
+            {
+                $data = explode(':', $item);
+                if(count($data) != 2) continue;
+                $dir = trim($data[0]);
+                if($dir == 'cur_time') {
+                    $val = $convert_time(trim($data[1]));
+                } else {
+                    $val = $format_detail($data[1]);
                 }
+
+                $result[$dir] =  $val;
             }
-            $item["server_name"] = $server_name;
-            $result[$ip] = $item;
+            return $result;
+        };
+
+        foreach ($servers as $ip => $server_name) {
+            $hash_str = $monitoring_data[$ip];
+            $details = $disk_monitoring_data($hash_str);
+            $details["server_name"] = $server_name;
+            $result[$ip] = $details;
         }
         Tpl::output('data', $result);
         Tpl::output('ago_time', $this->elapse_time($ago_time));

+ 13 - 7
admin/templates/default/arw.monitor.php

@@ -3,6 +3,12 @@
     th {
         width: 10px;
     }
+    .width-ten {
+        width: 10%;
+    }
+    .width-fifteen{
+        width: 15%;
+    }
 </style>
 
 <?php defined('InShopNC') or exit('Access Invalid!'); ?>
@@ -34,14 +40,14 @@
         <thead>
         <tr class="thead">
             <th class="align-left">ip</th>
-            <th class="align-center">类型</th>
-            <th class="align-center" style="width: 15%">更新时间</th>
-            <th class="align-center">/mnt</th>
-            <th class="align-center">/data</th>
-            <th class="align-center">logs</th>
+            <th class="align-center width-ten">类型</th>
+            <th class="align-center width-fifteen">更新时间</th>
+            <th class="align-center width-fifteen">/mnt</th>
+            <th class="align-center width-fifteen">/data</th>
+            <th class="align-center width-fifteen">logs</th>
             <th class="align-center">/stdata</th>
-            <th class="align-center">data/mysqlundo</th>
-            <th class="align-center">mysql/xyzshop</th>
+            <th class="align-center width-fifteen">data/mysqlundo</th>
+            <th class="align-center width-fifteen">mysql/xyzshop</th>
         </thead>
         <tbody id="tbody">
         <?php if (count($output['data']) > 0) { ?>

+ 129 - 0
test/TestArwMonitor.php

@@ -0,0 +1,129 @@
+<?php
+use PHPUnit\Framework\TestCase;
+
+define('APP_ID', 'test');
+define('BASE_ROOT_PATH', str_replace('/test', '', dirname(__FILE__)));
+
+require_once(BASE_ROOT_PATH . '/global.php');
+require_once(BASE_CORE_PATH . '/lrlz.php');
+require_once(BASE_ROOT_PATH . '/fooder.php');
+
+class TestArwMonitor extends TestCase
+{
+    public static function setUpBeforeClass(): void
+    {
+        Base::run_util();
+    }
+
+
+    public function testMonitor()
+    {
+        $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';
+        $convert_time = function ($stime)
+        {
+            $val = explode('-', $stime);
+            $val = "$val[0]-$val[1]-$val[2] $val[3]:$val[4]:$val[5]";
+            $time = strtotime($val);
+            return $time;
+        };
+
+        $calc_bytes = function ($str_capacity)
+        {
+            $total = strtolower(trim($str_capacity));
+            $last = $total[-1];
+
+            $total = intval($total);
+            if($last == 't') {
+                $per = 1024 * 1024 * 1024 * 1024;
+            }
+            elseif($last == 'g') {
+                $per = 1024 * 1024 * 1024;
+            }
+            elseif($last == 'm') {
+                $per = 1024 * 1024;
+            }
+            elseif($last == 'k') {
+                $per = 1024;
+            }
+            else {
+                $per = 1;
+            }
+
+            return $total * $per;
+        };
+
+        $format_detail = function ($detail) use ($calc_bytes)
+        {
+            $detail = trim($detail);
+            $items = explode(',',$detail);
+            if(count($items) < 2) {
+                return $detail;
+            }
+
+            $total = strtolower(trim($items[0]));
+            $avaliable = strtolower(trim($items[1]));
+
+            $ratio = $calc_bytes($avaliable) / $calc_bytes($total);
+
+            $ratio = number_format($ratio * 100,2);
+
+            return "$avaliable / $total = $ratio%";
+        };
+
+        $disk_monitoring_data = function ($str)  use ($convert_time,$format_detail)
+        {
+            $result = [];
+            $details = preg_split("/[\s#]+/", $str, 0, PREG_SPLIT_DELIM_CAPTURE);
+            foreach ($details as $item)
+            {
+                $data = explode(':', $item);
+                if(count($data) != 2) continue;
+                $dir = trim($data[0]);
+                if($dir == 'cur_time') {
+                    $val = $convert_time(trim($data[1]));
+                } else {
+                    $val = $format_detail($data[1]);
+                }
+
+                $result[$dir] =  $val;
+            }
+            return $result;
+        };
+
+        $details = $disk_monitoring_data($str);
+
+
+
+        $cur_time = $convert_time('2023-08-24-16-08-02');
+
+
+
+
+        $arr = "/mnt:7.3T,5.7T";
+        $data = preg_split("/[\s:]+/", $arr, 0, PREG_SPLIT_DELIM_CAPTURE);
+        $key = ltrim($data[0], '/');
+        $value = $data[1];
+
+//        global $config;
+//        $servers = $config['server'];
+//        $ins = Cache::getInstance('cacheredis');
+//        $name = 'disk_monitor';
+//        $disk_monitoring_data = $ins->hget($name, '');
+//        $result = [];
+//        $cur_time = time();
+//        $ago_time = 0;
+//
+//        array_map(function ($ip, $server_name) use($disk_monitoring_data) {
+//            $hash_str = $disk_monitoring_data[$ip];
+//            $item = [];
+//            if(!empty($hash_str)){
+//                $hash_str = str_replace('\t', "", $hash_str);
+//                $arrs = explode('#', $hash_str);
+//            }
+//        }, $servers);
+    }
+
+
+
+}
+//docker-compose run phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestAccount::testLog)( .*)?$/" --test-suffix TestAccount.php /var/www/html/test

+ 16 - 8
util/arw_monitor.sh

@@ -13,9 +13,11 @@ function df_check() {
   # shellcheck disable=SC2068
   for dir in ${df_dirs[@]}
   do
-    ret=$(df -h | grep $dir | awk 'BEGIN{OFS=","}{print $2,$4}')
-    key=${dir////_}
-    reports[$key]=$ret
+    if [ -d "$dir" ]; then
+      ret=$(df -h | grep $dir | awk 'BEGIN{OFS=","}{print $2,$4}')
+      key=${dir}
+      reports[$key]=$ret
+    fi
   done
 }
 
@@ -23,10 +25,12 @@ function du_check() {
   # shellcheck disable=SC2068
   for dir in ${du_dirs[@]}
   do
-    cd $dir
-    ret=$(du -h -d 0)
-    key=${dir////_}
-    reports[$key]=$ret
+    if [ -d "$dir" ]; then
+      cd $dir
+      ret=$(du -h -d 0)
+      key=${dir}
+      reports[$key]=$ret
+    fi
   done
 }
 
@@ -36,7 +40,11 @@ du_check
 str_report=''
 for key in $(echo ${!reports[*]})
 do
-    str_report="$str_report#$key:${reports[$key]}"
+    if [ ! -n "$str_report" ]; then
+      str_report="$key:${reports[$key]}"
+    else
+      str_report="$str_report#$key:${reports[$key]}"
+    fi
 done
 
 redis_host=$XYZ_REDIS_HOST