An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.
PHP coolifydatabasesdeploymentdockerdocker-composeinertiajslaravelmariadbmysqlnextjsnodejsphppostgresredisself-hostedself-hostingserverstatic-sitesveltesvelte5
feat: container metrics
Andras Bacsaic81ad5cd03a8
diff --git a/app/Actions/Server/StartSentinel.php b/app/Actions/Server/StartSentinel.phpindex a2afea3bbc..b79bc8f672 100644--- a/app/Actions/Server/StartSentinel.php+++ b/app/Actions/Server/StartSentinel.php@@ -21,6 +21,6 @@ class StartSentinel"docker run --rm --pull always -d -e \"TOKEN={$token}\" -e \"SCHEDULER=true\" -e \"METRICS_HISTORY={$metrics_history}\" -e \"REFRESH_RATE={$refresh_rate}\" --name coolify-sentinel -v /var/run/docker.sock:/var/run/docker.sock -v /data/coolify/metrics:/app/metrics -v /data/coolify/logs:/app/logs --pid host --health-cmd \"curl --fail http://127.0.0.1:8888/api/health || exit 1\" --health-interval 10s --health-retries 3 ghcr.io/coollabsio/sentinel:$version",'chown -R 9999:root /data/coolify/metrics /data/coolify/logs','chmod -R 700 /data/coolify/metrics /data/coolify/logs',- ], $server, false);+ ], $server, true);}}diff --git a/app/Console/Kernel.php b/app/Console/Kernel.phpindex c2f679699b..f529f63b92 100644--- a/app/Console/Kernel.php+++ b/app/Console/Kernel.php@@ -61,7 +61,7 @@ class Kernel extends ConsoleKernel{$servers = $this->all_servers->where('settings.is_usable', true)->where('settings.is_reachable', true)->where('ip', '!=', '1.2.3.4');foreach ($servers as $server) {- if ($server->isMetricsEnabled()) {+ if ($server->isSentinelEnabled()) {$schedule->job(new PullSentinelImageJob($server))->everyFiveMinutes()->onOneServer();}$schedule->job(new PullHelperImageJob($server))->everyFiveMinutes()->onOneServer();diff --git a/app/Jobs/DockerCleanupJob.php b/app/Jobs/DockerCleanupJob.phpindex e637fb6d47..e3ac193dce 100644--- a/app/Jobs/DockerCleanupJob.php+++ b/app/Jobs/DockerCleanupJob.php@@ -22,7 +22,9 @@ class DockerCleanupJob implements ShouldBeEncrypted, ShouldQueuepublic ?int $usageBefore = null;- public function __construct(public Server $server) {}+ public function __construct(public Server $server)+ {+ }public function handle(): void{diff --git a/app/Jobs/PullSentinelImageJob.php b/app/Jobs/PullSentinelImageJob.phpindex 30b36d99f9..e6252df0fe 100644--- a/app/Jobs/PullSentinelImageJob.php+++ b/app/Jobs/PullSentinelImageJob.php@@ -28,7 +28,9 @@ class PullSentinelImageJob implements ShouldBeEncrypted, ShouldQueuereturn $this->server->uuid;}- public function __construct(public Server $server) {}+ public function __construct(public Server $server)+ {+ }public function handle(): void{@@ -50,7 +52,7 @@ class PullSentinelImageJob implements ShouldBeEncrypted, ShouldQueue}ray('Sentinel image is up to date');} catch (\Throwable $e) {- send_internal_notification('PullSentinelImageJob failed with: '.$e->getMessage());+ // send_internal_notification('PullSentinelImageJob failed with: '.$e->getMessage());ray($e->getMessage());throw $e;}diff --git a/app/Jobs/ServerStatusJob.php b/app/Jobs/ServerStatusJob.phpindex 938f3fe401..1bba912c2b 100644--- a/app/Jobs/ServerStatusJob.php+++ b/app/Jobs/ServerStatusJob.php@@ -25,7 +25,9 @@ class ServerStatusJob implements ShouldBeEncrypted, ShouldQueuereturn isDev() ? 1 : 3;}- public function __construct(public Server $server) {}+ public function __construct(public Server $server)+ {+ }public function middleware(): array{@@ -46,7 +48,7 @@ class ServerStatusJob implements ShouldBeEncrypted, ShouldQueueif ($this->server->isFunctional()) {$this->cleanup(notify: false);$this->remove_unnecessary_coolify_yaml();- if ($this->server->isMetricsEnabled()) {+ if ($this->server->isSentinelEnabled()) {$this->server->checkSentinel();}}diff --git a/app/Livewire/Project/Shared/Metrics.php b/app/Livewire/Project/Shared/Metrics.phpnew file mode 100644index 0000000000..d9d7dd3ef8--- /dev/null+++ b/app/Livewire/Project/Shared/Metrics.php@@ -0,0 +1,64 @@+<?php++namespace App\Livewire\Project\Shared;++use Livewire\Component;++class Metrics extends Component+{+ public $resource;++ public $chartId = 'container-cpu';++ public $data;++ public $categories;++ public int $interval = 5;++ public bool $poll = true;++ public function pollData()+ {+ if ($this->poll || $this->interval <= 10) {+ $this->loadData();+ if ($this->interval > 10) {+ $this->poll = false;+ }+ }+ }++ public function loadData()+ {+ try {+ $metrics = $this->resource->getMetrics($this->interval);+ $cpuMetrics = collect($metrics)->map(function ($metric) {+ return [$metric[0], $metric[1]];+ });+ $memoryMetrics = collect($metrics)->map(function ($metric) {+ return [$metric[0], $metric[2]];+ });+ $this->dispatch("refreshChartData-{$this->chartId}-cpu", [+ 'seriesData' => $cpuMetrics,+ ]);+ $this->dispatch("refreshChartData-{$this->chartId}-memory", [+ 'seriesData' => $memoryMetrics,+ ]);+ } catch (\Throwable $e) {+ return handleError($e, $this);+ }+ }++ public function setInterval()+ {+ if ($this->interval <= 10) {+ $this->poll = true;+ }+ $this->loadData();+ }++ public function render()+ {+ return view('livewire.project.shared.metrics');+ }+}diff --git a/app/Livewire/Server/Form.php b/app/Livewire/Server/Form.phpindex 87c0d09d1b..5616123a5f 100644--- a/app/Livewire/Server/Form.php+++ b/app/Livewire/Server/Form.php@@ -44,6 +44,7 @@ class Form extends Component'server.settings.metrics_refresh_rate_seconds' => 'required|integer|min:1','server.settings.metrics_history_days' => 'required|integer|min:1','wildcard_domain' => 'nullable|url',+ 'server.settings.is_server_api_enabled' => 'required|boolean',];protected $validationAttributes = [@@ -63,7 +64,7 @@ class Form extends Component'server.settings.metrics_token' => 'Metrics Token','server.settings.metrics_refresh_rate_seconds' => 'Metrics Interval','server.settings.metrics_history_days' => 'Metrics History',-+ 'server.settings.is_server_api_enabled' => 'Server API',];public function mount()@@ -85,6 +86,18 @@ class Form extends Component$this->dispatch('proxyStatusUpdated');}+ public function checkPortForServerApi()+ {+ try {+ if ($this->server->settings->is_server_api_enabled === true) {+ $this->server->checkServerApi();+ $this->dispatch('success', 'Server API is reachable.');+ }+ } catch (\Throwable $e) {+ return handleError($e, $this);+ }+ }+public function instantSave(){try {@@ -94,12 +107,22 @@ class Form extends Component$this->server->save();$this->dispatch('success', 'Server updated.');$this->dispatch('refreshServerShow');- if ($this->server->isMetricsEnabled()) {+ if ($this->server->isSentinelEnabled()) {PullSentinelImageJob::dispatchSync($this->server);- $this->dispatch('reloadWindow');+ ray('Sentinel is enabled');+ if ($this->server->settings->isDirty('is_metrics_enabled')) {+ $this->dispatch('reloadWindow');+ }+ if ($this->server->settings->isDirty('is_server_api_enabled') && $this->server->settings->is_server_api_enabled === true) {+ ray('Starting sentinel');++ }} else {+ ray('Sentinel is not enabled');StopSentinel::dispatch($this->server);}+ // $this->checkPortForServerApi();+} catch (\Throwable $e) {return handleError($e, $this);}diff --git a/app/Models/Application.php b/app/Models/Application.phpindex f2a7ce51c5..c76a42d71d 100644--- a/app/Models/Application.php+++ b/app/Models/Application.php@@ -1167,4 +1167,33 @@ class Application extends BaseModelreturn $preview;}++ public function getMetrics(int $mins = 5)+ {+ $server = $this->destination->server;+ $container_name = generateApplicationContainerName($this);+ if ($server->isMetricsEnabled()) {+ $from = now()->subMinutes($mins)->toIso8601ZuluString();+ $metrics = instant_remote_process(["docker exec coolify-sentinel sh -c 'curl -H \"Authorization: Bearer {$server->settings->metrics_token}\" http://localhost:8888/api/container/{$container_name}/metrics/history?from=$from'"], $server, false);+ if (str($metrics)->contains('error')) {+ $error = json_decode($metrics, true);+ $error = data_get($error, 'error', 'Something is not okay, are you okay?');+ if ($error == 'Unauthorized') {+ $error = 'Unauthorized, please check your metrics token or restart Sentinel to set a new token.';+ }+ throw new \Exception($error);+ }+ $metrics = str($metrics)->explode("\n")->skip(1)->all();+ $parsedCollection = collect($metrics)->flatMap(function ($item) {+ return collect(explode("\n", trim($item)))->map(function ($line) {+ [$time, $cpu_usage_percent, $memory_usage, $memory_usage_percent] = explode(',', trim($line));+ $cpu_usage_percent = number_format($cpu_usage_percent, 2);++ return [(int) $time, (float) $cpu_usage_percent, (int) $memory_usage];+ });+ });++ return $parsedCollection->toArray();+ }+ }}diff --git a/app/Models/Server.php b/app/Models/Server.phpindex 3d40042bb8..7a99940fd3 100644--- a/app/Models/Server.php+++ b/app/Models/Server.php@@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Builder;use Illuminate\Database\Eloquent\Casts\Attribute;use Illuminate\Support\Collection;use Illuminate\Support\Facades\DB;+use Illuminate\Support\Facades\Process;use Illuminate\Support\Facades\Storage;use Illuminate\Support\Str;use Illuminate\Support\Stringable;@@ -460,15 +461,44 @@ $schema://$host {Storage::disk('ssh-mux')->delete($this->muxFilename());}+ public function isSentinelEnabled()+ {+ return $this->isMetricsEnabled() || $this->isServerApiEnabled();+ }+public function isMetricsEnabled(){return $this->settings->is_metrics_enabled;}+ public function isServerApiEnabled()+ {+ return $this->settings->is_server_api_enabled;+ }++ public function checkServerApi()+ {+ if ($this->isServerApiEnabled()) {+ $server_ip = $this->ip;+ if (isDev()) {+ if ($this->id === 0) {+ $server_ip = 'localhost';+ }+ }+ $command = "curl -s http://{$server_ip}:12172/api/health";+ $process = Process::timeout(5)->run($command);+ if ($process->failed()) {+ ray($process->exitCode(), $process->output(), $process->errorOutput());+ throw new \Exception("Server API is not reachable on http://{$server_ip}:12172");+ }++ }+ }+public function checkSentinel(){ray("Checking sentinel on server: {$this->name}");- if ($this->isMetricsEnabled()) {+ if ($this->isSentinelEnabled()) {$sentinel_found = instant_remote_process(['docker inspect coolify-sentinel'], $this, false);$sentinel_found = json_decode($sentinel_found, true);$status = data_get($sentinel_found, '0.State.Status', 'exited');@@ -497,10 +527,10 @@ $schema://$host {$cpu = str($cpu)->explode("\n")->skip(1)->all();$parsedCollection = collect($cpu)->flatMap(function ($item) {return collect(explode("\n", trim($item)))->map(function ($line) {- [$time, $value] = explode(',', trim($line));- $value = number_format($value, 0);+ [$time, $cpu_usage_percent] = explode(',', trim($line));+ $cpu_usage_percent = number_format($cpu_usage_percent, 0);- return [(int) $time, (float) $value];+ return [(int) $time, (float) $cpu_usage_percent];});});diff --git a/app/Models/StandaloneClickhouse.php b/app/Models/StandaloneClickhouse.phpindex c5e252c34a..e968db18d7 100644--- a/app/Models/StandaloneClickhouse.php+++ b/app/Models/StandaloneClickhouse.php@@ -226,4 +226,33 @@ class StandaloneClickhouse extends BaseModel{return $this->morphMany(ScheduledDatabaseBackup::class, 'database');}++ public function getMetrics(int $mins = 5)+ {+ $server = $this->destination->server;+ $container_name = $this->uuid;+ if ($server->isMetricsEnabled()) {+ $from = now()->subMinutes($mins)->toIso8601ZuluString();+ $metrics = instant_remote_process(["docker exec coolify-sentinel sh -c 'curl -H \"Authorization: Bearer {$server->settings->metrics_token}\" http://localhost:8888/api/container/{$container_name}/metrics/history?from=$from'"], $server, false);+ if (str($metrics)->contains('error')) {+ $error = json_decode($metrics, true);+ $error = data_get($error, 'error', 'Something is not okay, are you okay?');+ if ($error == 'Unauthorized') {+ $error = 'Unauthorized, please check your metrics token or restart Sentinel to set a new token.';+ }+ throw new \Exception($error);+ }+ $metrics = str($metrics)->explode("\n")->skip(1)->all();+ $parsedCollection = collect($metrics)->flatMap(function ($item) {+ return collect(explode("\n", trim($item)))->map(function ($line) {+ [$time, $cpu_usage_percent, $memory_usage, $memory_usage_percent] = explode(',', trim($line));+ $cpu_usage_percent = number_format($cpu_usage_percent, 2);++ return [(int) $time, (float) $cpu_usage_percent, (int) $memory_usage];+ });+ });++ return $parsedCollection->toArray();+ }+ }}diff --git a/app/Models/StandaloneDragonfly.php b/app/Models/StandaloneDragonfly.phpindex 8c739d9849..c6718acfe8 100644--- a/app/Models/StandaloneDragonfly.php+++ b/app/Models/StandaloneDragonfly.php@@ -226,4 +226,33 @@ class StandaloneDragonfly extends BaseModel{return $this->morphMany(ScheduledDatabaseBackup::class, 'database');}++ public function getMetrics(int $mins = 5)+ {+ $server = $this->destination->server;+ $container_name = $this->uuid;+ if ($server->isMetricsEnabled()) {+ $from = now()->subMinutes($mins)->toIso8601ZuluString();+ $metrics = instant_remote_process(["docker exec coolify-sentinel sh -c 'curl -H \"Authorization: Bearer {$server->settings->metrics_token}\" http://localhost:8888/api/container/{$container_name}/metrics/history?from=$from'"], $server, false);+ if (str($metrics)->contains('error')) {+ $error = json_decode($metrics, true);+ $error = data_get($error, 'error', 'Something is not okay, are you okay?');+ if ($error == 'Unauthorized') {+ $error = 'Unauthorized, please check your metrics token or restart Sentinel to set a new token.';+ }+ throw new \Exception($error);+ }+ $metrics = str($metrics)->explode("\n")->skip(1)->all();+ $parsedCollection = collect($metrics)->flatMap(function ($item) {+ return collect(explode("\n", trim($item)))->map(function ($line) {+ [$time, $cpu_usage_percent, $memory_usage, $memory_usage_percent] = explode(',', trim($line));+ $cpu_usage_percent = number_format($cpu_usage_percent, 2);++ return [(int) $time, (float) $cpu_usage_percent, (int) $memory_usage];+ });+ });++ return $parsedCollection->toArray();+ }+ }}diff --git a/app/Models/StandaloneKeydb.php b/app/Models/StandaloneKeydb.phpindex 5216681c9a..142f960aab 100644--- a/app/Models/StandaloneKeydb.php+++ b/app/Models/StandaloneKeydb.php@@ -226,4 +226,33 @@ class StandaloneKeydb extends BaseModel{return $this->morphMany(ScheduledDatabaseBackup::class, 'database');}++ public function getMetrics(int $mins = 5)+ {+ $server = $this->destination->server;+ $container_name = $this->uuid;+ if ($server->isMetricsEnabled()) {+ $from = now()->subMinutes($mins)->toIso8601ZuluString();+ $metrics = instant_remote_process(["docker exec coolify-sentinel sh -c 'curl -H \"Authorization: Bearer {$server->settings->metrics_token}\" http://localhost:8888/api/container/{$container_name}/metrics/history?from=$from'"], $server, false);+ if (str($metrics)->contains('error')) {+ $error = json_decode($metrics, true);+ $error = data_get($error, 'error', 'Something is not okay, are you okay?');+ if ($error == 'Unauthorized') {+ $error = 'Unauthorized, please check your metrics token or restart Sentinel to set a new token.';+ }+ throw new \Exception($error);+ }+ $metrics = str($metrics)->explode("\n")->skip(1)->all();+ $parsedCollection = collect($metrics)->flatMap(function ($item) {+ return collect(explode("\n", trim($item)))->map(function ($line) {+ [$time, $cpu_usage_percent, $memory_usage, $memory_usage_percent] = explode(',', trim($line));+ $cpu_usage_percent = number_format($cpu_usage_percent, 2);++ return [(int) $time, (float) $cpu_usage_percent, (int) $memory_usage];+ });+ });++ return $parsedCollection->toArray();+ }+ }}diff --git a/app/Models/StandaloneMariadb.php b/app/Models/StandaloneMariadb.phpindex 33fd2cbc27..7e6d2e0d1e 100644--- a/app/Models/StandaloneMariadb.php+++ b/app/Models/StandaloneMariadb.php@@ -226,4 +226,33 @@ class StandaloneMariadb extends BaseModel{return $this->morphMany(ScheduledDatabaseBackup::class, 'database');}++ public function getMetrics(int $mins = 5)+ {+ $server = $this->destination->server;+ $container_name = $this->uuid;+ if ($server->isMetricsEnabled()) {+ $from = now()->subMinutes($mins)->toIso8601ZuluString();+ $metrics = instant_remote_process(["docker exec coolify-sentinel sh -c 'curl -H \"Authorization: Bearer {$server->settings->metrics_token}\" http://localhost:8888/api/container/{$container_name}/metrics/history?from=$from'"], $server, false);+ if (str($metrics)->contains('error')) {+ $error = json_decode($metrics, true);+ $error = data_get($error, 'error', 'Something is not okay, are you okay?');+ if ($error == 'Unauthorized') {+ $error = 'Unauthorized, please check your metrics token or restart Sentinel to set a new token.';+ }+ throw new \Exception($error);+ }+ $metrics = str($metrics)->explode("\n")->skip(1)->all();+ $parsedCollection = collect($metrics)->flatMap(function ($item) {+ return collect(explode("\n", trim($item)))->map(function ($line) {+ [$time, $cpu_usage_percent, $memory_usage, $memory_usage_percent] = explode(',', trim($line));+ $cpu_usage_percent = number_format($cpu_usage_percent, 2);++ return [(int) $time, (float) $cpu_usage_percent, (int) $memory_usage];+ });+ });++ return $parsedCollection->toArray();+ }+ }}diff --git a/app/Models/StandaloneMongodb.php b/app/Models/StandaloneMongodb.phpindex 0cc52b3f71..df895bb34f 100644--- a/app/Models/StandaloneMongodb.php+++ b/app/Models/StandaloneMongodb.php@@ -246,4 +246,33 @@ class StandaloneMongodb extends BaseModel{return $this->morphMany(ScheduledDatabaseBackup::class, 'database');}++ public function getMetrics(int $mins = 5)+ {+ $server = $this->destination->server;+ $container_name = $this->uuid;+ if ($server->isMetricsEnabled()) {+ $from = now()->subMinutes($mins)->toIso8601ZuluString();+ $metrics = instant_remote_process(["docker exec coolify-sentinel sh -c 'curl -H \"Authorization: Bearer {$server->settings->metrics_token}\" http://localhost:8888/api/container/{$container_name}/metrics/history?from=$from'"], $server, false);+ if (str($metrics)->contains('error')) {+ $error = json_decode($metrics, true);+ $error = data_get($error, 'error', 'Something is not okay, are you okay?');+ if ($error == 'Unauthorized') {+ $error = 'Unauthorized, please check your metrics token or restart Sentinel to set a new token.';+ }+ throw new \Exception($error);+ }+ $metrics = str($metrics)->explode("\n")->skip(1)->all();+ $parsedCollection = collect($metrics)->flatMap(function ($item) {+ return collect(explode("\n", trim($item)))->map(function ($line) {+ [$time, $cpu_usage_percent, $memory_usage, $memory_usage_percent] = explode(',', trim($line));+ $cpu_usage_percent = number_format($cpu_usage_percent, 2);++ return [(int) $time, (float) $cpu_usage_percent, (int) $memory_usage];+ });+ });++ return $parsedCollection->toArray();+ }+ }}diff --git a/app/Models/StandaloneMysql.php b/app/Models/StandaloneMysql.phpindex 174736f779..bd160f8772 100644--- a/app/Models/StandaloneMysql.php+++ b/app/Models/StandaloneMysql.php@@ -227,4 +227,33 @@ class StandaloneMysql extends BaseModel{return $this->morphMany(ScheduledDatabaseBackup::class, 'database');}++ public function getMetrics(int $mins = 5)+ {+ $server = $this->destination->server;+ $container_name = $this->uuid;+ if ($server->isMetricsEnabled()) {+ $from = now()->subMinutes($mins)->toIso8601ZuluString();+ $metrics = instant_remote_process(["docker exec coolify-sentinel sh -c 'curl -H \"Authorization: Bearer {$server->settings->metrics_token}\" http://localhost:8888/api/container/{$container_name}/metrics/history?from=$from'"], $server, false);+ if (str($metrics)->contains('error')) {+ $error = json_decode($metrics, true);+ $error = data_get($error, 'error', 'Something is not okay, are you okay?');+ if ($error == 'Unauthorized') {+ $error = 'Unauthorized, please check your metrics token or restart Sentinel to set a new token.';+ }+ throw new \Exception($error);+ }+ $metrics = str($metrics)->explode("\n")->skip(1)->all();+ $parsedCollection = collect($metrics)->flatMap(function ($item) {+ return collect(explode("\n", trim($item)))->map(function ($line) {+ [$time, $cpu_usage_percent, $memory_usage, $memory_usage_percent] = explode(',', trim($line));+ $cpu_usage_percent = number_format($cpu_usage_percent, 2);++ return [(int) $time, (float) $cpu_usage_percent, (int) $memory_usage];+ });+ });++ return $parsedCollection->toArray();+ }+ }}diff --git a/app/Models/StandalonePostgresql.php b/app/Models/StandalonePostgresql.phpindex a5bf4dc2a7..114d376e89 100644--- a/app/Models/StandalonePostgresql.php+++ b/app/Models/StandalonePostgresql.php@@ -227,4 +227,33 @@ class StandalonePostgresql extends BaseModel{return $this->morphMany(ScheduledDatabaseBackup::class, 'database');}++ public function getMetrics(int $mins = 5)+ {+ $server = $this->destination->server;+ $container_name = $this->uuid;+ if ($server->isMetricsEnabled()) {+ $from = now()->subMinutes($mins)->toIso8601ZuluString();+ $metrics = instant_remote_process(["docker exec coolify-sentinel sh -c 'curl -H \"Authorization: Bearer {$server->settings->metrics_token}\" http://localhost:8888/api/container/{$container_name}/metrics/history?from=$from'"], $server, false);+ if (str($metrics)->contains('error')) {+ $error = json_decode($metrics, true);+ $error = data_get($error, 'error', 'Something is not okay, are you okay?');+ if ($error == 'Unauthorized') {+ $error = 'Unauthorized, please check your metrics token or restart Sentinel to set a new token.';+ }+ throw new \Exception($error);+ }+ $metrics = str($metrics)->explode("\n")->skip(1)->all();+ $parsedCollection = collect($metrics)->flatMap(function ($item) {+ return collect(explode("\n", trim($item)))->map(function ($line) {+ [$time, $cpu_usage_percent, $memory_usage, $memory_usage_percent] = explode(',', trim($line));+ $cpu_usage_percent = number_format($cpu_usage_percent, 2);++ return [(int) $time, (float) $cpu_usage_percent, (int) $memory_usage];+ });+ });++ return $parsedCollection->toArray();+ }+ }}diff --git a/app/Models/StandaloneRedis.php b/app/Models/StandaloneRedis.phpindex ed379750e6..022cd8d090 100644--- a/app/Models/StandaloneRedis.php+++ b/app/Models/StandaloneRedis.php@@ -222,4 +222,33 @@ class StandaloneRedis extends BaseModel{return $this->morphMany(ScheduledDatabaseBackup::class, 'database');}++ public function getMetrics(int $mins = 5)+ {+ $server = $this->destination->server;+ $container_name = $this->uuid;+ if ($server->isMetricsEnabled()) {+ $from = now()->subMinutes($mins)->toIso8601ZuluString();+ $metrics = instant_remote_process(["docker exec coolify-sentinel sh -c 'curl -H \"Authorization: Bearer {$server->settings->metrics_token}\" http://localhost:8888/api/container/{$container_name}/metrics/history?from=$from'"], $server, false);+ if (str($metrics)->contains('error')) {+ $error = json_decode($metrics, true);+ $error = data_get($error, 'error', 'Something is not okay, are you okay?');+ if ($error == 'Unauthorized') {+ $error = 'Unauthorized, please check your metrics token or restart Sentinel to set a new token.';+ }+ throw new \Exception($error);+ }+ $metrics = str($metrics)->explode("\n")->skip(1)->all();+ $parsedCollection = collect($metrics)->flatMap(function ($item) {+ return collect(explode("\n", trim($item)))->map(function ($line) {+ [$time, $cpu_usage_percent, $memory_usage, $memory_usage_percent] = explode(',', trim($line));+ $cpu_usage_percent = number_format($cpu_usage_percent, 2);++ return [(int) $time, (float) $cpu_usage_percent, (int) $memory_usage];+ });+ });++ return $parsedCollection->toArray();+ }+ }}diff --git a/bootstrap/helpers/shared.php b/bootstrap/helpers/shared.phpindex 2feda420fc..b9dc685b7e 100644--- a/bootstrap/helpers/shared.php+++ b/bootstrap/helpers/shared.php@@ -161,9 +161,6 @@ function get_route_parameters(): arrayfunction get_latest_sentinel_version(): string{- if (isDev()) {- return '0.0.8';- }try {$response = Http::get('https://cdn.coollabs.io/sentinel/versions.json');$versions = $response->json();diff --git a/database/migrations/2024_06_20_102551_add_server_api_sentinel.php b/database/migrations/2024_06_20_102551_add_server_api_sentinel.phpnew file mode 100644index 0000000000..b840195af6--- /dev/null+++ b/database/migrations/2024_06_20_102551_add_server_api_sentinel.php@@ -0,0 +1,28 @@+<?php++use Illuminate\Database\Migrations\Migration;+use Illuminate\Database\Schema\Blueprint;+use Illuminate\Support\Facades\Schema;++return new class extends Migration+{+ /**+ * Run the migrations.+ */+ public function up(): void+ {+ Schema::table('server_settings', function (Blueprint $table) {+ $table->boolean('is_server_api_enabled')->default(false);+ });+ }++ /**+ * Reverse the migrations.+ */+ public function down(): void+ {+ Schema::table('server_settings', function (Blueprint $table) {+ $table->dropColumn('is_server_api_enabled');+ });+ }+};diff --git a/resources/views/layouts/base.blade.php b/resources/views/layouts/base.blade.phpindex c1851e2f90..1b2628db10 100644--- a/resources/views/layouts/base.blade.php+++ b/resources/views/layouts/base.blade.php@@ -60,6 +60,20 @@document.documentElement.classList.remove('dark')}}+ let theme = localStorage.theme+ let baseColor = '#FCD452'+ let textColor = '#ffffff'++ function checkTheme() {+ theme = localStorage.theme+ if (theme == 'dark') {+ baseColor = '#FCD452'+ textColor = '#ffffff'+ } else {+ baseColor = 'black'+ textColor = '#000000'+ }+ }@authwindow.Pusher = Pusher;window.Echo = new Echo({diff --git a/resources/views/livewire/charts/server-cpu.blade.php b/resources/views/livewire/charts/server-cpu.blade.phpindex fe7131ade9..3aab224f94 100644--- a/resources/views/livewire/charts/server-cpu.blade.php+++ b/resources/views/livewire/charts/server-cpu.blade.php@@ -1,5 +1,5 @@<div @if ($poll) wire:poll.5000ms='pollData' @endif x-init="$wire.loadData()">- <h3>CPU</h3>+ <h3>CPU (%)</h3><x-forms.select label="Interval" wire:change="setInterval" id="interval"><option value="5">5 minutes (live)</option><option value="10">10 minutes (live)</option>diff --git a/resources/views/livewire/charts/server-memory.blade.php b/resources/views/livewire/charts/server-memory.blade.phpindex 737526b0de..851b9b9758 100644--- a/resources/views/livewire/charts/server-memory.blade.php+++ b/resources/views/livewire/charts/server-memory.blade.php@@ -1,5 +1,5 @@<div @if ($poll) wire:poll.5000ms='pollData' @endif x-init="$wire.loadData()">- <h3>Memory</h3>+ <h3>Memory (MB)</h3><x-forms.select label="Interval" wire:change="setInterval" id="interval"><option value="5">5 minutes (live)</option><option value="10">10 minutes (live)</option>diff --git a/resources/views/livewire/project/application/configuration.blade.php b/resources/views/livewire/project/application/configuration.blade.phpindex 4be672063c..639776730d 100644--- a/resources/views/livewire/project/application/configuration.blade.php+++ b/resources/views/livewire/project/application/configuration.blade.php@@ -74,6 +74,9 @@@click.prevent="activeTab = 'resource-operations'; window.location.hash = 'resource-operations'"href="#">Resource Operations</a>+ <a class="menu-item" :class="activeTab === 'metrics' && 'menu-item-active'"+ @click.prevent="activeTab = 'metrics'; window.location.hash = 'metrics'" href="#">Metrics+ </a><a class="menu-item" :class="activeTab === 'tags' && 'menu-item-active'"@click.prevent="activeTab = 'tags'; window.location.hash = 'tags'" href="#">Tags</a>@@ -126,6 +129,9 @@<div x-cloak x-show="activeTab === 'resource-operations'"><livewire:project.shared.resource-operations :resource="$application" /></div>+ <div x-cloak x-show="activeTab === 'metrics'">+ <livewire:project.shared.metrics :resource="$application" />+ </div><div x-cloak x-show="activeTab === 'tags'"><livewire:project.shared.tags :resource="$application" /></div>diff --git a/resources/views/livewire/project/database/configuration.blade.php b/resources/views/livewire/project/database/configuration.blade.phpindex 809a48c0f4..aa5b8ec5e1 100644--- a/resources/views/livewire/project/database/configuration.blade.php+++ b/resources/views/livewire/project/database/configuration.blade.php@@ -42,6 +42,9 @@@click.prevent="activeTab = 'resource-operations'; window.location.hash = 'resource-operations'"href="#">Resource Operations</a>+ <a class="menu-item" :class="activeTab === 'metrics' && 'menu-item-active'"+ @click.prevent="activeTab = 'metrics'; window.location.hash = 'metrics'" href="#">Metrics+ </a><a class="menu-item" :class="activeTab === 'tags' && 'menu-item-active'"@click.prevent="activeTab = 'tags'; window.location.hash = 'tags'" href="#">Tags</a>@@ -92,6 +95,9 @@<div x-cloak x-show="activeTab === 'resource-operations'"><livewire:project.shared.resource-operations :resource="$database" /></div>+ <div x-cloak x-show="activeTab === 'metrics'">+ <livewire:project.shared.metrics :resource="$database" />+ </div><div x-cloak x-show="activeTab === 'tags'"><livewire:project.shared.tags :resource="$database" /></div>diff --git a/resources/views/livewire/project/shared/metrics.blade.php b/resources/views/livewire/project/shared/metrics.blade.phpnew file mode 100644index 0000000000..f27c71bf5b--- /dev/null+++ b/resources/views/livewire/project/shared/metrics.blade.php@@ -0,0 +1,243 @@+<div>+ <div class="flex items-center gap-2 ">+ <h2>Metrics</h2>+ </div>+ <div class="pb-4">Basic metrics for your container.</div>+ @if ($resource->getMorphClass() === 'App\Models\Application' && $resource->build_pack === 'dockercompose')+ <div class="alert alert-warning">Metrics are not available for Docker Compose applications yet!</div>+ @else+ @if (!str($resource->status)->contains('running'))+ <div class="alert alert-warning">Metrics are only available when the application is running!</div>+ @else+ <x-forms.select label="Interval" wire:change="setInterval" id="interval">+ <option value="5">5 minutes (live)</option>+ <option value="10">10 minutes (live)</option>+ <option value="30">30 minutes</option>+ <option value="60">1 hour</option>+ <option value="720">12 hours</option>+ <option value="10080">1 week</option>+ <option value="43200">30 days</option>+ </x-forms.select>+ <div @if ($poll) wire:poll.5000ms='pollData' @endif x-init="$wire.loadData()"+ class="pt-5">+ <h4>CPU (%)</h4>+ <div wire:ignore id="{!! $chartId !!}-cpu"></div>++ <script>+ checkTheme();+ const optionsServerCpu = {+ stroke: {+ curve: 'straight',+ },+ chart: {+ height: '150px',+ id: '{!! $chartId !!}-cpu',+ type: 'area',+ toolbar: {+ show: false,+ tools: {+ download: true,+ selection: false,+ zoom: false,+ zoomin: false,+ zoomout: false,+ pan: false,+ reset: false+ },+ },+ animations: {+ enabled: false,+ },+ },+ fill: {+ type: 'gradient',+ },+ dataLabels: {+ enabled: false,+ offsetY: -10,+ style: {+ colors: ['#FCD452'],+ },+ background: {+ enabled: false,+ }+ },+ grid: {+ show: true,+ borderColor: '',+ },+ colors: [baseColor],+ xaxis: {+ type: 'datetime',+ },+ series: [{+ data: []+ }],+ noData: {+ text: 'Loading...',+ style: {+ color: textColor,+ }+ },+ tooltip: {+ enabled: false,+ },+ legend: {+ show: false+ }+ }+ const serverCpuChart = new ApexCharts(document.getElementById(`{!! $chartId !!}-cpu`), optionsServerCpu);+ serverCpuChart.render();+ document.addEventListener('livewire:init', () => {+ Livewire.on('refreshChartData-{!! $chartId !!}-cpu', (chartData) => {+ checkTheme();+ serverCpuChart.updateOptions({+ series: [{+ data: chartData[0].seriesData,+ }],+ colors: [baseColor],+ xaxis: {+ type: 'datetime',+ labels: {+ show: true,+ style: {+ colors: textColor,+ }+ }+ },+ yaxis: {+ show: true,+ labels: {+ show: true,+ style: {+ colors: textColor,+ }+ }+ },+ noData: {+ text: 'Loading...',+ style: {+ color: textColor,+ }+ }+ });+ });+ });+ </script>++ <h3>Memory (MB)</h3>+ <div wire:ignore id="{!! $chartId !!}-memory"></div>++ <script>+ checkTheme();+ const optionsServerMemory = {+ stroke: {+ curve: 'straight',+ },+ chart: {+ height: '150px',+ id: '{!! $chartId !!}-memory',+ type: 'area',+ toolbar: {+ show: false,+ tools: {+ download: true,+ selection: false,+ zoom: false,+ zoomin: false,+ zoomout: false,+ pan: false,+ reset: false+ },+ },+ animations: {+ enabled: false,+ },+ },+ fill: {+ type: 'gradient',+ },+ dataLabels: {+ enabled: false,+ offsetY: -10,+ style: {+ colors: ['#FCD452'],+ },+ background: {+ enabled: false,+ }+ },+ grid: {+ show: true,+ borderColor: '',+ },+ colors: [baseColor],+ xaxis: {+ type: 'datetime',+ labels: {+ show: true,+ style: {+ colors: textColor,+ }+ }+ },+ series: [{+ data: []+ }],+ noData: {+ text: 'Loading...',+ style: {+ color: textColor,+ }+ },+ tooltip: {+ enabled: false,+ },+ legend: {+ show: false+ }+ }+ const serverMemoryChart = new ApexCharts(document.getElementById(`{!! $chartId !!}-memory`),+ optionsServerMemory);+ serverMemoryChart.render();+ document.addEventListener('livewire:init', () => {+ Livewire.on('refreshChartData-{!! $chartId !!}-memory', (chartData) => {+ checkTheme();+ serverMemoryChart.updateOptions({+ series: [{+ data: chartData[0].seriesData,+ }],+ colors: [baseColor],+ xaxis: {+ type: 'datetime',+ labels: {+ show: true,+ style: {+ colors: textColor,+ }+ }+ },+ yaxis: {+ min: 0,+ show: true,+ labels: {+ show: true,+ style: {+ colors: textColor,+ }+ }+ },+ noData: {+ text: 'Loading...',+ style: {+ color: textColor,+ }+ }+ });+ });+ });+ </script>+ </div>+ @endif+ @endif+</div>diff --git a/resources/views/livewire/server/form.blade.php b/resources/views/livewire/server/form.blade.phpindex 9f061ba54c..938e47cbdf 100644--- a/resources/views/livewire/server/form.blade.php+++ b/resources/views/livewire/server/form.blade.php@@ -145,13 +145,16 @@helper="You can define the maximum duration for a deployment to run before timing it out." /></div><div class="flex items-center gap-2">- <h3 class="py-4">Metrics</h3>- @if ($server->isMetricsEnabled())- <x-forms.button wire:click='restartSentinel'>Restart Collector</x-forms.button>+ <h3 class="py-4">Sentinel</h3>+ @if ($server->isSentinelEnabled())+ <x-forms.button wire:click='restartSentinel'>Restart</x-forms.button>@endif</div><div class="w-64">- <x-forms.checkbox instantSave id="server.settings.is_metrics_enabled" label="Enable metrics" />+ <x-forms.checkbox instantSave id="server.settings.is_metrics_enabled" label="Enable Metrics" />+ {{-- <x-forms.checkbox instantSave id="server.settings.is_server_api_enabled" label="Enable Server API"+ helper="You need to open port 12172 on your firewall. This API will be used to gather data from your server, which makes Coolify a lot faster than relying on SSH connections." />+ <x-forms.button wire:click='checkPortForServerApi'>Check Port for Server API</x-forms.button> --}}</div><div class="pt-4"><div class="flex flex-wrap gap-2 sm:flex-nowrap">diff --git a/resources/views/livewire/server/show.blade.php b/resources/views/livewire/server/show.blade.phpindex d3a3bb8c66..0e8a37717b 100644--- a/resources/views/livewire/server/show.blade.php+++ b/resources/views/livewire/server/show.blade.php@@ -7,22 +7,6 @@<livewire:server.delete :server="$server" />@if ($server->isFunctional() && $server->isMetricsEnabled())<div class="pt-10">- <script>- let theme = localStorage.theme- let baseColor = '#FCD452'- let textColor = '#ffffff'-- function checkTheme() {- theme = localStorage.theme- if (theme == 'dark') {- baseColor = '#FCD452'- textColor = '#ffffff'- } else {- baseColor = 'black'- textColor = '#000000'- }- }- </script><livewire:charts.server-cpu :server="$server" /><livewire:charts.server-memory :server="$server" /></div>