spatie/laravel-server-monitor
Monitor the health of your servers from Laravel. Includes built-in checks (disk, memory, processes, etc.), easy custom checks, and notifications via Slack or email when something goes wrong.
Start by installing the package via Composer and publishing its migrations and config file. Run php artisan vendor:publish --provider="Spatie\ServerMonitor\ServerMonitorServiceProvider" twice (once for migrations, once for config), then php artisan migrate. Next, add your servers to the hosts table (or seed them), and configure SSH access (SSH key-based auth is strongly recommended). Register the server-monitor:run-checks command in your console kernel to run every minute. The first thing you’ll likely want to do is enable built-in checks like diskspace, mysql, and elasticsearch in config/server-monitor.php.
Use Eloquent models (Host, Check) directly for programmatic management: e.g., Host::create(['name' => 'prod-web-01', 'hostname' => '10.0.0.5']). Create custom checks by extending CheckDefinition and implementing run() and parseOutput() — ideal for app-specific health signals (e.g., checking Laravel Horizons status, Redis queue depths, or custom health endpoints via localhost curl). Schedule recurring checks on a per-host basis by setting the next_run_in_minutes attribute on the Host model. Leverage events (e.g., CheckFailed, CheckRestored) to integrate with internal tools (e.g., PagerDuty, Opsgenie, or custom alerting dashboards) via listeners. Use the Spatie\ServerMonitor\CheckDefinition type-hinted interfaces and traits (HasCustomAttribute, IsRunnable) to safely inject config or environment-specific parameters.
SSH key permissions are critical — ensure the web server user (e.g., www-data) has read access to the private key (not world-readable for security). Set concurrent_ssh_connections low (e.g., 2–3) on shared dev/staging infrastructure to avoid lockouts; raise it on production only if needed and with proper SSH hardening. Use environment variables for sensitive values (SERVER_MONITOR_SLACK_WEBHOOK_URL, SSH keys) and never commit secrets. Tip: Wrap ssh in a wrapper script with set -e and custom timeouts to catch hanging connections (the package times out by default at ~15s). Custom check tip: Log verbose output via Laravel\Logs\Logger or standard output (which is captured and stored) to aid debugging. Remember: notifications only fire on state transitions (OK → warning/fail, fail → warning, etc.) and are throttled by throttle_failing_notifications_for_minutes. Extension point: use the process_manipulator config to inject retry logic, environment cleanup, or anonymization of sensitive hostnames before SSH execution.
How can I help you explore Laravel packages today?