diff options
author | Daniel Kesselberg <mail@danielkesselberg.de> | 2020-03-16 16:37:53 +0300 |
---|---|---|
committer | Daniel Kesselberg <mail@danielkesselberg.de> | 2020-03-17 22:33:38 +0300 |
commit | c4e07900090877a1e8c9baef796b3af2ab5b99a2 (patch) | |
tree | 1c05045aebc5e48c271e3dc0d1bf39e5446b2dd9 /lib | |
parent | d3379160ce8154634573c7111cfdec7923394193 (diff) |
Use /proc/uptime to read the uptime
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Controller/ApiController.php | 27 | ||||
-rw-r--r-- | lib/OperatingSystems/DefaultOs.php | 22 | ||||
-rw-r--r-- | lib/Os.php | 9 |
3 files changed, 50 insertions, 8 deletions
diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index bbd2f31..455fa4a 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -116,7 +116,7 @@ class ApiController extends OCSController { */ public function BasicData(): DataResponse { $servertime = $this->os->getTime(); - $uptime = $this->os->getUptime(); + $uptime = $this->formatUptime($this->os->getUptime()); $timeservers = $this->os->getTimeServers()[0]; return new DataResponse([ @@ -145,4 +145,29 @@ class ApiController extends OCSController { } return 'unknown'; } + + /** + * Return the uptime of the system as human readable value + * + * @param int $uptime + * @return string + */ + private function formatUptime(int $uptime): string { + if ($uptime === -1) { + return 'Unknown'; + } + + try { + $boot = new \DateTime($uptime . ' seconds ago'); + } catch (\Exception $e) { + return 'Unknown'; + } + + $interval = $boot->diff(new \DateTime()); + if ($interval->d > 0) { + return $interval->format('%d days, %h hours, %i minutes, %s seconds'); + } + + return $interval->format('%h hours, %i minutes, %s seconds'); + } } diff --git a/lib/OperatingSystems/DefaultOs.php b/lib/OperatingSystems/DefaultOs.php index b29dccd..f7d71f2 100644 --- a/lib/OperatingSystems/DefaultOs.php +++ b/lib/OperatingSystems/DefaultOs.php @@ -32,6 +32,9 @@ class DefaultOs { /** @var string */ protected $meminfo; + /** @var string */ + protected $uptime; + /** * @return bool */ @@ -133,10 +136,23 @@ class DefaultOs { } /** - * @return string + * Get the total number of seconds the system has been up or -1 on failure. + * + * @return int */ - public function getUptime() { - $uptime = shell_exec('uptime -p'); + public function getUptime(): int { + $data = -1; + + if ($this->uptime === null) { + $this->uptime = $this->readContent('/proc/uptime'); + } + + if ($this->uptime === '') { + return $data; + } + + [$uptime,] = array_map('intval', explode(' ', $this->uptime)); + return $uptime; } @@ -124,11 +124,12 @@ class Os { } /** - * @return string + * Get the total number of seconds the system has been up + * + * @return int */ - public function getUptime() { - $data = $this->backend->getUptime(); - return $data; + public function getUptime(): int { + return $this->backend->getUptime(); } /** |