Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Add Cpu Usage % to header for standalone server #3840

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions web/ajax/status.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// Call the functions we want to dynamically update
$data['getBandwidthHTML'] = getBandwidthHTML($bandwidth_options, $user);
$data['getSysLoadHTML'] = getSysLoadHTML();
$data['getCpuUsageHTML'] = getCpuUsageHTML();
$data['getDbConHTML'] = getDbConHTML();
$data['getStorageHTML'] = getStorageHTML();
//$data['getShmHTML'] = getShmHTML();
Expand Down
75 changes: 75 additions & 0 deletions web/includes/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,5 +198,80 @@ public function CpuLoad() {
}
return $this->CpuLoad;
}

public function getServerLoadData($mode) {
$stats = false;
$fileName = null;
$fileNameCurStat = sys_get_temp_dir()."/cpu_usage";

if ($mode == "cur") {
$fileName = "/proc/stat";
} else if ($mode == "prev") {
$fileName = $fileNameCurStat;
}
if (is_readable($fileName)) {
$stats = @file_get_contents($fileName);
}

if ($stats !== false) {
//Save current state
if ($mode == "cur") {
file_put_contents($fileNameCurStat, $stats);
}

// Remove double spaces to make it easier to extract values with explode()
$stats = preg_replace("/[[:blank:]]+/", " ", $stats);

// Separate lines
$stats = str_replace(array("\r\n", "\n\r", "\r"), "\n", $stats);
$stats = explode("\n", $stats);

// Separate values and find line for main CPU load
foreach ($stats as $statLine) {
$statLineData = explode(" ", trim($statLine));

// Found!
if ((count($statLineData) >= 5) && ($statLineData[0] == "cpu")) {
return array(
$statLineData[1],
$statLineData[2],
$statLineData[3],
$statLineData[4],
);
}
}
}

return null;
}

// Returns server load in percent (just number, without percent sign)
public function CpuUsagePercent() {
if ($this->CpuUsagePercent == -1) {
// Collect 2 samples - each with 1 second period
// See: https://de.wikipedia.org/wiki/Load#Der_Load_Average_auf_Unix-Systemen
$statData1 = $this->getServerLoadData("prev");
$statData2 = $this->getServerLoadData("cur");

if ((!is_null($statData1)) && (!is_null($statData2))) {
// Get difference
$statData2[0] -= $statData1[0];
$statData2[1] -= $statData1[1];
$statData2[2] -= $statData1[2];
$statData2[3] -= $statData1[3];

// Sum up the 4 values for User, Nice, System and Idle and calculate
// the percentage of idle time (which is part of the 4 values!)
$cpuTime = $statData2[0] + $statData2[1] + $statData2[2] + $statData2[3];

// Invert percentage to get CPU time, not idle time
$this->CpuUsagePercent = 100 - ($statData2[3] * 100 / $cpuTime);
}
}

return $this->CpuUsagePercent;
}


} # end class Server
?>
2 changes: 1 addition & 1 deletion web/skins/classic/includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ function getCpuUsageHTML() {
$result = '';
if ( !canView('System') ) return $result;
global $thisServer;
if ($thisServer and $thisServer->Id()) {
if ($thisServer) {
$result .= '<li id="getCpuUsagesHTML" class="CpuUsage nav-item mx-2">'.PHP_EOL;
$result .= '&nbsp;'.translate('Cpu').': '.number_format($thisServer->CpuUsagePercent(), 1, '.', '').'%'.PHP_EOL;
$result .= '</li>'.PHP_EOL;
Expand Down
Loading