|
@@ -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));
|