Skip to content

Commit 43a6dd8

Browse files
authored
Merge pull request #431 from nextcloud/backport/427/stable25
[stable25] Fix FreeBSD: Add Support for Swap Output on TrueNas Core
2 parents 97f0913 + 5cdf258 commit 43a6dd8

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

lib/OperatingSystems/FreeBSD.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ public function getMemory(): Memory {
4141
}
4242

4343
$matches = [];
44-
$pattern = '/(?>\/dev\/\w+)\s+(?>\d+)\s+(?<Used>\d+)\s+(?<Avail>\d+)\s+(?<Capacity>\d+)/';
44+
$pattern = '/(?>\/dev\/\S+)\s+(?>\d+)\s+(?<Used>\d+)\s+(?<Avail>\d+)\s+(?<Capacity>\d+)/';
4545

4646
$result = preg_match_all($pattern, $swapinfo, $matches);
47-
if ($result === 1) {
48-
$data->setSwapTotal((int)((int)$matches['Avail'][0] / 1024));
49-
$data->setSwapFree(($data->getSwapTotal() - (int)((int)$matches['Used'][0] / 1024)));
47+
if ($result !== 0) {
48+
$data->setSwapTotal((int)((int)array_sum($matches['Avail']) / 1024));
49+
$data->setSwapFree(($data->getSwapTotal() - (int)((int)array_sum($matches['Used']) / 1024)));
5050
}
5151

5252
unset($matches, $result);

tests/data/truenas_core_meminfo

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
137434759168
2+
4096
3+
5390734
4+
0
5+
1044334

tests/data/truenas_core_swapinfo

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Device 1K-blocks Used Avail Capacity
2+
/dev/mirror/swap0.eli 2097152 0 2097152 0%
3+
/dev/mirror/swap1.eli 2097152 0 2097152 0%
4+
/dev/mirror/swap2.eli 2097152 0 2097152 0%
5+
/dev/mirror/swap3.eli 16777216 0 16777216 0%
6+
/dev/mirror/swap4.eli 2097152 0 2097152 0%
7+
Total 25165824 0 25165824 0%

tests/lib/FreeBSDTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,22 @@ public function testGetMemoryNoData(): void {
9696
$this->assertEquals(new Memory(), $this->os->getMemory());
9797
}
9898

99+
public function testGetMemoryTruenasSwapinfo(): void {
100+
$this->os->method('executeCommand')
101+
->willReturnMap([
102+
['/usr/sbin/swapinfo -k', file_get_contents(__DIR__ . '/../data/truenas_core_swapinfo')],
103+
['/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')],
104+
]);
105+
106+
$memory = $this->os->getMemory();
107+
108+
$this->assertEquals(131068, $memory->getMemTotal());
109+
$this->assertEquals(-1, $memory->getMemFree());
110+
$this->assertEquals(25136, $memory->getMemAvailable());
111+
$this->assertEquals(24576, $memory->getSwapTotal());
112+
$this->assertEquals(24576, $memory->getSwapFree());
113+
}
114+
99115
public function testGetNetworkInterfaces(): void {
100116
$this->os->method('executeCommand')
101117
->willReturnCallback(static function ($command) {

0 commit comments

Comments
 (0)