Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Cpu Load Health Check Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require spatie/cpu-load-health-check
    

    Publish the config (optional):

    php artisan vendor:publish --provider="Spatie\CpuLoadHealthCheck\CpuLoadHealthCheckServiceProvider"
    
  2. 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),
    ]);
    
    • Replace 1.2 with your threshold (e.g., 1.5 for 150% CPU load).
  3. Trigger Health Check: Visit /health (or your configured endpoint) to see the result.


Implementation Patterns

Common Workflows

  1. Dynamic Thresholds: Use environment variables or config to adjust thresholds dynamically:

    $threshold = config('health.cpu_threshold', 1.2);
    Health::checks([
        CpuLoadCheck::new()->failWhenLoadIsHigherInTheLast5Minutes($threshold),
    ]);
    
  2. Integration with Notifications: Combine with laravel-health's notification system:

    Health::checks([
        CpuLoadCheck::new()
            ->failWhenLoadIsHigherInTheLast5Minutes(1.5)
            ->notifyWhenFailed(),
    ]);
    
  3. Scheduled Monitoring: Run health checks via Laravel's scheduler (e.g., php artisan health:check):

    // app/Console/Kernel.php
    $schedule->command('health:check')->everyFiveMinutes();
    
  4. 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]);
        });
    
  5. Multi-Environment Checks: Use different thresholds per environment (e.g., staging vs. production):

    $threshold = env('CPU_THRESHOLD', config('health.cpu_threshold'));
    

Gotchas and Tips

Pitfalls

  1. Platform-Specific CPU Reporting:

    • Linux: Uses sys_getloadavg() (1-minute average by default).
    • Windows: May report inaccurate values (test thoroughly).
    • Docker/Kubernetes: Load averages may not reflect host CPU usage.
  2. Threshold Tuning:

    • Start with conservative values (e.g., 1.0 for 100% load).
    • Monitor false positives/negatives before setting alerts.
  3. Performance Overhead:

    • Frequent checks (e.g., every minute) may add negligible overhead, but avoid excessive logging.
  4. Time Window Misconfiguration:

    • failWhenLoadIsHigherInTheLastXMinutes() uses a rolling window. Ensure X aligns with your monitoring frequency.

Debugging

  1. Check Raw Data: Inspect the raw load values via:

    $check = CpuLoadCheck::new();
    $result = $check->perform();
    \Log::debug('Raw CPU Load', ['load' => $result->load]);
    
  2. Verify System Load: Cross-check with top, htop, or glances to confirm reported values.

  3. Notification Delays: If notifications are slow, ensure your mail/Slack/etc. queues are processing efficiently.

Tips

  1. 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
    ]);
    
  2. Custom Failure Messages: Override default messages:

    CpuLoadCheck::new()
        ->failWhenLoadIsHigherInTheLast5Minutes(1.5)
        ->whenFailed(function () {
            return 'High CPU load detected! Current: ' . number_format($this->load, 2) . '%';
        });
    
  3. Exclude Specific Processes: If certain processes skew results (e.g., backups), run checks during low-activity windows.

  4. Leverage Laravel Health Dashboard: Use spatie/laravel-health’s dashboard to visualize trends over time.

  5. 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
    }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport