diff options
author | Frank Karlitschek <karlitschek@gmx.de> | 2019-03-05 16:25:05 +0300 |
---|---|---|
committer | Frank Karlitschek <karlitschek@gmx.de> | 2019-03-05 16:25:05 +0300 |
commit | d798eaf85e30801ba394dc9515b5d1760f666d8f (patch) | |
tree | d75dddb2109ccdb7aa85f86cfd2414816955a4ff /lib | |
parent | d61f6eda9753907769650219b2791a416accc9c0 (diff) |
adding more info to the page. This is only the first step
Diffstat (limited to 'lib')
-rwxr-xr-x[-rw-r--r--] | lib/Controller/ApiController.php | 26 | ||||
-rwxr-xr-x | lib/OperatingSystems/DefaultOs.php | 168 | ||||
-rwxr-xr-x | lib/Os.php | 247 | ||||
-rwxr-xr-x[-rw-r--r--] | lib/Settings/AdminSection.php | 4 | ||||
-rwxr-xr-x[-rw-r--r--] | lib/Settings/AdminSettings.php | 15 |
5 files changed, 456 insertions, 4 deletions
diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index 8757238..a900ff4 100644..100755 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -22,6 +22,7 @@ namespace OCA\ServerInfo\Controller; +use OCA\ServerInfo\Os; use OCA\ServerInfo\DatabaseStatistics; use OCA\ServerInfo\PhpStatistics; use OCA\ServerInfo\SessionStatistics; @@ -34,6 +35,9 @@ use OCP\IRequest; class ApiController extends OCSController { + /** @var Os */ + private $Os; + /** @var SystemStatistics */ private $systemStatistics; @@ -57,6 +61,7 @@ class ApiController extends OCSController { * * @param string $appName * @param IRequest $request + * @param Os $Os * @param SystemStatistics $systemStatistics * @param StorageStatistics $storageStatistics * @param PhpStatistics $phpStatistics @@ -66,6 +71,7 @@ class ApiController extends OCSController { */ public function __construct($appName, IRequest $request, + Os $Os, SystemStatistics $systemStatistics, StorageStatistics $storageStatistics, PhpStatistics $phpStatistics, @@ -75,6 +81,7 @@ class ApiController extends OCSController { ) { parent::__construct($appName, $request); + $this->Os = $Os; $this->systemStatistics = $systemStatistics; $this->storageStatistics = $storageStatistics; $this->phpStatistics = $phpStatistics; @@ -111,6 +118,25 @@ class ApiController extends OCSController { } /** + * @return DataResponse + */ + public function BasicData(): DataResponse { + $servertime = $this->Os->getTime(); + $uptime = $this->Os->getUptime(); + $timeservers = $this->Os->getTimeServers()[0]; + return new DataResponse(array('servertime'=>$servertime,'uptime'=>$uptime,'timeservers'=>$timeservers)); + } + + /** + * @return DataResponse + */ + public function DiskData(): DataResponse { + $result = $this->Os->getDiskData(); + return new DataResponse($result); + } + + + /** * get webserver * * @return string diff --git a/lib/OperatingSystems/DefaultOs.php b/lib/OperatingSystems/DefaultOs.php new file mode 100755 index 0000000..a83fc3e --- /dev/null +++ b/lib/OperatingSystems/DefaultOs.php @@ -0,0 +1,168 @@ +<?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() { + $uptime = shell_exec('cat /etc/ntp.conf |grep \'^pool\' | cut -f 2 -d " "'); + return $uptime; + } + + /** + * @return string + */ + public function getNetworkInfo() { + $result=array(); + $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=array(); + + foreach($interfaces as $interface) { + $iface = array(); + $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 = array('','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]) and !in_array($entry[1],$blacklist)) { + $items = array(); + $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 100755 index 0000000..cf07560 --- /dev/null +++ b/lib/Os.php @@ -0,0 +1,247 @@ +<?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 OCP\AppFramework\Http; +use OCP\AppFramework\Http\DataResponse; +use OCP\Http\Client\IClientService; +use OCP\IConfig; +use OCP\IDBConnection; +use OCP\IL10N; +use OCA\ServerInfo\OperatingSystems\DefaultOs; + +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; + + $detectedOs = $this -> detectOs(); + switch ($detectedOs) { + case 'ubuntu': + $this->backend = new DefaultOs(); + break; + default: + $this->backend = new DefaultOs(); + break; + } + } + + + /** + * @return string + */ + public function detectOs() { + $release = shell_exec('cat /etc/*-release'); + if(stripos($release,'ubuntu')) { + $os = shell_exec('lsb_release -r -s'); + $os = 'Ubuntu '.$os; + $this->osname = $os; + return('ubuntu'); + } elseif(stripos($release,'debian')) { + $this->osname = 'Debian'; + return('debian'); + } elseif(stripos($release,'suse')) { + $this->osname = 'Suse'; + return('suse'); + } elseif(stripos($release,'fedora')) { + $this->osname = 'Fedora'; + return('fedora'); + } elseif(stripos($release,'centos')) { + $this->osname = 'CentOS'; + return('centos'); + } else { + return('unknown'); + } + } + + + /** + * @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(); +/* + // debug data + $data = array(); + + $item['device'] = 'device1'; + $item['fs'] = 'ext'; + $item['used'] = 1000000; + $item['available'] = 2000000; + $item['percent'] = '30%'; + $item['mount'] = '/data'; + $data[] = $item; + + $item['device'] = 'device2'; + $item['fs'] = 'ext'; + $item['used'] = 2000000; + $item['available'] = 1000000; + $item['percent'] = '10%'; + $item['mount'] = '/data222'; + $data[] = $item; + + + $item['device'] = 'device3'; + $item['fs'] = 'ext4'; + $item['used'] = 10000000; + $item['available'] = 50000000; + $item['percent'] = '90%'; + $item['mount'] = '/data3'; + $data[] = $item; +*/ + return $data; + } + + + /** + * @return string + */ + public function getDiskData() { + $disks = $this -> backend -> getDiskInfo(); + $data = array(); + $i = 0; + foreach($disks as $disk){ + $data[$i] = array(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..100755 --- 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..100755 --- 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(), |