diff --git a/lib/OperatingSystems/DefaultOs.php b/lib/OperatingSystems/DefaultOs.php index e69c7f74..0e2aa456 100644 --- a/lib/OperatingSystems/DefaultOs.php +++ b/lib/OperatingSystems/DefaultOs.php @@ -50,12 +50,8 @@ public function getMemory(): Memory { } foreach ($matches['Key'] as $i => $key) { - $value = (int)$matches['Value'][$i]; - $unit = $matches['Unit'][$i]; - - if ($unit === 'kB') { - $value *= 1024; - } + // Value is always in KB: https://github.com/torvalds/linux/blob/c70672d8d316ebd46ea447effadfe57ab7a30a50/fs/proc/meminfo.c#L58-L60 + $value = (int)($matches['Value'][$i] / 1024); switch ($key) { case 'MemTotal': @@ -195,7 +191,7 @@ public function getDiskInfo(): array { $data = []; try { - $disks = $this->executeCommand('df -TP'); + $disks = $this->executeCommand('df -TPk'); } catch (\RuntimeException $e) { return $data; } @@ -216,8 +212,8 @@ public function getDiskInfo(): array { $disk = new Disk(); $disk->setDevice($filesystem); $disk->setFs($matches['Type'][$i]); - $disk->setUsed((int)$matches['Used'][$i] * 1024); - $disk->setAvailable((int)$matches['Available'][$i] * 1024); + $disk->setUsed((int)($matches['Used'][$i] / 1024)); + $disk->setAvailable((int)($matches['Available'][$i] / 1024)); $disk->setPercent($matches['Capacity'][$i]); $disk->setMount($matches['Mounted'][$i]); diff --git a/lib/OperatingSystems/FreeBSD.php b/lib/OperatingSystems/FreeBSD.php index 4c27c6c5..c61d4234 100644 --- a/lib/OperatingSystems/FreeBSD.php +++ b/lib/OperatingSystems/FreeBSD.php @@ -53,8 +53,8 @@ public function getMemory(): Memory { $result = preg_match_all($pattern, $swapinfo, $matches); if ($result === 1) { - $data->setSwapTotal((int)$matches['Avail'][0]); - $data->setSwapFree($data->getSwapTotal() - (int)$matches['Used'][0]); + $data->setSwapTotal((int)($matches['Avail'][0] / 1024)); + $data->setSwapFree(($data->getSwapTotal() - (int)($matches['Used'][0]) / 1024)); } unset($matches, $result); @@ -67,8 +67,8 @@ public function getMemory(): Memory { $lines = explode("\n", $meminfo); if (count($lines) > 4) { - $data->setMemTotal((int)$lines[0]); - $data->setMemAvailable((int)$lines[1] * ((int)$lines[2] + (int)$lines[3] + (int)$lines[4])); + $data->setMemTotal((int)($lines[0] / 1024 / 1024)); + $data->setMemAvailable((int)(($lines[1] * ($lines[2] + $lines[3] + $lines[4])) / 1024 / 1024)); } unset($lines); @@ -239,8 +239,8 @@ public function getDiskInfo(): array { $disk = new Disk(); $disk->setDevice($filesystem); $disk->setFs($matches['Type'][$i]); - $disk->setUsed((int)$matches['Used'][$i] * 1024); - $disk->setAvailable((int)$matches['Available'][$i] * 1024); + $disk->setUsed((int)($matches['Used'][$i] / 1024)); + $disk->setAvailable((int)($matches['Available'][$i] / 1024)); $disk->setPercent($matches['Capacity'][$i]); $disk->setMount($matches['Mounted'][$i]); diff --git a/lib/Resources/Disk.php b/lib/Resources/Disk.php index 1c6893af..150a8ab2 100644 --- a/lib/Resources/Disk.php +++ b/lib/Resources/Disk.php @@ -49,18 +49,30 @@ public function setFs(string $fs): void { $this->fs = $fs; } + /** + * @return int in MB + */ public function getUsed(): int { return $this->used; } + /** + * @param int $used in MB + */ public function setUsed(int $used): void { $this->used = $used; } + /** + * @return int in MB + */ public function getAvailable(): int { return $this->available; } + /** + * @param int $available in MB + */ public function setAvailable(int $available): void { $this->available = $available; } diff --git a/lib/Resources/Memory.php b/lib/Resources/Memory.php index 71bd6036..8123f5b2 100644 --- a/lib/Resources/Memory.php +++ b/lib/Resources/Memory.php @@ -32,42 +32,72 @@ class Memory { private $swapTotal = -1; private $swapFree = -1; + /** + * @return int in MB + */ public function getMemTotal(): int { return $this->memTotal; } + /** + * @param int $memTotal in MB + */ public function setMemTotal(int $memTotal): void { $this->memTotal = $memTotal; } + /** + * @return int in MB + */ public function getMemFree(): int { return $this->memFree; } + /** + * @param int $memFree in MB + */ public function setMemFree(int $memFree): void { $this->memFree = $memFree; } + /** + * @return int in MB + */ public function getMemAvailable(): int { return $this->memAvailable; } + /** + * @param int $memAvailable in MB + */ public function setMemAvailable(int $memAvailable): void { $this->memAvailable = $memAvailable; } + /** + * @return int in MB + */ public function getSwapTotal(): int { return $this->swapTotal; } + /** + * @param int $swapTotal in MB + */ public function setSwapTotal(int $swapTotal): void { $this->swapTotal = $swapTotal; } + /** + * @return int in MB + */ public function getSwapFree(): int { return $this->swapFree; } - + + /** + * @param int $swapFree in MB + */ public function setSwapFree(int $swapFree): void { $this->swapFree = $swapFree; } diff --git a/templates/settings-admin.php b/templates/settings-admin.php index f39fd6ef..c43b427c 100644 --- a/templates/settings-admin.php +++ b/templates/settings-admin.php @@ -25,8 +25,8 @@ style('serverinfo', 'style'); -function FormatBytes($byte) { - $unim = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']; +function FormatMegabytes($byte) { + $unim = ['MB', 'GB', 'TB', 'PB']; $count = 0; while ($byte >= 1024) { $count++; @@ -66,7 +66,7 @@ function FormatBytes($byte) {