fix: update SSH command execution to use semicolons for reliability and improve disk usage calculation

This commit is contained in:
infosave2007
2026-04-24 15:48:42 +03:00
parent c9792a5d5d
commit aae920a5c2
+6 -5
View File
@@ -88,13 +88,14 @@ class ServerMonitoring
public function collectMetrics(): array public function collectMetrics(): array
{ {
// Combine all metric commands into one SSH call // Combine all metric commands into one SSH call
$combinedCmd = implode(' && ', [ // Use semicolons instead of && to ensure all commands execute even if one fails
$combinedCmd = implode('; ', [
"echo CPU_START", "echo CPU_START",
"top -bn1 | grep 'Cpu(s)' | sed 's/.*, *\\([0-9.]*\\)%* id.*/\\1/' | awk '{print 100 - \$1}'", "top -bn1 | grep 'Cpu(s)' | sed 's/.*, *\\([0-9.]*\\)%* id.*/\\1/' | awk '{print 100 - \$1}'",
"echo RAM_START", "echo RAM_START",
"free -m | grep Mem | awk '{print \$3, \$2}'", "free -m | grep Mem | awk '{print \$3, \$2}'",
"echo DISK_START", "echo DISK_START",
"df -BG / | tail -1 | awk '{gsub(/G/,\"\"); print \$3, \$2}'", "df -BM / | tail -1 | awk '{print int(\$3/1024), int(\$2/1024)}'",
"echo NET_RX_START", "echo NET_RX_START",
"cat /sys/class/net/\$(ip route | grep default | awk '{print \$5}' | head -1)/statistics/rx_bytes", "cat /sys/class/net/\$(ip route | grep default | awk '{print \$5}' | head -1)/statistics/rx_bytes",
"echo NET_TX_START", "echo NET_TX_START",
@@ -161,7 +162,7 @@ class ServerMonitoring
$txMbps = null; $txMbps = null;
if ($rxBytes1 !== null && $txBytes1 !== null) { if ($rxBytes1 !== null && $txBytes1 !== null) {
sleep(1); sleep(1);
$netCmd = implode(' && ', [ $netCmd = implode('; ', [
"echo RX", "echo RX",
"cat /sys/class/net/\$(ip route | grep default | awk '{print \$5}' | head -1)/statistics/rx_bytes", "cat /sys/class/net/\$(ip route | grep default | awk '{print \$5}' | head -1)/statistics/rx_bytes",
"echo TX", "echo TX",
@@ -657,8 +658,8 @@ class ServerMonitoring
// Password authentication // Password authentication
$sshOptions .= " -o PreferredAuthentications=password -o PubkeyAuthentication=no"; $sshOptions .= " -o PreferredAuthentications=password -o PubkeyAuthentication=no";
$sshCmd = sprintf( $sshCmd = sprintf(
"sshpass -p '%s' ssh -p %d %s %s@%s %s 2>/dev/null", "sshpass -p %s ssh -p %d %s %s@%s %s 2>/dev/null",
$password, escapeshellarg($password),
$port, $port,
$sshOptions, $sshOptions,
$username, $username,