|
| 1 | +# Laravel Health Monitor |
| 2 | +A lightweight, extendable Laravel package for monitoring the health of internal services like Database, Redis, Queue, Cache, Filesystem, and any third-party APIs. |
| 3 | + |
| 4 | +Built for developers who care about observability and application resilience. |
| 5 | + |
| 6 | +## Installation |
| 7 | +Install the package via Composer: |
| 8 | +``` |
| 9 | +composer require mrtolouei/laravel-health-monitor |
| 10 | +``` |
| 11 | +Publish the configuration and provider stub: |
| 12 | +``` |
| 13 | +php artisan vendor:publish --tag=health-monitor-config |
| 14 | +php artisan vendor:publish --tag=health-monitor-provider |
| 15 | +``` |
| 16 | + |
| 17 | +## Configuration |
| 18 | +The config file will be published to: |
| 19 | + |
| 20 | +`config/health-monitor.php` |
| 21 | + |
| 22 | +It contains two main sections: |
| 23 | +### ✅ App Services |
| 24 | +You can enable/disable health checkers and configure each one: |
| 25 | +```php |
| 26 | +'app-services' => [ |
| 27 | + 'database' => [ |
| 28 | + 'enabled' => true, |
| 29 | + 'inspector' => HealthMonitor\Monitors\DatabaseHealthChecker::class, |
| 30 | + 'arguments' => [ |
| 31 | + 'connection' => config('database.default', 'sqlite') |
| 32 | + ], |
| 33 | + ], |
| 34 | + 'filesystem' => [ |
| 35 | + 'enabled' => true, |
| 36 | + 'inspector' => HealthMonitor\Monitors\FilesystemHealthChecker::class, |
| 37 | + 'arguments' => [ |
| 38 | + 'disk' => config('filesystems.default', 'local') |
| 39 | + ], |
| 40 | + ], |
| 41 | + ... |
| 42 | +], |
| 43 | +``` |
| 44 | + |
| 45 | +### 🌐 Third-Party APIs |
| 46 | +You can define any number of third-party HTTP APIs to monitor: |
| 47 | + |
| 48 | +```php |
| 49 | +'third-party-services' => [ |
| 50 | + 'json-test-api' => [ |
| 51 | + 'enabled' => true, |
| 52 | + 'url' => 'https://jsonplaceholder.typicode.com/todos/1', |
| 53 | + 'method' => 'GET', |
| 54 | + 'headers' => [ |
| 55 | + 'Accept' => 'application/json', |
| 56 | + ], |
| 57 | + 'auth' => null, |
| 58 | + 'timeout' => 5, |
| 59 | + 'expected_status' => 200, |
| 60 | + ], |
| 61 | +], |
| 62 | +``` |
| 63 | + |
| 64 | +Supported authentication types: |
| 65 | + |
| 66 | +- `none` |
| 67 | +- `basic` (username & password) |
| 68 | +- `bearer` (token) |
| 69 | +- `ntlm` |
| 70 | + |
| 71 | +### 🔒 Access Control |
| 72 | +A `Gate` is registered automatically to restrict access to non-local environments. |
| 73 | + |
| 74 | +You can customize allowed users in `config/health-monitor.php`: |
| 75 | +```php |
| 76 | +'allowed_emails' => [ |
| 77 | + |
| 78 | + |
| 79 | +], |
| 80 | +``` |
| 81 | + |
| 82 | +### 📡 Routes |
| 83 | +This package registers two routes (behind the `viewHealthMonitor` gate): |
| 84 | + |
| 85 | +| Route | Method | Description | |
| 86 | +|--------------------|--------|-------------------------------------| |
| 87 | +| `/api-service-health`| `GET` | Returns JSON of health check status| |
| 88 | +| `/service-health` | `GET` | Shows a Blade view of statuses | |
| 89 | + |
| 90 | +### 🧪 Add Custom Checkers |
| 91 | +Create a new class that implements the `HealthCheckerInterface`: |
| 92 | + |
| 93 | +```php |
| 94 | +use HealthMonitor\Contracts\HealthCheckerInterface; |
| 95 | +use HealthMonitor\Dto\HealthResult; |
| 96 | + |
| 97 | +class CustomChecker implements HealthCheckerInterface { |
| 98 | + public function monitor(): HealthResult { |
| 99 | + return new HealthResult('My Checker', 'custom', true); |
| 100 | + } |
| 101 | + |
| 102 | + public function getConnectionDriver(): string|null { |
| 103 | + return 'custom'; |
| 104 | + } |
| 105 | +} |
| 106 | +``` |
| 107 | +Then register it in the config file. |
| 108 | + |
| 109 | +### 📂 Directory Structure (short overview) |
| 110 | +```text |
| 111 | +laravel-health-monitor/ |
| 112 | +├── config/ |
| 113 | +├── src/ |
| 114 | +│ ├── Contracts/ |
| 115 | +│ ├── Dto/ |
| 116 | +│ ├── Monitors/ |
| 117 | +│ ├── Facades/ |
| 118 | +│ ├── Factories/ |
| 119 | +│ └── HealthMonitorExecutor.php |
| 120 | +└── routes/web.php |
| 121 | +``` |
| 122 | + |
| 123 | +### 🙌 Credits |
| 124 | +Developed and maintained by [Ali Tolouei](https://github.com/mrtolouei) |
| 125 | + |
| 126 | +Feel free to contribute or suggest features via issues and PRs! |
| 127 | + |
| 128 | +### 📄 License |
| 129 | +This package is open-sourced software licensed under the [MIT license](LICENSE). |
0 commit comments