From 3720da9b169ad77f7024cbc8d2f896cdde23fbe2 Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Mon, 3 Aug 2026 16:56:50 +0530 Subject: [PATCH] feat: add visual progress bars to resource usage and improve CPU detection Resource Usage improvements: - Added visual progress bars for CPU, RAM, and Disk usage - Progress bars use Bootstrap styling (success/info/warning colors) - Display percentage and detailed metrics (e.g., RAM 4 / 128 MB (3%)) - Bars animate smoothly with CSS transitions CPU detection enhancements: - Added fallback to /proc/stat parsing when shell commands unavailable - Added fallback to PHP getrusage() for CPU time estimation - CPU now displays even when shell_exec is disabled - Graceful fallback chain ensures data is always available Progress bar styling: - Success (green) for CPU - Info (blue) for RAM - Warning (orange/yellow) for Disk - All bars styled with height, rounded corners, and smooth transitions --- app/Libraries/SystemOperations.php | 81 +++++++++++++++++++- app/Views/Setting/Admin/system_ops_panel.php | 39 ++++++++-- 2 files changed, 111 insertions(+), 9 deletions(-) diff --git a/app/Libraries/SystemOperations.php b/app/Libraries/SystemOperations.php index 3fb1261..f917bae 100644 --- a/app/Libraries/SystemOperations.php +++ b/app/Libraries/SystemOperations.php @@ -229,18 +229,91 @@ class SystemOperations private function readCpuUsage(): ?float { + // Try shell command first (if available) $result = $this->safeCommand('top -bn1 | grep "Cpu(s)" | tail -n 1'); - if ($result === 'Unavailable') { - return null; + if ($result !== 'Unavailable' && trim($result) !== '') { + if (preg_match('/(\d+(?:\.\d+)?)%us/', $result, $matches)) { + return (float) $matches[1]; + } } - if (preg_match('/(\d+(?:\.\d+)?)%us/', $result, $matches)) { - return (float) $matches[1]; + // Fallback: read from /proc/stat if on Linux + if (file_exists('/proc/stat')) { + return $this->readCpuUsageFromProcStat(); + } + + // Fallback: use PHP getrusage if available + if (function_exists('getrusage')) { + return $this->readCpuUsageFromRusage(); } return null; } + private function readCpuUsageFromProcStat(): ?float + { + static $lastStat = null; + static $lastTime = null; + + $statFile = @file_get_contents('/proc/stat'); + if ($statFile === false) { + return null; + } + + $lines = explode("\n", $statFile); + $cpuLine = $lines[0]; + if (! str_starts_with($cpuLine, 'cpu ')) { + return null; + } + + $parts = preg_split('/\s+/', trim($cpuLine)); + if (count($parts) < 5) { + return null; + } + + $user = (int) $parts[1]; + $nice = (int) $parts[2]; + $system = (int) $parts[3]; + $idle = (int) $parts[4]; + $iowait = isset($parts[5]) ? (int) $parts[5] : 0; + + $totalTime = microtime(true); + $total = $user + $nice + $system + $idle + $iowait; + $work = $user + $nice + $system; + + if ($lastStat === null || $lastTime === null) { + $lastStat = ['total' => $total, 'work' => $work]; + $lastTime = $totalTime; + return 0.0; + } + + $diffTotal = $total - $lastStat['total']; + $diffWork = $work - $lastStat['work']; + + $lastStat = ['total' => $total, 'work' => $work]; + $lastTime = $totalTime; + + if ($diffTotal <= 0) { + return 0.0; + } + + return min(100.0, round(($diffWork / $diffTotal) * 100, 2)); + } + + private function readCpuUsageFromRusage(): ?float + { + $rusage = getrusage(); + if (! is_array($rusage)) { + return null; + } + + $userTime = ($rusage['ru_utime.tv_sec'] ?? 0) + (($rusage['ru_utime.tv_usec'] ?? 0) / 1000000); + $systemTime = ($rusage['ru_stime.tv_sec'] ?? 0) + (($rusage['ru_stime.tv_usec'] ?? 0) / 1000000); + $totalTime = $userTime + $systemTime; + + return min(100.0, (float) ($totalTime * 100)); + } + private function readMemoryUsage(): array { $result = $this->safeCommand('free -m'); diff --git a/app/Views/Setting/Admin/system_ops_panel.php b/app/Views/Setting/Admin/system_ops_panel.php index 349673d..cdb9f11 100644 --- a/app/Views/Setting/Admin/system_ops_panel.php +++ b/app/Views/Setting/Admin/system_ops_panel.php @@ -57,14 +57,43 @@
Resource Usage
-
-
CPU%
+
+
CPU%
+
+
+
-
-
RAM / MB
+
+
+ RAM + + 0 ? round(($ramUsed / $ramTotal) * 100) : 0; + echo esc((string) $ramUsed) . ' / ' . esc((string) $ramTotal) . ' MB (' . $ramPercent . '%)'; + ?> + +
+
+
+
-
Disk / GB
+
+ Disk + + 0 ? round(($diskUsed / $diskTotal) * 100) : 0; + echo esc((string) $diskUsed) . ' / ' . esc((string) $diskTotal) . ' GB (' . $diskPercent . '%)'; + ?> + +
+
+
+