spatie/cpu-load-health-check
Laravel Health check for monitoring CPU load. Configure thresholds (e.g., average load over the last 5 minutes) and get notifications when load is too high. Integrates seamlessly with spatie/laravel-health.
Installation:
composer require spatie/cpu-load-health-check
Publish the config (optional):
php artisan vendor:publish --provider="Spatie\CpuLoadHealthCheck\CpuLoadHealthCheckServiceProvider"
First Use Case:
Register the check in a service provider (e.g., AppServiceProvider):
use Spatie\Health\Facades\Health;
use Spatie\CpuLoadHealthCheck\CpuLoadCheck;
Health::checks([
CpuLoadCheck::new()->failWhenLoadIsHigherInTheLast5Minutes(1.2),
]);
1.2 with your threshold (e.g., 1.5 for 150% CPU load).Trigger Health Check:
Visit /health (or your configured endpoint) to see the result.
Dynamic Thresholds: Use environment variables or config to adjust thresholds dynamically:
$threshold = config('health.cpu_threshold', 1.2);
Health::checks([
CpuLoadCheck::new()->failWhenLoadIsHigherInTheLast5Minutes($threshold),
]);
Integration with Notifications:
Combine with laravel-health's notification system:
Health::checks([
CpuLoadCheck::new()
->failWhenLoadIsHigherInTheLast5Minutes(1.5)
->notifyWhenFailed(),
]);
Scheduled Monitoring:
Run health checks via Laravel's scheduler (e.g., php artisan health:check):
// app/Console/Kernel.php
$schedule->command('health:check')->everyFiveMinutes();
Custom Metrics: Extend the check to log additional metrics (e.g., peak load):
CpuLoadCheck::new()
->failWhenLoadIsHigherInTheLast5Minutes(1.2)
->then(function ($result) {
\Log::info('CPU Load Check', ['load' => $result->load, 'status' => $result->status]);
});
Multi-Environment Checks: Use different thresholds per environment (e.g., staging vs. production):
$threshold = env('CPU_THRESHOLD', config('health.cpu_threshold'));
Platform-Specific CPU Reporting:
sys_getloadavg() (1-minute average by default).Threshold Tuning:
1.0 for 100% load).Performance Overhead:
Time Window Misconfiguration:
failWhenLoadIsHigherInTheLastXMinutes() uses a rolling window. Ensure X aligns with your monitoring frequency.Check Raw Data: Inspect the raw load values via:
$check = CpuLoadCheck::new();
$result = $check->perform();
\Log::debug('Raw CPU Load', ['load' => $result->load]);
Verify System Load:
Cross-check with top, htop, or glances to confirm reported values.
Notification Delays: If notifications are slow, ensure your mail/Slack/etc. queues are processing efficiently.
Combine with Other Checks:
Pair with Spatie\Health\Checks\MemoryCheck for holistic monitoring:
Health::checks([
CpuLoadCheck::new()->failWhenLoadIsHigherInTheLast5Minutes(1.2),
MemoryCheck::new()->failWhenMemoryUsageIsAbove(80), // 80% of available RAM
]);
Custom Failure Messages: Override default messages:
CpuLoadCheck::new()
->failWhenLoadIsHigherInTheLast5Minutes(1.5)
->whenFailed(function () {
return 'High CPU load detected! Current: ' . number_format($this->load, 2) . '%';
});
Exclude Specific Processes: If certain processes skew results (e.g., backups), run checks during low-activity windows.
Leverage Laravel Health Dashboard:
Use spatie/laravel-health’s dashboard to visualize trends over time.
Testing: Simulate high load in tests:
public function test_cpu_load_check()
{
$check = CpuLoadCheck::new();
$result = $check->perform();
$this->assertFalse($result->isHealthy()); // If load > threshold
}
How can I help you explore Laravel packages today?