diff --git a/lib/OperatingSystems/FreeBSD.php b/lib/OperatingSystems/FreeBSD.php index e39d88b5..7d41132b 100644 --- a/lib/OperatingSystems/FreeBSD.php +++ b/lib/OperatingSystems/FreeBSD.php @@ -41,12 +41,12 @@ public function getMemory(): Memory { } $matches = []; - $pattern = '/(?>\/dev\/\w+)\s+(?>\d+)\s+(?\d+)\s+(?\d+)\s+(?\d+)/'; + $pattern = '/(?>\/dev\/\S+)\s+(?>\d+)\s+(?\d+)\s+(?\d+)\s+(?\d+)/'; $result = preg_match_all($pattern, $swapinfo, $matches); - if ($result === 1) { - $data->setSwapTotal((int)((int)$matches['Avail'][0] / 1024)); - $data->setSwapFree(($data->getSwapTotal() - (int)((int)$matches['Used'][0] / 1024))); + if ($result !== 0) { + $data->setSwapTotal((int)((int)array_sum($matches['Avail']) / 1024)); + $data->setSwapFree(($data->getSwapTotal() - (int)((int)array_sum($matches['Used']) / 1024))); } unset($matches, $result); diff --git a/tests/data/truenas_core_meminfo b/tests/data/truenas_core_meminfo new file mode 100644 index 00000000..19967888 --- /dev/null +++ b/tests/data/truenas_core_meminfo @@ -0,0 +1,5 @@ +137434759168 +4096 +5390734 +0 +1044334 diff --git a/tests/data/truenas_core_swapinfo b/tests/data/truenas_core_swapinfo new file mode 100644 index 00000000..fab29da6 --- /dev/null +++ b/tests/data/truenas_core_swapinfo @@ -0,0 +1,7 @@ +Device 1K-blocks Used Avail Capacity +/dev/mirror/swap0.eli 2097152 0 2097152 0% +/dev/mirror/swap1.eli 2097152 0 2097152 0% +/dev/mirror/swap2.eli 2097152 0 2097152 0% +/dev/mirror/swap3.eli 16777216 0 16777216 0% +/dev/mirror/swap4.eli 2097152 0 2097152 0% +Total 25165824 0 25165824 0% diff --git a/tests/lib/FreeBSDTest.php b/tests/lib/FreeBSDTest.php index 2f5c6e03..cbc25731 100644 --- a/tests/lib/FreeBSDTest.php +++ b/tests/lib/FreeBSDTest.php @@ -96,6 +96,22 @@ public function testGetMemoryNoData(): void { $this->assertEquals(new Memory(), $this->os->getMemory()); } + public function testGetMemoryTruenasSwapinfo(): void { + $this->os->method('executeCommand') + ->willReturnMap([ + ['/usr/sbin/swapinfo -k', file_get_contents(__DIR__ . '/../data/truenas_core_swapinfo')], + ['/sbin/sysctl -n hw.realmem hw.pagesize vm.stats.vm.v_inactive_count vm.stats.vm.v_cache_count vm.stats.vm.v_free_count', file_get_contents(__DIR__ . '/../data/truenas_core_meminfo')], + ]); + + $memory = $this->os->getMemory(); + + $this->assertEquals(131068, $memory->getMemTotal()); + $this->assertEquals(-1, $memory->getMemFree()); + $this->assertEquals(25136, $memory->getMemAvailable()); + $this->assertEquals(24576, $memory->getSwapTotal()); + $this->assertEquals(24576, $memory->getSwapFree()); + } + public function testGetNetworkInterfaces(): void { $this->os->method('executeCommand') ->willReturnCallback(static function ($command) {