Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add CpuLoadCheck #34

Merged
merged 2 commits into from
Sep 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/Checks/CpuLoadCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Vormkracht10\LaravelOK\Checks;

use Exception;
use Vormkracht10\LaravelOK\Checks\Base\Check;
use Vormkracht10\LaravelOK\Checks\Base\Result;

class CpuLoadCheck extends Check
{
protected array $maxLoad;

public function setMaxLoad(
float $short = null,
float $mid = null,
float $long = null,
): static {
$this->maxLoad = [$short, $mid, $long];

return $this;
}

public function run(): Result
{
if (! isset($this->maxLoad)) {
throw new Exception('The max average load was not set');
}

$result = Result::new();

$load = sys_getloadavg();

if (! $load) {
return $result->failed('Failed to get system load averages');
}

foreach ($this->maxLoad as $index => $max) {
if (is_null($max)) {
continue;
}

if ($index == count($load)) {
break;
}

if ($max < $actual = round($load[$index], 2)) {
return $result->failed(
"System average load [{$index}] is at {$max}%, max is configured at {$actual}%",
);
}
}

return $result->ok('System load averages are ok');
}
}