diff options
author | Daniel Kesselberg <mail@danielkesselberg.de> | 2020-03-15 22:31:52 +0300 |
---|---|---|
committer | Daniel Kesselberg <mail@danielkesselberg.de> | 2020-03-17 22:29:02 +0300 |
commit | 04870e1442a5193401e7dbe19479007caae23a6a (patch) | |
tree | 7bf29412a0c034b48c94e00afc2006acafce892e /lib | |
parent | b2ed6c5ceab3734a842f9be5c0455d4786ef15e4 (diff) |
Rewrite getMemory to return a array of information
Don't use shell_exec to fetch that information anymore, Make it testable
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/OperatingSystems/DefaultOs.php | 55 | ||||
-rw-r--r-- | lib/Os.php | 11 |
2 files changed, 50 insertions, 16 deletions
diff --git a/lib/OperatingSystems/DefaultOs.php b/lib/OperatingSystems/DefaultOs.php index 8aa0894..06cd5fc 100644 --- a/lib/OperatingSystems/DefaultOs.php +++ b/lib/OperatingSystems/DefaultOs.php @@ -26,7 +26,8 @@ namespace OCA\ServerInfo\OperatingSystems; */ class DefaultOs { - public function __construct() {} + /** @var string */ + protected $meminfo; /** * @return bool @@ -44,18 +45,44 @@ class DefaultOs { } /** - * @return string + * Get memory will return a list key => value where all values are in bytes. + * [MemTotal => 0, MemFree => 0, MemAvailable => 0, SwapTotal => 0, SwapFree => 0]. + * + * @return array */ - 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'; + public function getMemory(): array { + $data = ['MemTotal' => -1, 'MemFree' => -1, 'MemAvailable' => -1, 'SwapTotal' => -1, 'SwapFree' => -1]; + + if ($this->meminfo === null) { + $this->meminfo = $this->readContent('/proc/meminfo'); } - return $memory; + + if ($this->meminfo === '') { + return $data; + } + + $matches = []; + $pattern = '/(?<Key>(?:MemTotal|MemFree|MemAvailable|SwapTotal|SwapFree)+):\s+(?<Value>\d+)\s+(?<Unit>\w{2})/'; + if (preg_match_all($pattern, $this->meminfo, $matches) === false) { + return $data; + } + + $keys = array_map('trim', $matches['Key']); + $values = array_map('trim', $matches['Value']); + $units = array_map('trim', $matches['Unit']); + + foreach ($keys as $i => $key) { + $value = (int)$values[$i]; + $unit = $units[$i]; + + if ($unit === 'kB') { + $value *= 1000; + } + + $data[$key] = $value; + } + + return $data; } /** @@ -173,4 +200,10 @@ class DefaultOs { return $result; } + protected function readContent(string $filename): string { + if (is_readable($filename)) { + return file_get_contents($filename); + } + return ''; + } } @@ -22,7 +22,6 @@ 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; @@ -98,11 +97,13 @@ class Os { } /** - * @return string + * Get memory will return a list key => value where all values are in bytes. + * [MemTotal => 0, MemFree => 0, MemAvailable => 0, SwapTotal => 0, SwapFree => 0]. + * + * @return array */ - public function getMemory() { - $data = $this->backend->getMemory(); - return $data; + public function getMemory(): array { + return $this->backend->getMemory(); } /** |