diff options
author | Roeland Jago Douma <rullzer@users.noreply.github.com> | 2019-04-15 15:08:22 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-15 15:08:22 +0300 |
commit | df89e0de3e050e11287ab371dcbf55ac2ea0df22 (patch) | |
tree | f3d3de17f5df8ee029861b5f5f179d745ffb3dad /lib | |
parent | 4675aaf9921f7476c414356943cbe7f81d54793e (diff) | |
parent | e499dd0b4d68912ea2a517bcf97d39c100d85d08 (diff) |
Merge pull request #146 from nextcloud/moreinfo
adding more info to the page. This is only the first step
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Controller/ApiController.php | 80 | ||||
-rw-r--r-- | lib/OperatingSystems/DefaultOs.php | 176 | ||||
-rw-r--r-- | lib/Os.php | 185 | ||||
-rw-r--r-- | lib/Settings/AdminSection.php | 4 | ||||
-rw-r--r-- | lib/Settings/AdminSettings.php | 15 |
5 files changed, 426 insertions, 34 deletions
diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index 8757238..bbd2f31 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -19,10 +19,10 @@ * */ - namespace OCA\ServerInfo\Controller; use OCA\ServerInfo\DatabaseStatistics; +use OCA\ServerInfo\Os; use OCA\ServerInfo\PhpStatistics; use OCA\ServerInfo\SessionStatistics; use OCA\ServerInfo\ShareStatistics; @@ -34,6 +34,9 @@ use OCP\IRequest; class ApiController extends OCSController { + /** @var Os */ + private $os; + /** @var SystemStatistics */ private $systemStatistics; @@ -57,6 +60,7 @@ class ApiController extends OCSController { * * @param string $appName * @param IRequest $request + * @param Os $os * @param SystemStatistics $systemStatistics * @param StorageStatistics $storageStatistics * @param PhpStatistics $phpStatistics @@ -66,21 +70,22 @@ class ApiController extends OCSController { */ public function __construct($appName, IRequest $request, + Os $os, SystemStatistics $systemStatistics, StorageStatistics $storageStatistics, PhpStatistics $phpStatistics, DatabaseStatistics $databaseStatistics, ShareStatistics $shareStatistics, - SessionStatistics $sessionStatistics - ) { + SessionStatistics $sessionStatistics) { parent::__construct($appName, $request); - $this->systemStatistics = $systemStatistics; - $this->storageStatistics = $storageStatistics; - $this->phpStatistics = $phpStatistics; + $this->os = $os; + $this->systemStatistics = $systemStatistics; + $this->storageStatistics = $storageStatistics; + $this->phpStatistics = $phpStatistics; $this->databaseStatistics = $databaseStatistics; - $this->shareStatistics = $shareStatistics; - $this->sessionStatistics = $sessionStatistics; + $this->shareStatistics = $shareStatistics; + $this->sessionStatistics = $sessionStatistics; } /** @@ -90,24 +95,43 @@ class ApiController extends OCSController { */ public function info() { - return new DataResponse( - [ - 'nextcloud' => - [ - 'system' => $this->systemStatistics->getSystemStatistics(), - 'storage' => $this->storageStatistics->getStorageStatistics(), - 'shares' => $this->shareStatistics->getShareStatistics() - ], - 'server' => - [ - 'webserver' => $this->getWebserver(), - 'php' => $this->phpStatistics->getPhpStatistics(), - 'database' => $this->databaseStatistics->getDatabaseStatistics() - ], - 'activeUsers' => $this->sessionStatistics->getSessionStatistics() - ] - ); + return new DataResponse([ + 'nextcloud' => [ + 'system' => $this->systemStatistics->getSystemStatistics(), + 'storage' => $this->storageStatistics->getStorageStatistics(), + 'shares' => $this->shareStatistics->getShareStatistics() + ], + 'server' => [ + 'webserver' => $this->getWebserver(), + 'php' => $this->phpStatistics->getPhpStatistics(), + 'database' => $this->databaseStatistics->getDatabaseStatistics() + ], + 'activeUsers' => $this->sessionStatistics->getSessionStatistics() + ]); + + } + + /** + * @return DataResponse + */ + public function BasicData(): DataResponse { + $servertime = $this->os->getTime(); + $uptime = $this->os->getUptime(); + $timeservers = $this->os->getTimeServers()[0]; + + return new DataResponse([ + 'servertime' => $servertime, + 'uptime' => $uptime, + 'timeservers' => $timeservers + ]); + } + /** + * @return DataResponse + */ + public function DiskData(): DataResponse { + $result = $this->os->getDiskData(); + return new DataResponse($result); } /** @@ -119,10 +143,6 @@ class ApiController extends OCSController { if (isset($_SERVER['SERVER_SOFTWARE'])) { return $_SERVER['SERVER_SOFTWARE']; } - - return "unknown"; + return 'unknown'; } - - - } diff --git a/lib/OperatingSystems/DefaultOs.php b/lib/OperatingSystems/DefaultOs.php new file mode 100644 index 0000000..d695c34 --- /dev/null +++ b/lib/OperatingSystems/DefaultOs.php @@ -0,0 +1,176 @@ +<?php +/** + * @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; +/** + * Class Ubuntu + * + * @package OCA\ServerInfo\OperatingSystems + */ +class DefaultOs { + + public function __construct() {} + + /** + * @return bool + */ + public function supported() { + return true; + } + + /** + * @return string + */ + public function getHostname() { + $hostname = shell_exec('hostname'); + return $hostname; + } + + /** + * @return string + */ + public function getMemory() { + $memory = shell_exec('cat /proc/meminfo | grep -i \'MemTotal\' | cut -f 2 -d ":" | awk \'{$1=$1}1\''); + $memory = explode(' ', $memory); + $memory = round($memory[0] / 1024); + if ($memory < 1024) { + $memory = $memory . ' MB'; + } else { + $memory = round($memory / 1024, 1) . ' GB'; + } + return $memory; + } + + /** + * @return string + */ + public function getCPUName() { + $cpu = shell_exec('cat /proc/cpuinfo | grep -i \'Model name\' | cut -f 2 -d ":" | awk \'{$1=$1}1\''); + $cores = shell_exec('cat /proc/cpuinfo | grep -i \'cpu cores\' | cut -f 2 -d ":" | awk \'{$1=$1}1\''); + if ($cores === 1) { + $cores = ' (' . $cores . ' core)'; + } else { + $cores = ' (' . $cores . ' cores)'; + } + return $cpu . ' ' . $cores; + } + + /** + * @return string + */ + public function getTime() { + $uptime = shell_exec('date'); + return $uptime; + } + + /** + * @return string + */ + public function getUptime() { + $uptime = shell_exec('uptime -p'); + return $uptime; + } + + /** + * @return string + */ + public function getTimeServers() { + $servers = shell_exec('cat /etc/ntp.conf |grep \'^pool\' | cut -f 2 -d " "'); + $servers .= ' ' . shell_exec('cat /etc/systemd/timesyncd.conf |grep \'^NTP=\' | cut -f 2 -d " "'); + return $servers; + } + + /** + * @return string + */ + public function getNetworkInfo() { + $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; + } + + /** + * @return string + */ + public function getNetworkInterfaces() { + $interfaces = glob('/sys/class/net/*'); + $result = []; + + 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'] = shell_exec('cat /sys/class/net/' . $iface['interface'] . '/speed'); + if ($iface['speed'] !== '') { + $iface['speed'] = $iface['speed'] . 'Mbps'; + } else { + $iface['speed'] = 'unknown'; + } + + $duplex = shell_exec('cat /sys/class/net/' . $iface['interface'] . '/duplex'); + if ($duplex !== '') { + $iface['duplex'] = 'Duplex: ' . $duplex; + } else { + $iface['duplex'] = ''; + } + } else { + $iface['status'] = 'up'; + $iface['speed'] = 'unknown'; + $iface['duplex'] = ''; + } + $result[] = $iface; + } + + return $result; + } + + /** + * @return array + */ + public function getDiskInfo() { + $blacklist = ['', 'Type', 'tmpfs', 'devtmpfs']; + $data = shell_exec('df -T'); + $lines = preg_split('/[\r\n]+/', $data); + + foreach ($lines as $line) { + $entry = preg_split('/\s+/', trim($line)); + if (isset($entry[1]) && !in_array($entry[1], $blacklist)) { + $items = []; + $items['device'] = $entry[0]; + $items['fs'] = $entry[1]; + $items['used'] = $entry[3]; + $items['available'] = $entry[4]; + $items['percent'] = $entry[5]; + $items['mount'] = $entry[6]; + $result[] = $items; + } + } + return $result; + } + +} diff --git a/lib/Os.php b/lib/Os.php new file mode 100644 index 0000000..52a6c64 --- /dev/null +++ b/lib/Os.php @@ -0,0 +1,185 @@ +<?php +/** + * @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; + +use bantu\IniGetWrapper\IniGetWrapper; +use OCA\ServerInfo\OperatingSystems\DefaultOs; +use OCP\AppFramework\Http; +use OCP\Http\Client\IClientService; +use OCP\IConfig; +use OCP\IDBConnection; +use OCP\IL10N; + +class Os { + + /** @var IClientService */ + protected $clientService; + + /** @var IConfig */ + protected $config; + + /** @var IDBConnection */ + protected $connection; + + /** @var IniGetWrapper */ + protected $phpIni; + + /** @var \OCP\IL10N */ + protected $l; + + /** @var */ + protected $backend; + + /** @var */ + protected $osname; + + /** + * Os constructor. + * + * @param IClientService $clientService + * @param IConfig $config + * @param IDBConnection $connection + * @param IniGetWrapper $phpIni + * @param IL10N $l + */ + public function __construct(IClientService $clientService, + IConfig $config, + IDBConnection $connection, + IniGetWrapper $phpIni, + IL10N $l) { + $this->clientService = $clientService; + $this->config = $config; + $this->connection = $connection; + $this->phpIni = $phpIni; + $this->l = $l; + $this->backend = new DefaultOs(); + } + + /** + * @return bool + */ + public function supported() { + $data = $this->backend->supported(); + return $data; + } + + /** + * @return string + */ + public function getHostname() { + $data = $this->backend->getHostname(); + return $data; + } + + /** + * @return string + */ + public function getOSName() { + $data = $this->osname; + return $data; + } + + /** + * @return string + */ + public function getMemory() { + $data = $this->backend->getMemory(); + return $data; + } + + /** + * @return string + */ + public function getCPUName() { + $data = $this->backend->getCPUName(); + return $data; + } + + /** + * @return string + */ + public function getTime() { + $data = $this->backend->getTime(); + return $data; + } + + /** + * @return string + */ + public function getUptime() { + $data = $this->backend->getUptime(); + return $data; + } + + /** + * @return string + */ + public function getTimeServers() { + $data = $this->backend->getTimeServers(); + return explode("\n", $data); + } + + /** + * @return string + */ + public function getDiskInfo() { + $data = $this->backend->getDiskInfo(); + return $data; + } + + /** + * @return string + */ + public function getDiskData() { + $disks = $this->backend->getDiskInfo(); + $data = array(); + $i = 0; + foreach ($disks as $disk) { + $data[$i] = [ + round(($disk['used']) / 1024 / 1024, 1), + round($disk['available'] / 1024 / 1024, 1) + ]; + $i++; + } + +// debug data + // $data = array('0'=>array(1,2),'1'=>array(4,5),'2'=>array(3,1)); + + return $data; + } + + /** + * @return string + */ + public function getNetworkInfo() { + $data = $this->backend->getNetworkInfo(); + return $data; + } + + /** + * @return string + */ + public function getNetworkInterfaces() { + $data = $this->backend->getNetworkInterfaces(); + return $data; + } + +} diff --git a/lib/Settings/AdminSection.php b/lib/Settings/AdminSection.php index eb74dfa..1b0a552 100644 --- a/lib/Settings/AdminSection.php +++ b/lib/Settings/AdminSection.php @@ -55,7 +55,7 @@ class AdminSection implements IIconSection { * @return string */ public function getName() { - return $this->l->t('Monitoring'); + return $this->l->t('System'); } /** @@ -66,7 +66,7 @@ class AdminSection implements IIconSection { * keep the server setting at the top, right after "overview" and "basic settings" */ public function getPriority() { - return 1; + return 90; } /** diff --git a/lib/Settings/AdminSettings.php b/lib/Settings/AdminSettings.php index 8a2eaa4..03bfeb2 100644 --- a/lib/Settings/AdminSettings.php +++ b/lib/Settings/AdminSettings.php @@ -22,7 +22,7 @@ namespace OCA\ServerInfo\Settings; - +use OCA\ServerInfo\Os; use OCP\AppFramework\Http\TemplateResponse; use OCP\IL10N; use OCP\IURLGenerator; @@ -36,6 +36,8 @@ use OCA\ServerInfo\SystemStatistics; class AdminSettings implements ISettings { + /** @var Os */ + private $os; /** @var IL10N */ private $l; @@ -73,7 +75,8 @@ class AdminSettings implements ISettings { * @param SessionStatistics $sessionStatistics * @param SystemStatistics $systemStatistics */ - public function __construct(IL10N $l, + public function __construct(Os $os, + IL10N $l, IURLGenerator $urlGenerator, StorageStatistics $storageStatistics, PhpStatistics $phpStatistics, @@ -82,6 +85,7 @@ class AdminSettings implements ISettings { SessionStatistics $sessionStatistics, SystemStatistics $systemStatistics ) { + $this->os = $os; $this->l = $l; $this->urlGenerator = $urlGenerator; $this->storageStatistics = $storageStatistics; @@ -98,6 +102,13 @@ class AdminSettings implements ISettings { public function getForm() { $monitoringEndPoint = $this->urlGenerator->getAbsoluteURL('ocs/v2.php/apps/serverinfo/api/v1/info'); $params = [ + 'hostname' => $this-> os -> getHostname(), + 'osname' => $this-> os -> getOSName(), + 'memory' => $this-> os -> getMemory(), + 'cpu' => $this-> os -> getCPUName(), + 'diskinfo' => $this-> os -> getDiskInfo(), + 'networkinfo' => $this-> os -> getNetworkInfo(), + 'networkinterfaces' => $this-> os -> getNetworkInterfaces(), 'ocs' => $monitoringEndPoint, 'storage' => $this->storageStatistics->getStorageStatistics(), 'shares' => $this->shareStatistics->getShareStatistics(), |