diff --git a/application/controllers/GetStatus.php b/application/controllers/GetStatus.php index 3845dd5..8a28a0a 100644 --- a/application/controllers/GetStatus.php +++ b/application/controllers/GetStatus.php @@ -62,20 +62,44 @@ function replaceAll(Array $pattern, String $target) : String { } return $rltArray; } - private function recoverType(Array $resultArray) : Array { - if ( ! function_exists('isJson') ) { - function isJson(String $data) : bool { - json_decode($data); - return json_last_error() === JSON_ERROR_NONE; + + + + private function isJson(String $resultArray) : bool { + json_decode($resultArray); + return json_last_error() === JSON_ERROR_NONE; + } + + + private function getCpuAvgTemp(String $value) { + if ( ! function_exists('tempAvg') ) { + function tempAvg(Array $arr) : Int { + return array_reduce($arr, function($first, $secondVal){ + return (floatval($first)) + (floatval($secondVal)); + }) / count($arr); } } + + return $this->isJson($value) ? $value : tempAvg(explode(' ', $value)); + + } + + + + + private function recoverType(Array $resultArray) : Array { + for ($i = 0, $key = array_keys($resultArray), $len = count($resultArray); $i < $len; $i++) { - $resultArray[$key[$i]] = is_numeric($resultArray[$key[$i]]) ? $resultArray[$key[$i]] + 0 : (! is_array( $resultArray[$key[$i]] )) && isJson( $resultArray[$key[$i]] ) ? json_decode( $resultArray[$key[$i]], TRUE) : $resultArray[$key[$i]]; + $resultArray[$key[$i]] = is_numeric($resultArray[$key[$i]]) ? $resultArray[$key[$i]] + 0 : (! is_array( $resultArray[$key[$i]] )) && $this->isJson( $resultArray[$key[$i]] ) ? json_decode( $resultArray[$key[$i]], TRUE) : $resultArray[$key[$i]]; } return $resultArray; } + + + private function getServerInfo() : Array { + $setting = [ 'depenDency' => [ // 'bc', 'mpstat', // im-sensors @@ -86,14 +110,16 @@ private function getServerInfo() : Array { 'option' => [ // package name // 'im-sensors','testPackage' - 'im-sensors', //'testPackage' + 'sensors', //'testPackage' ] ], 'commandList' => [ 'option' => [ - 'imSensors' => '$(ls -a)', + 'cpuTemp' => "$(sensors | grep Core | awk '{print $3}' | sed \"s/+//\" | sed \"s/°C//\")" + // 'cpuTemp' => '$(cat /sys/devices/virtual/thermal/thermal_zone*/temp)', // 'testPackage' => '$(asdfasdf df df)' ], + 'ipAddress' => '$(ifconfig | head -2 | tail -1 | awk \'{print $2}\' | cut -f 2 -d ":")', 'diskPercent' => "$(df -P | grep -v ^Filesystem | awk '{total += $2; used += $3} END {printf(\"%.2f\",used/total * 100.0)}')", 'ramPercent' => "$(free | grep Mem | awk '{ printf(\"%.2f\",$3/$2 * 100.0) }')", @@ -105,19 +131,20 @@ private function getServerInfo() : Array { $this->load->model('ExecCommand'); $this->load->library('GenerateCommand', $setting); - if ( ! $this->session->firstInfoCommand ) { - $this->session->set_userdata('firstInfoCommand', $this->generatecommand->main()); - } - $getCommandResult = $this->ExecCommand->execUserCommand($this->generatecommand->main()); + $getCommandResult = $this->ExecCommand->execUserCommand( $this->generatecommand->main() ); if ( $getCommandResult['status'] ) { $getCommandResult = json_decode( $getCommandResult['message'], TRUE); if ( $getCommandResult['status'] ) { + + $getCommandResult['cpuTemp'] = $this->getCpuAvgTemp($getCommandResult['cpuTemp']); + $getCommandResult['diskInfo'] = $this->dfParser($getCommandResult['diskInfo']); $getCommandResult = $this->recoverType($getCommandResult); } } + return $getCommandResult; } @@ -132,12 +159,12 @@ public function interValServerInfo() : void { ], 'option' => [ // 'im-sensors','testPackage' - 'im-sensors', //'testPackage' + 'sensors', //'testPackage' ] ], 'commandList' => [ 'option' => [ - 'imSensors' => '$(ls -a)', + 'cpuTemp' => "$(sensors | grep Core | awk '{print $3}' | sed \"s/+//\" | sed \"s/°C//\")", // 'testPackage' => '$(asdfasdf df df)' ], @@ -150,18 +177,19 @@ public function interValServerInfo() : void { $this->load->model('ExecCommand'); $this->load->library('GenerateCommand', $setting); - if ( ! $this->session->intervalCommand ) { - $this->session->set_userdata('intervalCommand', $this->generatecommand->main()); - } - $getCommandResult = $this->ExecCommand->execUserCommand($this->session->intervalCommand); - + + $getCommandResult = $this->ExecCommand->execUserCommand( $this->generatecommand->main() ); + if ( $getCommandResult['status'] ) { $getCommandResult = json_decode( $getCommandResult['message'], TRUE); if ( $getCommandResult['status'] ) { + + $getCommandResult['cpuTemp'] = $this->getCpuAvgTemp($getCommandResult['cpuTemp']); $getCommandResult = $this->recoverType($getCommandResult); + } } - // print_r($getCommandResult); + $this->json->echo($getCommandResult); } @@ -182,6 +210,7 @@ public function renderMainInfo() { } else { $retArray['message'] = $this->config->base_url(); } + $this->json->echo($retArray); } diff --git a/application/libraries/GenerateCommand.php b/application/libraries/GenerateCommand.php index 49f19ef..af89a02 100644 --- a/application/libraries/GenerateCommand.php +++ b/application/libraries/GenerateCommand.php @@ -57,10 +57,11 @@ private function requireCheckCommand(Array $packageList) : String { $command = "packageExists(){ echo \"$(command -v \"$1\" > /dev/null; echo \$?)\"; };"; for ( $i = 0, $requirement = $packageList['require'], $len = count($requirement); $i < $len; $i++) { + if ( $i === 0 ) { - $command .= "if [ \"$(packageExists {$requirement[$i]['packageName']})\" -eq 1 ]; then "; + $command .= "if [ \"$(packageExists ".$requirement[$i]['packageName'].")\" -eq 1 ]; then "; } else { - $command .= "elif [ \"$(packageExists {$requirement[$i]['packageName']})\" -eq 1 ]; then "; + $command .= "elif [ \"$(packageExists ".$requirement[$i]['packageName'].")\" -eq 1 ]; then "; } $command .= $this->bashJson($requirement[$i], TRUE); } diff --git a/application/views/status/body.php b/application/views/status/body.php index 5d970f0..86b249c 100644 --- a/application/views/status/body.php +++ b/application/views/status/body.php @@ -43,7 +43,7 @@
- +
diff --git a/html/static/main/js/userData.js b/html/static/main/js/userData.js index b0eca0f..93963e5 100644 --- a/html/static/main/js/userData.js +++ b/html/static/main/js/userData.js @@ -58,26 +58,27 @@ class checkUserData { return statObject; } - processData() { + async processData() { const statusObject = this.isEmptyData(this.generateData(this.formTag)); if ( statusObject.stat ) { - - const jsonData = this.sendUserData({ - 'action' : this.formTag.getAttribute('action'), - 'data' : statusObject.data + + const checkStatus = req => req.ok && req.status === 200 && req.statusText === 'OK'; + const req = await fetch(this.formTag.getAttribute('action'), { + method: 'POST', + body: statusObject.data }); - - if ( jsonData ) { - jsonData.then((resp) => { - if (resp.status && resp.code) { - location.href = resp.page; - } else { - alert(res[this.userLang][this.ErrorTag.idxError][this.ErrorTag.code][resp.code]); - } - }); + + if (checkStatus(req)) { + const resp = await req.json(); + if ( resp.status && resp.code ) { + location.href = resp.page; + } else { + alert(res[this.userLang][this.ErrorTag.idxError][this.ErrorTag.code][resp.code]); + } } - + } else { + if (this.userLang === 'ko-KR') { res[this.userLang][this.ErrorTag.idxError][this.ErrorTag.code][this.ErrorTag.tag][0] = statusObject.data; @@ -86,15 +87,8 @@ class checkUserData { } alert(res[this.userLang][this.ErrorTag.idxError][this.ErrorTag.code][this.ErrorTag.tag].join('')); - return false; + return ; } } - - sendUserData(dataSet) { - return fetch(dataSet.action, { - method: 'POST', - body: dataSet.data - }).then(resp => resp.ok && resp.status === 200 ? resp.json() : false); - } } \ No newline at end of file diff --git a/html/static/status/js/gaugeChart.js b/html/static/status/js/gaugeChart.js index 7863d02..2ee4cba 100644 --- a/html/static/status/js/gaugeChart.js +++ b/html/static/status/js/gaugeChart.js @@ -5,6 +5,8 @@ class Gauge { this.body = null; this.placeholderName = placeholderName; this.configure(configuration); + } else { + console.error('set selector Name and config') } } configure(configuration) { @@ -25,7 +27,8 @@ class Gauge { this.config.redColor = configuration.redColor || '#DC3912'; this.config.initValue = configuration.initValue || 0; this.config.transitionDuration = configuration.transitionDuration || 500; - + this.config.isTemp = configuration.isTemp; + this.fontSize = null; this.render(); } @@ -200,11 +203,10 @@ class Gauge { .attr('transform', () => `translate(${this.config.cx},${this.config.cy}) rotate(270)`); } - - + redraw(value, transitionDuration) { let pointerContainer = this.body.select('.pointerContainer'); - pointerContainer.selectAll('text').text(`${value}%`); + pointerContainer.selectAll('text').text(`${value}${this.config.isTemp ? '℃' : '%'}`); let pointer = pointerContainer.selectAll('path'); pointer.transition() .duration(transitionDuration ? transitionDuration : this.config.transitionDuration) diff --git a/html/static/status/js/render.js b/html/static/status/js/render.js index 2274bb9..8b2a9e4 100644 --- a/html/static/status/js/render.js +++ b/html/static/status/js/render.js @@ -5,12 +5,13 @@ class renderInterFace { } this.userLang = navigator.language || navigator.userLanguage; this.pageName = 'viewStatus'; + this.useCpuTemp = false; } - renderUrl(urlObject = {}) { + renderUrl({className, methodName}) { let retVal = false; - if (urlObject.className && urlObject.methodName) { + if (className && methodName) { retVal = [ - location.origin, urlObject.className, urlObject.methodName + location.origin, className, methodName ].join('/'); } return retVal; @@ -41,6 +42,7 @@ class renderServerInfo extends renderInterFace { const json = await req.json(); if ( json.message.serverInfo.status ) { + this.imgInput.insertAdjacentHTML('afterbegin', json.message.userTemplate); document.getElementById('card_main').insertAdjacentHTML('afterbegin', json.message.serverInfo.diskInfo.map((i) => this.renderTemplate(i)).join('')) @@ -57,15 +59,16 @@ class renderServerInfo extends renderInterFace { } } - getConfig(name, value) { + getConfig({labelName, value, isTemprature}) { return { - label: name, + label: labelName, size: 120, min: 0, max: 100, initValue: value, minorTicks: 5, yellowZones: [{from: this.min}], + isTemp : isTemprature, range: null, get yellowZones () { @@ -82,13 +85,26 @@ class renderServerInfo extends renderInterFace { } }; } - + renderGraph(info) { - const chart = { - 'cpu': new Gauge('chartDiv1', this.getConfig('cpu', info.cpuUsage)), - 'ram': new Gauge('chartDiv2', this.getConfig('ram', info.ramPercent)), - 'disk': new Gauge('chartDiv3', this.getConfig('disk', info.diskPercent)) - }; + let chart = null; + + if (typeof(info.cpuTemp.status) === 'undefined') { + this.useCpuTemp = true; + chart = { + 'cpu': new Gauge('chartDiv1', this.getConfig({labelName: 'cpu', value: info.cpuUsage, isTemprature: false})), + 'ram': new Gauge('chartDiv2', this.getConfig({labelName: 'ram', value: info.ramPercent, isTemprature: false})), + 'disk': new Gauge('chartDiv3', this.getConfig({labelName: 'disk', value: info.diskPercent, isTemprature: false})), + 'cpuTemp' : new Gauge('chartDiv4', this.getConfig({labelName: 'Cpu Temp', value: info.cpuTemp, isTemprature: true})) + }; + + } else { + chart = { + 'cpu': new Gauge('chartDiv1', this.getConfig({labelName: 'cpu', value: info.cpuUsage, isTemprature: false})), + 'ram': new Gauge('chartDiv2', this.getConfig({labelName: 'ram', value: info.ramPercent, isTemprature: false})), + 'disk': new Gauge('chartDiv3', this.getConfig({labelName: 'disk', value: info.diskPercent, isTemprature: false})), + } + } let isSend = true; const intervalUrl = this.renderUrl({ @@ -96,11 +112,6 @@ class renderServerInfo extends renderInterFace { methodName : 'interValServerInfo' }); let json = null; - function getRandomInt(min, max) { - min = Math.ceil(min); - max = Math.floor(max); - return Math.floor(Math.random() * (max - min + 1)) + min; - } setInterval( async () => { if ( isSend ) { @@ -110,10 +121,14 @@ class renderServerInfo extends renderInterFace { }); if ( this.checkStatus(getServerInfo) ) { json = await getServerInfo.json(); + if ( json.status ) { chart.cpu.redraw(json.cpuUsage); chart.ram.redraw(json.ramPercent); chart.disk.redraw(json.diskPercent); + if ( this.useCpuTemp ) { + chart.cpuTemp.redraw(json.cpuTemp); + } } isSend = true; }