Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 49 additions & 20 deletions application/controllers/GetStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) }')",
Expand All @@ -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;
}

Expand All @@ -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)'
],

Expand All @@ -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);
}

Expand All @@ -182,6 +210,7 @@ public function renderMainInfo() {
} else {
$retArray['message'] = $this->config->base_url();
}

$this->json->echo($retArray);
}

Expand Down
5 changes: 3 additions & 2 deletions application/libraries/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion application/views/status/body.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

<div class="content_wrap flex_02">
<div class="top-card">
<?php for ($i = 1; $i <= 3; $i++ ): ?>
<?php for ($i = 1; $i <= 4; $i++ ): ?>
<span class="gauge_chart" id="chartDiv<?=$i?>"></span>
<?php endfor; ?>
</div>
Expand Down
40 changes: 17 additions & 23 deletions html/static/main/js/userData.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
}
10 changes: 6 additions & 4 deletions html/static/status/js/gaugeChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
}

Expand Down Expand Up @@ -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)
Expand Down
47 changes: 31 additions & 16 deletions html/static/status/js/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(''))

Expand All @@ -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 () {
Expand All @@ -82,25 +85,33 @@ 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({
className: 'GetStatus',
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 ) {
Expand All @@ -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;
}
Expand Down