From ace82fcacd914abc1382eb8dd5b8c2e655ea2ac3 Mon Sep 17 00:00:00 2001 From: David <75451291+dulkoss@users.noreply.github.com> Date: Tue, 26 Sep 2023 13:47:38 +0200 Subject: [PATCH 1/4] add UptimeCheck --- src/Checks/UptimeCheck.php | 66 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/Checks/UptimeCheck.php diff --git a/src/Checks/UptimeCheck.php b/src/Checks/UptimeCheck.php new file mode 100644 index 0000000..ccbcf38 --- /dev/null +++ b/src/Checks/UptimeCheck.php @@ -0,0 +1,66 @@ +maxTimeSinceRebootTimestamp = $time; + + return $this; + } + + public function run(): Result + { + $result = Result::new(); + + $timestamp = $this->getSystemUptime(); + + if ($this->maxTimeSinceRebootTimestamp > $timestamp) { + return $result->failed("Last reboot was at [{$timestamp}], the maximum uptime for this server was set to [{$this->maxTimeSinceRebootTimestamp}]"); + } + + return $result->ok("Last reboot {$timestamp->diffInDays()} days and {$timestamp->diffInMinutes()} ago"); + } + + protected function getSystemUptime(): Carbon + { + return match (PHP_OS) { + "Linux" => $this->getSystemUptimeLinux(), + "Darwin" => $this->getSystemUptimeDarwin(), + }; + } + + protected function runProcess(string $command): string + { + $process = Process::run($command); + + return match ($process->successful()) { + true => $process->output(), + false => throw new RuntimeException('Could not get system boot timestamp: ' . $process->errorOutput()), + }; + } + + protected function getSystemUptimeLinux(): Carbon + { + return Carbon::createFromTimestamp( + $this->runProcess('date -d "$(who -b | awk \'{print $3, $4}\')" +%s'), + ); + } + + protected function getSystemUptimeDarwin(): Carbon + { + return Carbon::createFromTimestamp( + $this->runProcess('date -j -f "%b %d %H:%M" "$(who -b | awk \'{print $3,$4,$5}\')" +%s'), + ); + } +} From 184733b7615d8e2b78b9aac81ad61c37321b6a63 Mon Sep 17 00:00:00 2001 From: dulkoss Date: Tue, 26 Sep 2023 11:48:01 +0000 Subject: [PATCH 2/4] Fix styling --- src/Checks/UptimeCheck.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Checks/UptimeCheck.php b/src/Checks/UptimeCheck.php index ccbcf38..cd2be1d 100644 --- a/src/Checks/UptimeCheck.php +++ b/src/Checks/UptimeCheck.php @@ -35,8 +35,8 @@ public function run(): Result protected function getSystemUptime(): Carbon { return match (PHP_OS) { - "Linux" => $this->getSystemUptimeLinux(), - "Darwin" => $this->getSystemUptimeDarwin(), + 'Linux' => $this->getSystemUptimeLinux(), + 'Darwin' => $this->getSystemUptimeDarwin(), }; } @@ -46,7 +46,7 @@ protected function runProcess(string $command): string return match ($process->successful()) { true => $process->output(), - false => throw new RuntimeException('Could not get system boot timestamp: ' . $process->errorOutput()), + false => throw new RuntimeException('Could not get system boot timestamp: '.$process->errorOutput()), }; } From 60a6b9de6e6e2798aab609b2da02d3b834064343 Mon Sep 17 00:00:00 2001 From: David <75451291+dulkoss@users.noreply.github.com> Date: Wed, 27 Sep 2023 10:02:13 +0200 Subject: [PATCH 3/4] check if `maxTimeSinceRebootTimestamp` is set --- src/Checks/UptimeCheck.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Checks/UptimeCheck.php b/src/Checks/UptimeCheck.php index cd2be1d..13407c4 100644 --- a/src/Checks/UptimeCheck.php +++ b/src/Checks/UptimeCheck.php @@ -25,6 +25,10 @@ public function run(): Result $timestamp = $this->getSystemUptime(); + if (! isset($this->maxTimeSinceRebootTimestamp)) { + throw new \Exception('The max time since reboot was not set.'); + } + if ($this->maxTimeSinceRebootTimestamp > $timestamp) { return $result->failed("Last reboot was at [{$timestamp}], the maximum uptime for this server was set to [{$this->maxTimeSinceRebootTimestamp}]"); } From e43120db83e8b0da459fd62f21fee27a83535148 Mon Sep 17 00:00:00 2001 From: David <75451291+dulkoss@users.noreply.github.com> Date: Wed, 27 Sep 2023 10:05:22 +0200 Subject: [PATCH 4/4] fix phpstan errors --- src/Checks/UptimeCheck.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Checks/UptimeCheck.php b/src/Checks/UptimeCheck.php index 13407c4..5b77401 100644 --- a/src/Checks/UptimeCheck.php +++ b/src/Checks/UptimeCheck.php @@ -38,9 +38,10 @@ public function run(): Result protected function getSystemUptime(): Carbon { - return match (PHP_OS) { + return match ($os = PHP_OS) { 'Linux' => $this->getSystemUptimeLinux(), 'Darwin' => $this->getSystemUptimeDarwin(), + default => throw new RuntimeException("This os ({$os}) is not supported by the UptimeCheck"), }; }