Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
david-d-h committed Oct 4, 2023
2 parents 3a32ba3 + cadc63a commit b772d01
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 36 deletions.
39 changes: 39 additions & 0 deletions src/Checks/RebootCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Vormkracht10\LaravelOK\Checks;

use Carbon\Carbon;
use Vormkracht10\LaravelOK\Checks\Base\Check;
use Vormkracht10\LaravelOK\Checks\Base\Result;
use Vormkracht10\LaravelOK\Checks\Traits\ReadsBootTimes;

class RebootCheck extends Check
{
use ReadsBootTimes;

protected Carbon $minTimeSinceReboot;

public function setMinTimeSinceReboot(Carbon $timestamp): static
{
$this->minTimeSinceReboot = $timestamp;

return $this;
}

public function run(): Result
{
$result = Result::new();

$timestamp = $this->getSystemUptime();

if (! isset($this->minTimeSinceReboot)) {
throw new \Exception('The minimum time since reboot was not set.');
}

if ($this->minTimeSinceReboot < $timestamp) {
return $result->failed("Last reboot was at [{$this->minTimeSinceReboot->format('Y-m-d H:i')}], the minimum uptime for this server was set to {$this->minTimeSinceReboot->diffInMinutes()} minutes");
}

return $result->ok("Last reboot {$timestamp->diffInDays()} days and {$timestamp->diffInMinutes()} ago");
}
}
43 changes: 43 additions & 0 deletions src/Checks/Traits/ReadsBootTimes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Vormkracht10\LaravelOK\Checks\Traits;

use Carbon\Carbon;
use Illuminate\Support\Facades\Process;
use RuntimeException;

trait ReadsBootTimes
{
protected function getSystemUptime(): Carbon
{
return match ($os = PHP_OS) {
'Linux' => $this->getSystemUptimeLinux(),
'Darwin' => $this->getSystemUptimeDarwin(),
default => throw new RuntimeException("This os ({$os}) is not supported by the UptimeCheck"),
};
}

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'),
)->roundMinute();
}
}
40 changes: 4 additions & 36 deletions src/Checks/UptimeCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
namespace Vormkracht10\LaravelOK\Checks;

use Carbon\Carbon;
use Illuminate\Support\Facades\Process;
use RuntimeException;
use Vormkracht10\LaravelOK\Checks\Base\Check;
use Vormkracht10\LaravelOK\Checks\Base\Result;
use Vormkracht10\LaravelOK\Checks\Traits\ReadsBootTimes;

class UptimeCheck extends Check
{
use ReadsBootTimes;

protected Carbon $maxTimeSinceRebootTimestamp;

public function setMaxTimeSinceRebootTimestamp(Carbon $time): static
Expand All @@ -30,42 +31,9 @@ public function run(): Result
}

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->failed("Last reboot was at [{$timestamp->format('Y-m-d H:i')}], 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 ($os = PHP_OS) {
'Linux' => $this->getSystemUptimeLinux(),
'Darwin' => $this->getSystemUptimeDarwin(),
default => throw new RuntimeException("This os ({$os}) is not supported by the UptimeCheck"),
};
}

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'),
);
}
}

0 comments on commit b772d01

Please sign in to comment.