From f40d3db8cf9e33623860a6706fe60faf90ce11e2 Mon Sep 17 00:00:00 2001 From: IgorA100 Date: Sat, 24 Feb 2024 19:19:31 +0300 Subject: [PATCH 1/4] Add function for Cpu Usage % to header --- web/includes/Server.php | 63 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/web/includes/Server.php b/web/includes/Server.php index bcab952d84..0726303237 100644 --- a/web/includes/Server.php +++ b/web/includes/Server.php @@ -198,5 +198,68 @@ public function CpuLoad() { } return $this->CpuLoad; } + + public function getServerLoadData() { + if (is_readable("/proc/stat")) { + $stats = @file_get_contents("/proc/stat"); + + if ($stats !== false) { + // 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) { + if (is_readable("/proc/stat")) { + // Collect 2 samples - each with 1 second period + // See: https://de.wikipedia.org/wiki/Load#Der_Load_Average_auf_Unix-Systemen + $statData1 = $this->getServerLoadData(); + sleep(1); + $statData2 = $this->getServerLoadData(); + + 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 ?> From 38e7495857d36c4fbf6a696172aeaad7f8b6cb86 Mon Sep 17 00:00:00 2001 From: IgorA100 Date: Sat, 24 Feb 2024 19:35:05 +0300 Subject: [PATCH 2/4] Fix: Add Cpu Usage % to header for standalone server --- web/skins/classic/includes/functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/skins/classic/includes/functions.php b/web/skins/classic/includes/functions.php index 68488bfb96..1e393399be 100644 --- a/web/skins/classic/includes/functions.php +++ b/web/skins/classic/includes/functions.php @@ -390,7 +390,7 @@ function getCpuUsageHTML() { $result = ''; if ( !canView('System') ) return $result; global $thisServer; - if ($thisServer and $thisServer->Id()) { + if ($thisServer) { $result .= ''.PHP_EOL; From f41b60e9cdb118cdd40ef3beac818373e6598901 Mon Sep 17 00:00:00 2001 From: IgorA100 Date: Sun, 25 Feb 2024 13:10:18 +0300 Subject: [PATCH 3/4] Changing the algorithm for obtaining CPU usage values. Instead of 1 sec. period uses AJAX update period. --- web/includes/Server.php | 104 ++++++++++++++++++++++------------------ 1 file changed, 58 insertions(+), 46 deletions(-) diff --git a/web/includes/Server.php b/web/includes/Server.php index 0726303237..ec4027b94d 100644 --- a/web/includes/Server.php +++ b/web/includes/Server.php @@ -199,31 +199,45 @@ public function CpuLoad() { return $this->CpuLoad; } - public function getServerLoadData() { - if (is_readable("/proc/stat")) { - $stats = @file_get_contents("/proc/stat"); - - if ($stats !== false) { - // 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], - ); - } + 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], + ); } } } @@ -234,32 +248,30 @@ public function getServerLoadData() { // Returns server load in percent (just number, without percent sign) public function CpuUsagePercent() { if ($this->CpuUsagePercent == -1) { - if (is_readable("/proc/stat")) { - // Collect 2 samples - each with 1 second period - // See: https://de.wikipedia.org/wiki/Load#Der_Load_Average_auf_Unix-Systemen - $statData1 = $this->getServerLoadData(); - sleep(1); - $statData2 = $this->getServerLoadData(); - - 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); - } + // 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 ?> From 61d286b0bc254675acf18a3e7213e393d13937b3 Mon Sep 17 00:00:00 2001 From: IgorA100 Date: Sun, 25 Feb 2024 13:14:42 +0300 Subject: [PATCH 4/4] Added update getCpuUsageHTML() --- web/ajax/status.php | 1 + 1 file changed, 1 insertion(+) diff --git a/web/ajax/status.php b/web/ajax/status.php index dcd9bafe2b..e73c3b0342 100644 --- a/web/ajax/status.php +++ b/web/ajax/status.php @@ -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();