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
99 changes: 61 additions & 38 deletions lib/OperatingSystems/DefaultOs.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@

use OCA\ServerInfo\Resources\Disk;
use OCA\ServerInfo\Resources\Memory;
use OCA\ServerInfo\Resources\NetInterface;
use RuntimeException;

class DefaultOs implements IOperatingSystem {
private const AF_INET = 2;
private const AF_INET6 = 10;

public function supported(): bool {
return true;
}
Expand All @@ -36,7 +41,7 @@ public function getMemory(): Memory {

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

Expand Down Expand Up @@ -79,7 +84,7 @@ public function getCpuName(): string {

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

Expand All @@ -100,7 +105,7 @@ public function getCpuName(): string {

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

$result = preg_match_all($pattern, $cpuinfo, $matches);
preg_match_all($pattern, $cpuinfo, $matches);
$cores = count($matches[1]);

if ($cores === 1) {
Expand All @@ -121,7 +126,7 @@ public function getUptime(): int {

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

Expand All @@ -141,50 +146,52 @@ public function getNetworkInfo(): array {
}

public function getNetworkInterfaces(): array {
$interfaces = glob('/sys/class/net/*') ?: [];
$result = [];
$data = [];

foreach ($interfaces as $interface) {
$iface = [];
$iface['interface'] = basename($interface);
$iface['mac'] = shell_exec('ip addr show dev ' . $iface['interface'] . ' | grep "link/ether " | cut -d \' \' -f 6 | cut -f 1 -d \'/\'');
$iface['ipv4'] = shell_exec('ip addr show dev ' . $iface['interface'] . ' | grep "inet " | cut -d \' \' -f 6 | cut -f 1 -d \'/\'');
$iface['ipv6'] = shell_exec('ip -o -6 addr show ' . $iface['interface'] . ' | sed -e \'s/^.*inet6 \([^ ]\+\).*/\1/\'');
if ($iface['interface'] !== 'lo') {
$iface['status'] = shell_exec('cat /sys/class/net/' . $iface['interface'] . '/operstate');
$iface['speed'] = (int)shell_exec('cat /sys/class/net/' . $iface['interface'] . '/speed');
if (isset($iface['speed']) && $iface['speed'] > 0) {
if ($iface['speed'] >= 1000) {
$iface['speed'] = $iface['speed'] / 1000 . ' Gbps';
} else {
$iface['speed'] = $iface['speed'] . ' Mbps';
}
} else {
$iface['speed'] = 'unknown';
foreach ($this->getNetInterfaces() as $interfaceName => $interface) {
$netInterface = new NetInterface($interfaceName, $interface['up']);
$data[] = $netInterface;

foreach ($interface['unicast'] as $unicast) {
if ($unicast['family'] === self::AF_INET) {
$netInterface->addIPv4($unicast['address']);
}
$duplex = shell_exec('cat /sys/class/net/' . $iface['interface'] . '/duplex');
if (isset($duplex) && $duplex !== '') {
$iface['duplex'] = 'Duplex: ' . $duplex;
if ($unicast['family'] === self::AF_INET6) {
$netInterface->addIPv6($unicast['address']);
}
}

if ($netInterface->isLoopback()) {
continue;
}

$interfacePath = '/sys/class/net/' . $interfaceName;

try {
$netInterface->setMAC($this->readContent($interfacePath . '/address'));

$speed = (int)$this->readContent($interfacePath . '/speed');
if ($speed >= 1000) {
$netInterface->setSpeed($speed / 1000 . ' Gbps');
} else {
$iface['duplex'] = '';
$netInterface->setSpeed($speed . ' Mbps');
}
} else {
$iface['status'] = 'up';
$iface['speed'] = 'unknown';
$iface['duplex'] = '';

$netInterface->setDuplex($this->readContent($interfacePath . '/duplex'));
} catch (RuntimeException $e) {
// unable to read interface data
}
$result[] = $iface;
}

return $result;
return $data;
}

public function getDiskInfo(): array {
$data = [];

try {
$disks = $this->executeCommand('df -TPk');
} catch (\RuntimeException $e) {
} catch (RuntimeException $e) {
return $data;
}

Expand Down Expand Up @@ -227,7 +234,7 @@ public function getThermalZones(): array {
$tzone['hash'] = md5($thermalZone);
$tzone['type'] = $this->readContent($thermalZone . '/type');
$tzone['temp'] = (float)((int)($this->readContent($thermalZone . '/temp')) / 1000);
} catch (\RuntimeException $e) {
} catch (RuntimeException $e) {
continue;
}
$result[] = $tzone;
Expand All @@ -236,19 +243,35 @@ public function getThermalZones(): array {
return $result;
}

/**
* @throws RuntimeException
*/
protected function readContent(string $filename): string {
$data = @file_get_contents($filename);
if ($data === false || $data === '') {
throw new \RuntimeException('Unable to read: "' . $filename . '"');
throw new RuntimeException('Unable to read: "' . $filename . '"');
}
return $data;
return trim($data);
}

protected function executeCommand(string $command): string {
$output = @shell_exec(escapeshellcmd($command));
if ($output === false || $output === null || $output === '') {
throw new \RuntimeException('No output for command: "' . $command . '"');
throw new RuntimeException('No output for command: "' . $command . '"');
}
return $output;
}

/**
* Wrapper for net_get_interfaces
*
* @throws RuntimeException
*/
protected function getNetInterfaces(): array {
$data = net_get_interfaces();
if ($data === false) {
throw new RuntimeException('Unable to get network interfaces');
}
return $data;
}
}
122 changes: 59 additions & 63 deletions lib/OperatingSystems/FreeBSD.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@

use OCA\ServerInfo\Resources\Disk;
use OCA\ServerInfo\Resources\Memory;
use OCA\ServerInfo\Resources\NetInterface;
use RuntimeException;

class FreeBSD implements IOperatingSystem {
private const AF_INET = 2;
private const AF_INET6 = 28;

public function supported(): bool {
return false;
}
Expand All @@ -36,7 +41,7 @@ public function getMemory(): Memory {

try {
$swapinfo = $this->executeCommand('/usr/sbin/swapinfo -k');
} catch (\RuntimeException $e) {
} catch (RuntimeException $e) {
$swapinfo = '';
}

Expand All @@ -53,7 +58,7 @@ public function getMemory(): Memory {

try {
$meminfo = $this->executeCommand('/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');
} catch (\RuntimeException $e) {
} catch (RuntimeException $e) {
$meminfo = '';
}

Expand All @@ -80,7 +85,7 @@ public function getCpuName(): string {
} else {
$data = $model . ' (' . $cores . ' cores)';
}
} catch (\RuntimeException $e) {
} catch (RuntimeException $e) {
return $data;
}
return $data;
Expand All @@ -89,7 +94,7 @@ public function getCpuName(): string {
public function getTime(): string {
try {
return $this->executeCommand('date');
} catch (\RuntimeException $e) {
} catch (RuntimeException $e) {
return '';
}
}
Expand All @@ -102,7 +107,7 @@ public function getUptime(): int {
preg_match("/[\d]+/", $shell_boot, $boottime);
$time = $this->executeCommand('date +%s');
$uptime = (int)$time - (int)$boottime[0];
} catch (\RuntimeException $e) {
} catch (RuntimeException $e) {
return $uptime;
}
return $uptime;
Expand All @@ -122,91 +127,69 @@ public function getNetworkInfo(): array {
if (count($gw[0]) > 0) {
$result['gateway'] = implode(", ", array_map("trim", $gw[0]));
}
} catch (\RuntimeException $e) {
} catch (RuntimeException $e) {
return $result;
}
return $result;
}

public function getNetworkInterfaces(): array {
$result = [];
$data = [];

try {
$ifconfig = $this->executeCommand('/sbin/ifconfig -a');
} catch (\RuntimeException $e) {
return $result;
}
foreach ($this->getNetInterfaces() as $interfaceName => $interface) {
$netInterface = new NetInterface($interfaceName, $interface['up']);
$data[] = $netInterface;

preg_match_all("/^(?<=(?!\t)).*(?=:)/m", $ifconfig, $interfaces);
foreach ($interface['unicast'] as $unicast) {
if ($unicast['family'] === self::AF_INET) {
$netInterface->addIPv4($unicast['address']);
}
if ($unicast['family'] === self::AF_INET6) {
$netInterface->addIPv6($unicast['address']);
}
}

foreach ($interfaces[0] as $interface) {
$iface = [];
$iface['interface'] = $interface;
if ($netInterface->isLoopback()) {
continue;
}

try {
$intface = $this->executeCommand('/sbin/ifconfig ' . $iface['interface']);
} catch (\RuntimeException $e) {
$details = $this->executeCommand('/sbin/ifconfig ' . $interfaceName);
} catch (RuntimeException $e) {
continue;
}

preg_match_all("/(?<=inet ).\S*/m", $intface, $ipv4);
preg_match_all("/(?<=inet6 )((.*(?=%))|(.\S*))/m", $intface, $ipv6);
$iface['ipv4'] = implode(' ', $ipv4[0]);
$iface['ipv6'] = implode(' ', $ipv6[0]);

if ($iface['interface'] !== 'lo0') {
preg_match_all("/(?<=ether ).*/m", $intface, $mac);
preg_match("/(?<=status: ).*/m", $intface, $status);
preg_match("/\b[0-9].*?(?=base)/m", $intface, $speed);
preg_match("/(?<=\<).*(?=-)/m", $intface, $duplex);

if (isset($mac[0])) {
$iface['mac'] = implode(' ', $mac[0]);
}

if (isset($speed[0])) {
$iface['speed'] = $speed[0];
}

if (isset($status[0])) {
$iface['status'] = $status[0];
} else {
$iface['status'] = 'active';
}
preg_match("/(?<=ether ).*/m", $details, $mac);
if (isset($mac[0])) {
$netInterface->setMAC($mac[0]);
}

if (isset($iface['speed'])) {
if (strpos($iface['speed'], 'G')) {
$iface['speed'] = rtrim($iface['speed'], 'G');
$iface['speed'] = $iface['speed'] . ' Gbps';
} else {
$iface['speed'] = $iface['speed'] . ' Mbps';
}
preg_match("/\b[0-9].*?(?=base)/m", $details, $speed);
if (isset($speed[0])) {
if (substr($speed[0], -1) === 'G') {
$netInterface->setSpeed(rtrim($speed[0], 'G') . ' Gbps');
} else {
$iface['speed'] = 'unknown';
$netInterface->setSpeed($speed[0] . ' Mbps');
}
}

if (isset($duplex[0])) {
$iface['duplex'] = 'Duplex: ' . $duplex[0];
} else {
$iface['duplex'] = '';
}
} else {
$iface['status'] = 'active';
$iface['speed'] = 'unknown';
$iface['duplex'] = '';
preg_match("/(?<=\<).*(?=-)/m", $details, $duplex);
if (isset($duplex[0])) {
$netInterface->setDuplex($duplex[0]);
}
$result[] = $iface;

unset($mac, $speed, $duplex);
}

return $result;
return $data;
}

public function getDiskInfo(): array {
$data = [];

try {
$disks = $this->executeCommand('df -TPk');
} catch (\RuntimeException $e) {
} catch (RuntimeException $e) {
return $data;
}

Expand Down Expand Up @@ -245,8 +228,21 @@ public function getThermalZones(): array {
protected function executeCommand(string $command): string {
$output = @shell_exec(escapeshellcmd($command));
if ($output === null || $output === '' || $output === false) {
throw new \RuntimeException('No output for command: "' . $command . '"');
throw new RuntimeException('No output for command: "' . $command . '"');
}
return $output;
}

/**
* Wrapper for net_get_interfaces
*
* @throws RuntimeException
*/
protected function getNetInterfaces(): array {
$data = net_get_interfaces();
if ($data === false) {
throw new RuntimeException('Unable to get network interfaces');
}
return $data;
}
}
Loading