spatie/server-monitor-app
Laravel-based command-line app to monitor your servers’ health (disk, memory, processes, etc.) with built-in checks and alerts via Slack or email. Powered by Spatie’s laravel-server-monitor; ideal if you want a ready-to-run monitoring app.
Installation:
composer create-project spatie/server-monitor-app
Clone the repo and run composer install.
Configuration:
.env.example to .env and update database credentials (MySQL/PostgreSQL/SQLite).php artisan migrate
.env (e.g., CHECK_DISK_SPACE=true, CHECK_PROCESSES=true).First Run:
* * * * * cd /path/to/server-monitor-app && php artisan schedule:run >> /dev/null 2>&1
http://localhost:8000 (serve with php artisan serve for testing).Notifications:
.env (e.g., SLACK_WEBHOOK_URL, MAIL_MAILER).config/server-monitor.php..env:
CHECK_DISK_SPACE=true
DISK_SPACE_CHECK_THRESHOLD=10
/, /var, etc., in config/server-monitor.php.php artisan server-monitor:check
Check Creation:
app/Checks.CheckRedisMemory.php:
namespace App\Checks;
use Spatie\ServerMonitor\Checks\Check;
class CheckRedisMemory extends Check
{
public function getName(): string
{
return 'Redis Memory Usage';
}
public function check(): array
{
$memory = shell_exec('redis-cli info memory | grep used_memory');
return ['memory_used' => $memory];
}
public function isOk(): bool
{
return (int) $this->memoryUsed < 100000000; // 100MB threshold
}
}
config/server-monitor.php under custom_checks.Notification Routing:
EventServiceProvider:
protected $listen = [
\Spatie\ServerMonitor\Events\CheckFailed::class => [
\App\Listeners\SendCustomSlackAlert::class,
],
];
app/Listeners/SendCustomSlackAlert.php) to modify payloads.Dashboard Customization:
resources/views/vendor/server-monitor.php artisan vendor:publish --tag=server-monitor-assets
Multi-Server Monitoring:
.env configurations.Laravel Integration:
laravel-server-monitor directly in your Laravel app for tighter integration:
composer require spatie/laravel-server-monitor
CI/CD Hooks:
- name: Run Server Checks
run: php artisan server-monitor:check
Logging:
use Monolog\Logger;
$logger = new Logger('server_monitor');
$logger->pushHandler(new \Monolog\Handler\SyslogHandler('server_monitor'));
event(new \Spatie\ServerMonitor\Events\CheckFailed($check, $result));
API Access:
Route::get('/api/checks', function () {
return \Spatie\ServerMonitor\Facades\ServerMonitor::checks()->latest()->take(10)->get();
});
Cron Job Misconfiguration:
php artisan schedule:run
Check Dependencies:
CheckProcesses) require specific system tools (e.g., ps). Verify these are installed on all servers.shell_exec cautiously—sanitize inputs to avoid command injection.Database Locks:
php artisan queue:work
Timezone Mismatches:
.env). Use date_default_timezone_set() in checks if needed.Notification Delays:
Check Logs:
.env (APP_DEBUG=true) and check storage/logs/laravel.log.php artisan server-monitor:check --verbose for detailed output.Test Checks Locally:
$this->expectFacadeInstance(\Spatie\ServerMonitor\Facades\ServerMonitor::class)
->shouldReceive('executeCommand')
->andReturn('1000000'); // Simulate disk usage
Database Issues:
php artisan migrate:fresh
Custom Check Templates:
namespace App\Checks;
use Spatie\ServerMonitor\Checks\Check;
class CustomCheck extends Check
{
public function getName(): string { return 'Custom Check'; }
public function check(): array { return ['data' => 'value']; }
public function isOk(): bool { return true; }
public function getHelpText(): string { return 'Description of the check.'; }
}
Threshold Tuning:
Exclude Checks:
.env:
CHECK_DISK_SPACE=false
Backup Config:
.env to version control (with sensitive values removed) for consistency across deployments.Monitor the Monitor:
Route::get('/health', function () {
return response()->json(['status' => 'ok']);
});
CheckHttpEndpoint to verify this route periodically.Legacy Systems:
laravel-server-monitor package directly with a custom Laravel app (instead of this monolithic app).Documentation:
How can I help you explore Laravel packages today?