diff options
author | John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com> | 2019-04-11 17:20:37 +0300 |
---|---|---|
committer | John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com> | 2019-04-11 17:49:29 +0300 |
commit | c63e1093f79d2d50f9e7c0e2eb41c40f18625ae8 (patch) | |
tree | 7534ae871ad89b7661c49db034d4d0d0a373e6c3 /lib | |
parent | e2281220b36fc0219826c15f728af812ccb6e91f (diff) |
Cleanup
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
Diffstat (limited to 'lib')
-rwxr-xr-x | lib/Controller/ApiController.php | 74 | ||||
-rwxr-xr-x | lib/OperatingSystems/DefaultOs.php | 109 | ||||
-rw-r--r-- | lib/Os.php | 122 |
3 files changed, 122 insertions, 183 deletions
diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index 7d1b2d2..bbd2f31 100755 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -19,11 +19,10 @@ * */ - namespace OCA\ServerInfo\Controller; -use OCA\ServerInfo\Os; use OCA\ServerInfo\DatabaseStatistics; +use OCA\ServerInfo\Os; use OCA\ServerInfo\PhpStatistics; use OCA\ServerInfo\SessionStatistics; use OCA\ServerInfo\ShareStatistics; @@ -61,7 +60,7 @@ class ApiController extends OCSController { * * @param string $appName * @param IRequest $request - * @param Os $Os + * @param Os $os * @param SystemStatistics $systemStatistics * @param StorageStatistics $storageStatistics * @param PhpStatistics $phpStatistics @@ -71,23 +70,22 @@ class ApiController extends OCSController { */ public function __construct($appName, IRequest $request, - Os $Os, + Os $os, SystemStatistics $systemStatistics, StorageStatistics $storageStatistics, PhpStatistics $phpStatistics, DatabaseStatistics $databaseStatistics, ShareStatistics $shareStatistics, - SessionStatistics $sessionStatistics - ) { + SessionStatistics $sessionStatistics) { parent::__construct($appName, $request); - $this->Os = $Os; - $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; } /** @@ -97,23 +95,19 @@ 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() + ]); } @@ -121,21 +115,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)); + $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(); + $result = $this->os->getDiskData(); return new DataResponse($result); } - /** * get webserver * @@ -145,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 index 88d4fc3..d695c34 100755 --- a/lib/OperatingSystems/DefaultOs.php +++ b/lib/OperatingSystems/DefaultOs.php @@ -19,8 +19,6 @@ */ namespace OCA\Serverinfo\OperatingSystems; - - /** * Class Ubuntu * @@ -28,10 +26,7 @@ namespace OCA\Serverinfo\OperatingSystems; */ class DefaultOs { - /** - */ - public function __construct() { - } + public function __construct() {} /** * @return bool @@ -53,12 +48,12 @@ class DefaultOs { */ 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'; + $memory = explode(' ', $memory); + $memory = round($memory[0] / 1024); + if ($memory < 1024) { + $memory = $memory . ' MB'; } else { - $memory = round($memory/1024,1).' GB'; + $memory = round($memory / 1024, 1) . ' GB'; } return $memory; } @@ -69,8 +64,12 @@ class DefaultOs { 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; + if ($cores === 1) { + $cores = ' (' . $cores . ' core)'; + } else { + $cores = ' (' . $cores . ' cores)'; + } + return $cpu . ' ' . $cores; } /** @@ -89,13 +88,12 @@ class DefaultOs { return $uptime; } - /** - * @return string - */ + * @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 " "'); + $servers .= ' ' . shell_exec('cat /etc/systemd/timesyncd.conf |grep \'^NTP=\' | cut -f 2 -d " "'); return $servers; } @@ -103,13 +101,13 @@ class DefaultOs { * @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; + $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; } /** @@ -117,28 +115,37 @@ class DefaultOs { */ 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'] = ''; + $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['status'] = 'up'; $iface['speed'] = 'unknown'; - $iface['duplex'] = ''; } - $result[] = $iface; + $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; } @@ -146,20 +153,20 @@ class DefaultOs { * @return array */ public function getDiskInfo() { - $blacklist = array('','Type','tmpfs','devtmpfs'); - - $data = shell_exec('df -T'); + $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]) and !in_array($entry[1],$blacklist)) { - $items = array(); - $items['device'] = $entry[0]; - $items['fs'] = $entry[1]; - $items['used'] = $entry[3]; + 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]; + $items['percent'] = $entry[5]; + $items['mount'] = $entry[6]; $result[] = $items; } } @@ -21,13 +21,12 @@ namespace OCA\ServerInfo; use bantu\IniGetWrapper\IniGetWrapper; +use OCA\ServerInfo\OperatingSystems\DefaultOs; 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 { @@ -61,58 +60,24 @@ class Os { * @param IniGetWrapper $phpIni * @param IL10N $l */ - public function __construct(IClientService $clientService, IConfig $config, IDBConnection $connection, IniGetWrapper $phpIni, 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(): string { - $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'); - } + $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(); + $data = $this->backend->supported(); return $data; } @@ -120,7 +85,7 @@ class Os { * @return string */ public function getHostname() { - $data = $this -> backend -> getHostname(); + $data = $this->backend->getHostname(); return $data; } @@ -128,7 +93,7 @@ class Os { * @return string */ public function getOSName() { - $data = $this -> osname; + $data = $this->osname; return $data; } @@ -136,7 +101,7 @@ class Os { * @return string */ public function getMemory() { - $data = $this -> backend -> getMemory(); + $data = $this->backend->getMemory(); return $data; } @@ -144,7 +109,7 @@ class Os { * @return string */ public function getCPUName() { - $data = $this -> backend -> getCPUName(); + $data = $this->backend->getCPUName(); return $data; } @@ -152,7 +117,7 @@ class Os { * @return string */ public function getTime() { - $data = $this -> backend -> getTime(); + $data = $this->backend->getTime(); return $data; } @@ -160,7 +125,7 @@ class Os { * @return string */ public function getUptime() { - $data = $this -> backend -> getUptime(); + $data = $this->backend->getUptime(); return $data; } @@ -168,62 +133,35 @@ class Os { * @return string */ public function getTimeServers() { - $data = $this -> backend -> getTimeServers(); - return explode("\n",$data); + $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; -*/ + $data = $this->backend->getDiskInfo(); return $data; } - /** * @return string */ public function getDiskData() { - $disks = $this -> backend -> getDiskInfo(); + $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)); + 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)); + // $data = array('0'=>array(1,2),'1'=>array(4,5),'2'=>array(3,1)); return $data; } @@ -232,7 +170,7 @@ class Os { * @return string */ public function getNetworkInfo() { - $data = $this -> backend -> getNetworkInfo(); + $data = $this->backend->getNetworkInfo(); return $data; } @@ -240,7 +178,7 @@ class Os { * @return string */ public function getNetworkInterfaces() { - $data = $this -> backend -> getNetworkInterfaces(); + $data = $this->backend->getNetworkInterfaces(); return $data; } |