migrate therinaldos.com data
Build & Deploy to DigitalOcean Space / build (push) Failing after 2m38s

This commit is contained in:
2024-05-05 15:50:45 -04:00
commit ef1ff240d4
23182 changed files with 3801898 additions and 0 deletions
@@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
/**
* @author Frank Karlitschek <frank@nextcloud.com>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\ServerInfo\OperatingSystems;
use OCA\ServerInfo\Resources\Memory;
class Dummy implements IOperatingSystem {
public function supported(): bool {
return false;
}
public function getMemory(): Memory {
return new Memory();
}
public function getCpuName(): string {
return 'Unknown Processor';
}
public function getTime(): string {
return '';
}
public function getUptime(): int {
return -1;
}
public function getNetworkInfo(): array {
return [
'hostname' => \gethostname(),
'dns' => '',
'gateway' => '',
];
}
public function getNetworkInterfaces(): array {
return [];
}
public function getDiskInfo(): array {
return [];
}
public function getThermalZones(): array {
return [];
}
}
@@ -0,0 +1,250 @@
<?php
declare(strict_types=1);
/**
* @author Matthew Wener <matthew@wener.org>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <https://www.gnu.org/licenses/>
*
*/
namespace OCA\ServerInfo\OperatingSystems;
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;
}
public function getMemory(): Memory {
$data = new Memory();
try {
$swapinfo = $this->executeCommand('/usr/sbin/swapinfo -k');
} catch (RuntimeException $e) {
$swapinfo = '';
}
$matches = [];
$pattern = '/(?>\/dev\/\S+)\s+(?>\d+)\s+(?<Used>\d+)\s+(?<Avail>\d+)\s+(?<Capacity>\d+)/';
$result = preg_match_all($pattern, $swapinfo, $matches);
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);
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) {
$meminfo = '';
}
$lines = array_map('intval', explode("\n", $meminfo));
if (count($lines) > 4) {
$data->setMemTotal((int)($lines[0] / 1024 / 1024));
$data->setMemAvailable((int)(($lines[1] * ($lines[2] + $lines[3] + $lines[4])) / 1024 / 1024));
}
unset($lines);
return $data;
}
public function getCpuName(): string {
$data = 'Unknown Processor';
try {
$model = $this->executeCommand('/sbin/sysctl -n hw.model');
$cores = $this->executeCommand('/sbin/sysctl -n kern.smp.cpus');
if ((int)$cores === 1) {
$data = $model . ' (1 core)';
} else {
$data = $model . ' (' . $cores . ' cores)';
}
} catch (RuntimeException $e) {
return $data;
}
return $data;
}
public function getTime(): string {
try {
return $this->executeCommand('date');
} catch (RuntimeException $e) {
return '';
}
}
public function getUptime(): int {
$uptime = -1;
try {
$shell_boot = $this->executeCommand('/sbin/sysctl -n kern.boottime');
preg_match("/[\d]+/", $shell_boot, $boottime);
$time = $this->executeCommand('date +%s');
$uptime = (int)$time - (int)$boottime[0];
} catch (RuntimeException $e) {
return $uptime;
}
return $uptime;
}
public function getNetworkInfo(): array {
$result = [];
$result['hostname'] = \gethostname();
try {
$dns = $this->executeCommand('cat /etc/resolv.conf 2>/dev/null');
preg_match_all("/(?<=^nameserver ).\S*/m", $dns, $matches);
$alldns = implode(' ', $matches[0]);
$result['dns'] = $alldns;
$netstat = $this->executeCommand('netstat -rn');
preg_match_all("/(?<=^default)\s*[0-9a-fA-f\.:]+/m", $netstat, $gw);
if (count($gw[0]) > 0) {
$result['gateway'] = implode(", ", array_map("trim", $gw[0]));
} else {
$result['gateway'] = '';
}
} catch (RuntimeException $e) {
return $result;
}
return $result;
}
public function getNetworkInterfaces(): array {
$data = [];
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']);
}
if ($unicast['family'] === self::AF_INET6) {
$netInterface->addIPv6($unicast['address']);
}
}
if ($netInterface->isLoopback()) {
continue;
}
try {
$details = $this->executeCommand('/sbin/ifconfig ' . $interfaceName);
} catch (RuntimeException $e) {
continue;
}
preg_match("/(?<=ether ).*/m", $details, $mac);
if (isset($mac[0])) {
$netInterface->setMAC($mac[0]);
}
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 {
$netInterface->setSpeed($speed[0] . ' Mbps');
}
}
preg_match("/(?<=\<).*(?=-)/m", $details, $duplex);
if (isset($duplex[0])) {
$netInterface->setDuplex($duplex[0]);
}
unset($mac, $speed, $duplex);
}
return $data;
}
public function getDiskInfo(): array {
$data = [];
try {
$disks = $this->executeCommand('df -TPk');
} catch (RuntimeException $e) {
return $data;
}
$matches = [];
$pattern = '/^(?<Filesystem>[\S]+)\s*(?<Type>[\S]+)\s*(?<Blocks>\d+)\s*(?<Used>\d+)\s*(?<Available>\d+)\s*(?<Capacity>\d+%)\s*(?<Mounted>[\w\/-]+)$/m';
$result = preg_match_all($pattern, $disks, $matches);
if ($result === 0 || $result === false) {
return $data;
}
$excluded = ['devfs', 'fdescfs', 'tmpfs', 'devtmpfs', 'procfs', 'linprocfs', 'linsysfs'];
foreach ($matches['Filesystem'] as $i => $filesystem) {
if (in_array($matches['Type'][$i], $excluded, false)) {
continue;
}
$disk = new Disk();
$disk->setDevice($filesystem);
$disk->setFs($matches['Type'][$i]);
$disk->setUsed((int)((int)$matches['Used'][$i] / 1024));
$disk->setAvailable((int)((int)$matches['Available'][$i] / 1024));
$disk->setPercent($matches['Capacity'][$i]);
$disk->setMount($matches['Mounted'][$i]);
$data[] = $disk;
}
return $data;
}
public function getThermalZones(): array {
return [];
}
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 . '"');
}
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;
}
}
@@ -0,0 +1,93 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2020 Daniel Kesselberg <mail@danielkesselberg.de>
*
* @author Daniel Kesselberg <mail@danielkesselberg.de>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\ServerInfo\OperatingSystems;
use OCA\ServerInfo\Resources\Disk;
use OCA\ServerInfo\Resources\Memory;
use OCA\ServerInfo\Resources\NetInterface;
use OCA\ServerInfo\Resources\ThermalZone;
interface IOperatingSystem {
public function supported(): bool;
/**
* Get name of the processor.
*
* @return string
*/
public function getCpuName(): string;
/**
* 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.
*
* [
* 'dns' => string,
* 'gateway' => string,
* 'hostname' => string,
* ]
*/
public function getNetworkInfo(): array;
/**
* Get info about available network interfaces.
*
* @return NetInterface[]
*/
public function getNetworkInterfaces(): array;
/**
* Get system time and timezone.
* Empty string in case of errors
*/
public function getTime(): string;
/**
* Get the total number of seconds the system has been up or -1 on failure.
*/
public function getUptime(): int;
/**
* Get info about available thermal zones.
*
* @return ThermalZone[]
*/
public function getThermalZones(): array;
}
@@ -0,0 +1,284 @@
<?php
declare(strict_types=1);
/**
* @author Frank Karlitschek <frank@nextcloud.com>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\ServerInfo\OperatingSystems;
use OCA\ServerInfo\Resources\Disk;
use OCA\ServerInfo\Resources\Memory;
use OCA\ServerInfo\Resources\NetInterface;
use OCA\ServerInfo\Resources\ThermalZone;
use RuntimeException;
class Linux implements IOperatingSystem {
private const AF_INET = 2;
private const AF_INET6 = 10;
public function supported(): bool {
return true;
}
public function getMemory(): Memory {
$data = new Memory();
try {
$meminfo = $this->readContent('/proc/meminfo');
} catch (RuntimeException $e) {
return $data;
}
$matches = [];
$pattern = '/(?<Key>(?:MemTotal|MemFree|MemAvailable|SwapTotal|SwapFree)+):\s+(?<Value>\d+)\s+(?<Unit>\w{2})/';
$result = preg_match_all($pattern, $meminfo, $matches);
if ($result === 0 || $result === false) {
return $data;
}
foreach ($matches['Key'] as $i => $key) {
// Value is always in KB: https://github.com/torvalds/linux/blob/c70672d8d316ebd46ea447effadfe57ab7a30a50/fs/proc/meminfo.c#L58-L60
$value = (int)((int)$matches['Value'][$i] / 1024);
switch ($key) {
case 'MemTotal':
$data->setMemTotal($value);
break;
case 'MemFree':
$data->setMemFree($value);
break;
case 'MemAvailable':
$data->setMemAvailable($value);
break;
case 'SwapTotal':
$data->setSwapTotal($value);
break;
case 'SwapFree':
$data->setSwapFree($value);
break;
}
}
return $data;
}
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];
$pattern = '/processor\s+:\s(.+)/';
preg_match_all($pattern, $cpuinfo, $matches);
$cores = count($matches[1]);
if ($cores === 1) {
$data = $model . ' (1 core)';
} else {
$data = $model . ' (' . $cores . ' cores)';
}
return $data;
}
public function getTime(): string {
return (string)shell_exec('date');
}
public function getUptime(): int {
$data = -1;
try {
$uptime = $this->readContent('/proc/uptime');
} catch (RuntimeException $e) {
return $data;
}
[$uptimeInSeconds,] = array_map('intval', explode(' ', $uptime));
return $uptimeInSeconds;
}
public function getNetworkInfo(): array {
$result = [];
$result['hostname'] = \gethostname();
$dns = shell_exec('cat /etc/resolv.conf |grep -i \'^nameserver\'|head -n1|cut -d \' \' -f2');
$result['dns'] = $dns;
$gw = shell_exec('ip route | awk \'/default/ { print $3 }\'');
$result['gateway'] = $gw;
return $result;
}
public function getNetworkInterfaces(): array {
$data = [];
foreach ($this->getNetInterfaces() as $interfaceName => $interface) {
$netInterface = new NetInterface($interfaceName, $interface['up']);
$data[] = $netInterface;
foreach ($interface['unicast'] as $unicast) {
if (isset($unicast['family'])) {
if ($unicast['family'] === self::AF_INET) {
$netInterface->addIPv4($unicast['address']);
}
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 {
$netInterface->setSpeed($speed . ' Mbps');
}
$netInterface->setDuplex($this->readContent($interfacePath . '/duplex'));
} catch (RuntimeException $e) {
// unable to read interface data
}
}
return $data;
}
public function getDiskInfo(): array {
$data = [];
try {
$disks = $this->executeCommand('df -TPk');
} catch (RuntimeException $e) {
return $data;
}
$matches = [];
$pattern = '/^(?<Filesystem>[\S]+)\s*(?<Type>[\S]+)\s*(?<Blocks>\d+)\s*(?<Used>\d+)\s*(?<Available>\d+)\s*(?<Capacity>\d+%)\s*(?<Mounted>[\w\/-]+)$/m';
$result = preg_match_all($pattern, $disks, $matches);
if ($result === 0 || $result === false) {
return $data;
}
foreach ($matches['Filesystem'] as $i => $filesystem) {
if (in_array($matches['Type'][$i], ['tmpfs', 'devtmpfs', 'squashfs', 'overlay'], false)) {
continue;
} elseif (in_array($matches['Mounted'][$i], ['/etc/hostname', '/etc/hosts'], false)) {
continue;
}
$disk = new Disk();
$disk->setDevice($filesystem);
$disk->setFs($matches['Type'][$i]);
$disk->setUsed((int)((int)$matches['Used'][$i] / 1024));
$disk->setAvailable((int)((int)$matches['Available'][$i] / 1024));
$disk->setPercent($matches['Capacity'][$i]);
$disk->setMount($matches['Mounted'][$i]);
$data[] = $disk;
}
return $data;
}
public function getThermalZones(): array {
$data = [];
$zones = glob('/sys/class/thermal/thermal_zone*');
if ($zones === false) {
return $data;
}
foreach ($zones as $zone) {
try {
$type = $this->readContent($zone . '/type');
$temp = (float)((int)($this->readContent($zone . '/temp')) / 1000);
$data[] = new ThermalZone(md5($zone), $type, $temp);
} catch (RuntimeException) {
// unable to read thermal zone
}
}
return $data;
}
/**
* @throws RuntimeException
*/
protected function readContent(string $filename): string {
$data = @file_get_contents($filename);
if ($data === false || $data === '') {
throw new RuntimeException('Unable to read: "' . $filename . '"');
}
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 . '"');
}
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;
}
}