diff options
author | Daniel Kesselberg <mail@danielkesselberg.de> | 2020-03-16 15:56:45 +0300 |
---|---|---|
committer | Daniel Kesselberg <mail@danielkesselberg.de> | 2020-03-17 22:29:03 +0300 |
commit | d3379160ce8154634573c7111cfdec7923394193 (patch) | |
tree | 7c312836aa8a841e70ff78c79aa09424259a8ca8 /lib | |
parent | 04870e1442a5193401e7dbe19479007caae23a6a (diff) |
Read cpu name from proc without shell_exec
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/OperatingSystems/DefaultOs.php | 37 | ||||
-rw-r--r-- | lib/Os.php | 7 |
2 files changed, 35 insertions, 9 deletions
diff --git a/lib/OperatingSystems/DefaultOs.php b/lib/OperatingSystems/DefaultOs.php index 06cd5fc..b29dccd 100644 --- a/lib/OperatingSystems/DefaultOs.php +++ b/lib/OperatingSystems/DefaultOs.php @@ -27,6 +27,9 @@ namespace OCA\ServerInfo\OperatingSystems; class DefaultOs { /** @var string */ + protected $cpuinfo; + + /** @var string */ protected $meminfo; /** @@ -63,6 +66,7 @@ class DefaultOs { $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; } @@ -86,17 +90,38 @@ class DefaultOs { } /** + * Get name of the processor + * * @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\''); + public function getCPUName(): string { + $data = 'Unknown Processor'; + + if ($this->cpuinfo === null) { + $this->cpuinfo = $this->readContent('/proc/cpuinfo'); + } + + if ($this->cpuinfo === '') { + return $data; + } + + $matches = []; + $pattern = '/model name\s:\s(.+)/'; + + if (preg_match_all($pattern, $this->cpuinfo, $matches) === false) { + return $data; + } + + $model = $matches[1][0]; + $cores = count($matches[1]); + if ($cores === 1) { - $cores = ' (' . $cores . ' core)'; + $data = $model . ' (1 core)'; } else { - $cores = ' (' . $cores . ' cores)'; + $data = $model . ' (' . $cores . ' cores)'; } - return $cpu . ' ' . $cores; + + return $data; } /** @@ -107,11 +107,12 @@ class Os { } /** + * Get name of the processor + * * @return string */ - public function getCPUName() { - $data = $this->backend->getCPUName(); - return $data; + public function getCPUName(): string { + return $this->backend->getCPUName(); } /** |