Skip to content
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
2 changes: 1 addition & 1 deletion js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
e.preventDefault();
})

if (cpuload === 'N/A' || numCpus === -1) {
if (cpuload === false || numCpus === -1) {
$cpuFooterInfo.text(t('serverinfo', 'CPU info not available'));
$cpuLoadCanvas.addClass('hidden');
return;
Expand Down
14 changes: 5 additions & 9 deletions lib/OperatingSystems/Dummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
namespace OCA\ServerInfo\OperatingSystems;

use OCA\ServerInfo\Resources\CPU;
use OCA\ServerInfo\Resources\Memory;

class Dummy implements IOperatingSystem {
Expand All @@ -16,13 +17,13 @@ public function supported(): bool {
}

#[\Override]
public function getMemory(): Memory {
return new Memory();
public function getCPU(): CPU {
return new CPU('Unknown Processor', 1);
}

#[\Override]
public function getCpuName(): string {
return 'Unknown Processor';
public function getMemory(): Memory {
return new Memory();
}

#[\Override]
Expand Down Expand Up @@ -58,9 +59,4 @@ public function getDiskInfo(): array {
public function getThermalZones(): array {
return [];
}

#[\Override]
public function getCpuCount(): int {
return 1;
}
}
45 changes: 13 additions & 32 deletions lib/OperatingSystems/FreeBSD.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
namespace OCA\ServerInfo\OperatingSystems;

use OCA\ServerInfo\Resources\CPU;
use OCA\ServerInfo\Resources\Disk;
use OCA\ServerInfo\Resources\Memory;
use OCA\ServerInfo\Resources\NetInterface;
Expand All @@ -21,6 +22,18 @@ public function supported(): bool {
return false;
}

#[\Override]
public function getCPU(): CPU {
try {
$name = $this->executeCommand('/sbin/sysctl -n hw.model');
$cores = (int)$this->executeCommand('/sbin/sysctl -n kern.smp.cpus');
} catch (RuntimeException) {
$name = 'Unknown Processor';
$cores = 1;
}
return new CPU($name, $cores);
}

#[\Override]
public function getMemory(): Memory {
$data = new Memory();
Expand Down Expand Up @@ -59,38 +72,6 @@ public function getMemory(): Memory {
return $data;
}

#[\Override]
public function getCpuName(): string {
$data = 'Unknown Processor';

try {
$model = $this->executeCommand('/sbin/sysctl -n hw.model');
$threads = $this->executeCommand('/sbin/sysctl -n kern.smp.cpus');

if ((int)$threads === 1) {
$data = $model . ' (1 thread)';
} else {
$data = $model . ' (' . $threads . ' threads)';
}
} catch (RuntimeException $e) {
return $data;
}
return $data;
}

#[\Override]
public function getCpuCount(): int {
$numCpu = -1;

try {
$numCpu = intval($this->executeCommand('/sbin/sysctl -n hw.ncpu'));
} catch (RuntimeException) {
return $numCpu;
}

return $numCpu;
}

#[\Override]
public function getTime(): string {
try {
Expand Down
24 changes: 3 additions & 21 deletions lib/OperatingSystems/IOperatingSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace OCA\ServerInfo\OperatingSystems;

use OCA\ServerInfo\Resources\CPU;
use OCA\ServerInfo\Resources\Disk;
use OCA\ServerInfo\Resources\Memory;
use OCA\ServerInfo\Resources\NetInterface;
Expand All @@ -17,34 +18,15 @@
interface IOperatingSystem {
public function supported(): bool;

/**
* Get name of the processor.
*
* @return string
*/
public function getCpuName(): string;
public function getCPU(): CPU;

/**
* Get number of processors (threads).
*
* @return int
*/
public function getCpuCount(): int;
public function getMemory(): Memory;

/**
* Get disk info returns a list of Disk objects. Used and Available in bytes.
*
* @return Disk[]
*/
public function getDiskInfo(): array;

/**
* Get memory returns a Memory object. All values are in bytes.
*
* @return Memory
*/
public function getMemory(): Memory;

/**
* Get info about network connection.
*
Expand Down
83 changes: 29 additions & 54 deletions lib/OperatingSystems/Linux.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace OCA\ServerInfo\OperatingSystems;

use OCA\ServerInfo\Resources\CPU;
use OCA\ServerInfo\Resources\Disk;
use OCA\ServerInfo\Resources\Memory;
use OCA\ServerInfo\Resources\NetInterface;
Expand All @@ -24,6 +25,34 @@ public function supported(): bool {
return true;
}

#[\Override]
public function getCPU(): CPU {
$default = new CPU('Unknown Processor', 1);

try {
$cpuinfo = $this->readContent('/proc/cpuinfo');
} catch (RuntimeException) {
return $default;
}

$matches = [];

if (str_contains($cpuinfo, 'Raspberry Pi')) {
$pattern = '/Model\s+:\s(.+)/';
} elseif (str_contains($cpuinfo, 'PowerNV') || str_contains($cpuinfo, 'CHRP IBM pSeries')) {
$pattern = '/cpu\s+:\s+(.+)/';
} else {
$pattern = '/model name\s:\s(.+)/';
}

$result = preg_match_all($pattern, $cpuinfo, $matches);
if ($result === 0 || $result === false) {
return $default;
}

return new CPU($matches[1][0], substr_count($cpuinfo, "processor\t"));
}

#[\Override]
public function getMemory(): Memory {
$data = new Memory();
Expand Down Expand Up @@ -68,60 +97,6 @@ public function getMemory(): Memory {
return $data;
}

#[\Override]
public function getCpuName(): string {
$data = 'Unknown Processor';

try {
$cpuinfo = $this->readContent('/proc/cpuinfo');
} catch (RuntimeException $e) {
return $data;
}

$matches = [];

if (str_contains($cpuinfo, 'Raspberry Pi')) {
$pattern = '/Model\s+:\s(.+)/';
} elseif (str_contains($cpuinfo, 'PowerNV') || str_contains($cpuinfo, 'CHRP IBM pSeries')) {
$pattern = '/cpu\s+:\s+(.+)/';
} else {
$pattern = '/model name\s:\s(.+)/';
}

$result = preg_match_all($pattern, $cpuinfo, $matches);
if ($result === 0 || $result === false) {
return $data;
}

$model = $matches[1][0];

$threads = $this->getCpuCount();

if ($threads === 1) {
$data = $model . ' (1 thread)';
} else {
$data = $model . ' (' . $threads . ' threads)';
}

return $data;
}

#[\Override]
public function getCpuCount(): int {
$numCpu = -1;

try {
$cpuinfo = $this->readContent('/proc/cpuinfo');
} catch (RuntimeException $e) {
return $numCpu;
}

$pattern = '/processor\s+:\s(.+)/';

preg_match_all($pattern, $cpuinfo, $matches);
return count($matches[1]);
}

#[\Override]
public function getTime(): string {
try {
Expand Down
14 changes: 5 additions & 9 deletions lib/Os.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use OCA\ServerInfo\OperatingSystems\FreeBSD;
use OCA\ServerInfo\OperatingSystems\IOperatingSystem;
use OCA\ServerInfo\OperatingSystems\Linux;
use OCA\ServerInfo\Resources\CPU;
use OCA\ServerInfo\Resources\Memory;
use OCP\IConfig;

Expand Down Expand Up @@ -39,18 +40,13 @@ public function getOSName(): string {
}

#[\Override]
public function getMemory(): Memory {
return $this->backend->getMemory();
public function getCPU(): CPU {
return $this->backend->getCPU();
}

#[\Override]
public function getCpuName(): string {
return $this->backend->getCpuName();
}

#[\Override]
public function getCpuCount(): int {
return $this->backend->getCpuCount();
public function getMemory(): Memory {
return $this->backend->getMemory();
}

#[\Override]
Expand Down
48 changes: 48 additions & 0 deletions lib/Resources/CPU.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\ServerInfo\Resources;

/**
* @psalm-api
*/
class CPU implements \JsonSerializable {
public function __construct(
private string $name,
private int $threads,
) {
}

public function getName(): string {
return $this->name;
}

public function getThreads(): int {
return $this->threads;
}

/**
* Retrieves the system load averages.
*
* @return array|false Returns an array containing the system load averages for the last 1, 5, and 15 minutes.
*/
public function getAverageLoad(): array|false {
if (function_exists('sys_getloadavg')) {
return sys_getloadavg();
}
return false;
}

#[\Override]
public function jsonSerialize(): array {
return [
'name' => $this->name,
'threads' => $this->threads,
];
}
}
2 changes: 1 addition & 1 deletion lib/Settings/AdminSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function getForm(): TemplateResponse {
'hostname' => $this->os->getHostname(),
'osname' => $this->os->getOSName(),
'memory' => $this->os->getMemory(),
'cpu' => $this->os->getCpuName(),
'cpu' => $this->os->getCPU(),
'diskinfo' => $this->os->getDiskInfo(),
'networkinfo' => $this->os->getNetworkInfo(),
'networkinterfaces' => $this->os->getNetworkInterfaces(),
Expand Down
Loading
Loading