diff options
-rw-r--r-- | lib/SystemStatistics.php | 46 |
1 files changed, 34 insertions, 12 deletions
diff --git a/lib/SystemStatistics.php b/lib/SystemStatistics.php index cc2bb60..cc8cbaf 100644 --- a/lib/SystemStatistics.php +++ b/lib/SystemStatistics.php @@ -26,6 +26,7 @@ use OC\Files\View; use OC\Installer; use OCP\IConfig; use OCP\App\IAppManager; +use bantu\IniGetWrapper\IniGetWrapper; class SystemStatistics { @@ -37,6 +38,8 @@ class SystemStatistics { private $appManager; /** @var Installer */ private $installer; + /** @var IniGetWrapper */ + protected $phpIni; /** * SystemStatistics constructor. @@ -44,13 +47,15 @@ class SystemStatistics { * @param IConfig $config * @param IAppManager $appManager * @param Installer $installer + * @param IniGetWrapper $phpIni * @throws \Exception */ - public function __construct(IConfig $config, IAppManager $appManager, Installer $installer) { + public function __construct(IConfig $config, IAppManager $appManager, Installer $installer, IniGetWrapper $phpIni) { $this->config = $config; $this->view = new View(); $this->appManager = $appManager; $this->installer = $installer; + $this->phpIni = $phpIni; } /** @@ -82,7 +87,7 @@ class SystemStatistics { } /** - * get available and free memory including both RAM and Swap + * Get available and free memory including both RAM and Swap * * @return array with the two values 'mem_free' and 'mem_total' */ @@ -93,26 +98,26 @@ class SystemStatistics { $memoryUsage = file_get_contents('/proc/meminfo'); } //If FreeBSD is used and exec()-usage is allowed - if (PHP_OS === 'FreeBSD' && \OC_Helper::is_function_enabled('exec')) { + if (PHP_OS === 'FreeBSD' && $this->is_function_enabled('exec')) { //Read Swap usage: - exec("/usr/sbin/swapinfo",$return,$status); - if ($status===0 && count($return) > 1) { + exec("/usr/sbin/swapinfo", $return, $status); + if ($status === 0 && count($return) > 1) { $line = preg_split("/[\s]+/", $return[1]); if(count($line) > 3) { - $swapTotal = (int) $line[3]; - $swapFree = $swapTotal- (int) $line[2]; + $swapTotal = (int)$line[3]; + $swapFree = $swapTotal - (int)$line[2]; } } unset($status); unset($return); //Read Memory Usage - exec("/sbin/sysctl -n hw.physmem hw.pagesize vm.stats.vm.v_inactive_count vm.stats.vm.v_cache_count vm.stats.vm.v_free_count",$return,$status); - if ($status===0) { - $return=array_map('intval',$return); + exec("/sbin/sysctl -n hw.physmem hw.pagesize vm.stats.vm.v_inactive_count vm.stats.vm.v_cache_count vm.stats.vm.v_free_count", $return, $status); + if ($status === 0) { + $return = array_map('intval', $return); if ($return === array_filter($return, 'is_int')) { return [ - 'mem_total' => (int) $return[0]/1024, - 'mem_free' => (int) $return[1]*($return[2]+$return[3]+$return[4])/1024, + 'mem_total' => (int)$return[0]/1024, + 'mem_free' => (int)$return[1] * ($return[2] + $return[3] + $return[4]) / 1024, 'swap_free' => (isset($swapFree)) ? $swapFree : 'N/A', 'swap_total' => (isset($swapTotal)) ? $swapTotal : 'N/A' ]; @@ -179,4 +184,21 @@ class SystemStatistics { return $info; } + /** + * Checks if a function is available. Borrowed from + * https://github.com/nextcloud/server/blob/2e36069e24406455ad3f3998aa25e2a949d1402a/lib/private/legacy/helper.php#L475 + * + * @param string $function_name + * @return bool + */ + public function is_function_enabled($function_name) { + if (!function_exists($function_name)) { + return false; + } + if ($this->phpIni->listContains('disable_functions', $function_name)) { + return false; + } + return true; + } + } |