Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable25] Fix FreeBSD: Add Support for Swap Output on TrueNas Core #431

Merged
merged 2 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/OperatingSystems/FreeBSD.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ public function getMemory(): Memory {
}

$matches = [];
$pattern = '/(?>\/dev\/\w+)\s+(?>\d+)\s+(?<Used>\d+)\s+(?<Avail>\d+)\s+(?<Capacity>\d+)/';
$pattern = '/(?>\/dev\/\S+)\s+(?>\d+)\s+(?<Used>\d+)\s+(?<Avail>\d+)\s+(?<Capacity>\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);
Expand Down
5 changes: 5 additions & 0 deletions tests/data/truenas_core_meminfo
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
137434759168
4096
5390734
0
1044334
7 changes: 7 additions & 0 deletions tests/data/truenas_core_swapinfo
Original file line number Diff line number Diff line change
@@ -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%
16 changes: 16 additions & 0 deletions tests/lib/FreeBSDTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down