1
0
Fork 0
mirror of synced 2024-07-06 23:21:05 +12:00
appwrite/app/tasks/doctor.php

269 lines
10 KiB
PHP
Raw Normal View History

2020-03-01 19:33:19 +13:00
<?php
2020-07-29 07:48:51 +12:00
global $cli;
2020-03-01 19:33:19 +13:00
2020-06-17 23:39:46 +12:00
use Appwrite\ClamAV\Network;
use Utopia\Logger\Logger;
2021-01-22 21:28:33 +13:00
use Utopia\Storage\Device\Local;
use Utopia\Storage\Storage;
2020-06-29 08:45:36 +12:00
use Utopia\App;
2020-03-01 19:33:19 +13:00
use Utopia\CLI\Console;
2022-10-17 06:48:53 +13:00
use Utopia\Config\Config;
2020-06-15 20:01:48 +12:00
use Utopia\Domains\Domain;
2020-03-01 19:33:19 +13:00
2020-06-15 20:01:48 +12:00
$cli
->task('doctor')
->desc('Validate server health')
2020-06-29 08:45:36 +12:00
->action(function () use ($register) {
2020-06-15 20:01:48 +12:00
Console::log(" __ ____ ____ _ _ ____ __ ____ ____ __ __
/ _\ ( _ \( _ \/ )( \( _ \( )(_ _)( __) ( )/ \
/ \ ) __/ ) __/\ /\ / ) / )( )( ) _) _ )(( O )
\_/\_/(__) (__) (_/\_)(__\_)(__) (__) (____)(_)(__)\__/ ");
2022-05-24 02:54:50 +12:00
Console::log("\n" . '👩‍⚕️ Running ' . APP_NAME . ' Doctor for version ' . App::getEnv('_APP_VERSION', 'UNKNOWN') . ' ...' . "\n");
2020-06-15 20:01:48 +12:00
2022-10-17 06:48:53 +13:00
Console::log('[Settings]');
2020-06-29 08:45:36 +12:00
$domain = new Domain(App::getEnv('_APP_DOMAIN'));
2020-06-15 20:01:48 +12:00
2022-05-24 02:54:50 +12:00
if (!$domain->isKnown() || $domain->isTest()) {
Console::log('🔴 Hostname has no public suffix (' . $domain->get() . ')');
} else {
Console::log('🟢 Hostname has a public suffix (' . $domain->get() . ')');
2020-06-15 20:01:48 +12:00
}
2020-06-29 08:45:36 +12:00
$domain = new Domain(App::getEnv('_APP_DOMAIN_TARGET'));
2020-06-15 20:01:48 +12:00
2022-05-24 02:54:50 +12:00
if (!$domain->isKnown() || $domain->isTest()) {
Console::log('🔴 CNAME target has no public suffix (' . $domain->get() . ')');
} else {
Console::log('🟢 CNAME target has a public suffix (' . $domain->get() . ')');
2020-06-15 20:01:48 +12:00
}
2022-05-24 02:54:50 +12:00
if (App::getEnv('_APP_OPENSSL_KEY_V1') === 'your-secret-key' || empty(App::getEnv('_APP_OPENSSL_KEY_V1'))) {
Console::log('🔴 Not using a unique secret key for encryption');
2022-05-24 02:54:50 +12:00
} else {
2020-06-17 23:39:46 +12:00
Console::log('🟢 Using a unique secret key for encryption');
2020-06-15 20:01:48 +12:00
}
2022-05-24 02:54:50 +12:00
if (App::getEnv('_APP_ENV', 'development') !== 'production') {
Console::log('🔴 App environment is set for development');
2022-05-24 02:54:50 +12:00
} else {
Console::log('🟢 App environment is set for production');
2020-06-15 20:01:48 +12:00
}
2022-05-24 02:54:50 +12:00
if ('enabled' !== App::getEnv('_APP_OPTIONS_ABUSE', 'disabled')) {
Console::log('🔴 Abuse protection is disabled');
2022-05-24 02:54:50 +12:00
} else {
2020-06-17 23:39:46 +12:00
Console::log('🟢 Abuse protection is enabled');
}
2021-05-13 02:53:25 +12:00
$authWhitelistRoot = App::getEnv('_APP_CONSOLE_WHITELIST_ROOT', null);
2020-06-29 08:45:36 +12:00
$authWhitelistEmails = App::getEnv('_APP_CONSOLE_WHITELIST_EMAILS', null);
$authWhitelistIPs = App::getEnv('_APP_CONSOLE_WHITELIST_IPS', null);
2020-06-17 23:39:46 +12:00
2022-05-24 02:54:50 +12:00
if (
empty($authWhitelistRoot)
2021-02-24 00:29:12 +13:00
&& empty($authWhitelistEmails)
2020-06-17 23:39:46 +12:00
&& empty($authWhitelistIPs)
) {
Console::log('🔴 Console access limits are disabled');
2022-05-24 02:54:50 +12:00
} else {
2020-06-17 23:39:46 +12:00
Console::log('🟢 Console access limits are enabled');
}
2022-05-24 02:54:50 +12:00
if ('enabled' !== App::getEnv('_APP_OPTIONS_FORCE_HTTPS', 'disabled')) {
Console::log('🔴 HTTPS force option is disabled');
2022-05-24 02:54:50 +12:00
} else {
Console::log('🟢 HTTPS force option is enabled');
2020-06-16 01:54:19 +12:00
}
$providerName = App::getEnv('_APP_LOGGING_PROVIDER', '');
$providerConfig = App::getEnv('_APP_LOGGING_CONFIG', '');
2022-05-24 02:54:50 +12:00
if (empty($providerName) || empty($providerConfig) || !Logger::hasProvider($providerName)) {
Console::log('🔴 Logging adapter is disabled');
} else {
Console::log('🟢 Logging adapter is enabled (' . $providerName . ')');
}
2020-06-20 23:20:49 +12:00
\sleep(0.2);
2020-06-15 20:01:48 +12:00
try {
2022-10-17 06:48:53 +13:00
Console::log("\n" . '[Connectivity]');
2020-06-15 20:01:48 +12:00
} catch (\Throwable $th) {
//throw $th;
}
2022-10-17 06:48:53 +13:00
$pools = $register->get('pools'); /** @var \Utopia\Pools\Group $pools */
2022-10-17 11:49:53 +13:00
2022-10-17 06:48:53 +13:00
$configs = [
'Console.DB' => Config::getParam('pools-console'),
'Projects.DB' => Config::getParam('pools-database'),
];
foreach ($configs as $key => $config) {
foreach ($config as $database) {
try {
2022-10-17 11:49:53 +13:00
$adapter = $pools->get($database)->pop()->getResource();
2022-10-19 21:35:30 +13:00
if ($adapter->ping()) {
Console::success('🟢 ' . str_pad("{$key}({$database})", 50, '.') . 'connected');
2022-10-17 06:48:53 +13:00
} else {
2022-10-19 21:35:30 +13:00
Console::error('🔴 ' . str_pad("{$key}({$database})", 47, '.') . 'disconnected');
2022-10-17 06:48:53 +13:00
}
} catch (\Throwable $th) {
2022-10-19 21:35:30 +13:00
Console::error('🔴 ' . str_pad("{$key}.({$database})", 47, '.') . 'disconnected');
2022-10-17 06:48:53 +13:00
}
}
2020-06-15 20:01:48 +12:00
}
2022-10-17 06:48:53 +13:00
$pools = $register->get('pools'); /** @var \Utopia\Pools\Group $pools */
$configs = [
'Cache' => Config::getParam('pools-cache'),
'Queue' => Config::getParam('pools-queue'),
'PubSub' => Config::getParam('pools-pubsub'),
];
foreach ($configs as $key => $config) {
foreach ($config as $pool) {
try {
2022-10-17 11:49:53 +13:00
$adapter = $pools->get($pool)->pop()->getResource();
2022-10-19 21:35:30 +13:00
if ($adapter->ping()) {
Console::success('🟢 ' . str_pad("{$key}({$pool})", 50, '.') . 'connected');
2022-10-17 06:48:53 +13:00
} else {
2022-10-19 21:35:30 +13:00
Console::error('🔴 ' . str_pad("{$key}({$pool})", 47, '.') . 'disconnected');
2022-10-17 06:48:53 +13:00
}
} catch (\Throwable $th) {
2022-10-19 21:35:30 +13:00
Console::error('🔴 ' . str_pad("{$key}({$pool})", 47, '.') . 'disconnected');
2022-10-17 06:48:53 +13:00
}
}
2020-06-15 20:01:48 +12:00
}
2022-05-24 02:54:50 +12:00
if (App::getEnv('_APP_STORAGE_ANTIVIRUS') === 'enabled') { // Check if scans are enabled
try {
2022-05-24 02:54:50 +12:00
$antivirus = new Network(
App::getEnv('_APP_STORAGE_ANTIVIRUS_HOST', 'clamav'),
(int) App::getEnv('_APP_STORAGE_ANTIVIRUS_PORT', 3310)
);
2022-05-24 02:54:50 +12:00
if ((@$antivirus->ping())) {
2022-10-19 21:35:30 +13:00
Console::success('🟢 ' . str_pad("Antivirus", 50, '.') . 'connected');
2022-05-24 02:54:50 +12:00
} else {
2022-10-19 21:35:30 +13:00
Console::error('🔴 ' . str_pad("Antivirus", 47, '.') . 'disconnected');
}
} catch (\Throwable $th) {
2022-10-19 21:35:30 +13:00
Console::error('🔴 ' . str_pad("Antivirus", 47, '.') . 'disconnected');
2020-06-17 23:39:46 +12:00
}
}
2020-06-15 20:01:48 +12:00
try {
$mail = $register->get('smtp'); /* @var $mail \PHPMailer\PHPMailer\PHPMailer */
$mail->addAddress('demo@example.com', 'Example.com');
$mail->Subject = 'Test SMTP Connection';
$mail->Body = 'Hello World';
$mail->AltBody = 'Hello World';
2020-06-15 20:01:48 +12:00
$mail->send();
2022-10-19 21:35:30 +13:00
Console::success('🟢 ' . str_pad("SMTP", 50, '.') . 'connected');
2020-06-15 20:01:48 +12:00
} catch (\Throwable $th) {
2022-10-19 21:35:30 +13:00
Console::error('🔴 ' . str_pad("SMTP", 47, '.') . 'disconnected');
2020-06-15 20:01:48 +12:00
}
2020-06-29 08:45:36 +12:00
$host = App::getEnv('_APP_STATSD_HOST', 'telegraf');
$port = App::getEnv('_APP_STATSD_PORT', 8125);
2020-06-15 20:01:48 +12:00
2022-05-24 02:54:50 +12:00
if ($fp = @\fsockopen('udp://' . $host, $port, $errCode, $errStr, 2)) {
2022-10-19 21:35:30 +13:00
Console::success('🟢 ' . str_pad("StatsD", 50, '.') . 'connected');
2020-06-20 23:20:49 +12:00
\fclose($fp);
2020-06-15 20:01:48 +12:00
} else {
2022-10-19 21:35:30 +13:00
Console::error('🔴 ' . str_pad("StatsD", 47, '.') . 'disconnected');
2020-06-15 20:01:48 +12:00
}
2020-06-29 08:45:36 +12:00
$host = App::getEnv('_APP_INFLUXDB_HOST', '');
$port = App::getEnv('_APP_INFLUXDB_PORT', '');
2020-06-15 20:01:48 +12:00
2022-05-24 02:54:50 +12:00
if ($fp = @\fsockopen($host, $port, $errCode, $errStr, 2)) {
2022-10-19 21:35:30 +13:00
Console::success('🟢 ' . str_pad("InfluxDB", 50, '.') . 'connected');
2020-06-20 23:20:49 +12:00
\fclose($fp);
2020-06-15 20:01:48 +12:00
} else {
2022-10-19 21:35:30 +13:00
Console::error('🔴 ' . str_pad("InfluxDB", 47, '.') . 'disconnected');
2020-06-15 20:01:48 +12:00
}
2020-06-20 23:20:49 +12:00
\sleep(0.2);
2020-06-15 20:01:48 +12:00
Console::log('');
2022-10-17 06:48:53 +13:00
Console::log('[Volumes]');
2020-06-15 20:01:48 +12:00
2022-05-24 02:54:50 +12:00
foreach (
[
2020-06-17 23:18:28 +12:00
'Uploads' => APP_STORAGE_UPLOADS,
'Cache' => APP_STORAGE_CACHE,
'Config' => APP_STORAGE_CONFIG,
'Certs' => APP_STORAGE_CERTIFICATES
2022-05-24 02:54:50 +12:00
] as $key => $volume
) {
2020-06-17 23:18:28 +12:00
$device = new Local($volume);
2020-06-15 20:01:48 +12:00
2020-06-20 23:20:49 +12:00
if (\is_readable($device->getRoot())) {
2022-05-24 02:54:50 +12:00
Console::success('🟢 ' . $key . ' Volume is readable');
} else {
Console::error('🔴 ' . $key . ' Volume is unreadable');
2020-06-17 23:18:28 +12:00
}
2020-06-20 23:20:49 +12:00
if (\is_writable($device->getRoot())) {
2022-05-24 02:54:50 +12:00
Console::success('🟢 ' . $key . ' Volume is writeable');
} else {
Console::error('🔴 ' . $key . ' Volume is unwriteable');
2020-06-17 23:18:28 +12:00
}
2020-06-15 20:01:48 +12:00
}
2020-06-20 23:20:49 +12:00
\sleep(0.2);
2020-06-15 20:01:48 +12:00
2020-06-17 23:18:28 +12:00
Console::log('');
2022-10-17 06:48:53 +13:00
Console::log('[Disk Space]');
2020-06-17 23:18:28 +12:00
2022-05-24 02:54:50 +12:00
foreach (
[
2020-06-17 23:18:28 +12:00
'Uploads' => APP_STORAGE_UPLOADS,
'Cache' => APP_STORAGE_CACHE,
'Config' => APP_STORAGE_CONFIG,
'Certs' => APP_STORAGE_CERTIFICATES
2022-05-24 02:54:50 +12:00
] as $key => $volume
) {
2020-06-17 23:18:28 +12:00
$device = new Local($volume);
$percentage = (($device->getPartitionTotalSpace() - $device->getPartitionFreeSpace())
/ $device->getPartitionTotalSpace()) * 100;
2022-05-24 02:54:50 +12:00
$message = $key . ' Volume has ' . Storage::human($device->getPartitionFreeSpace()) . ' free space (' . \round($percentage, 2) . '% used)';
2020-06-17 23:18:28 +12:00
if ($percentage < 80) {
Console::success('🟢 ' . $message);
2022-05-24 02:54:50 +12:00
} else {
2020-06-17 23:18:28 +12:00
Console::error('🔴 ' . $message);
}
2020-06-15 20:01:48 +12:00
}
2020-06-15 20:01:48 +12:00
try {
2022-05-24 02:54:50 +12:00
if (App::isProduction()) {
Console::log('');
2022-05-24 02:54:50 +12:00
$version = \json_decode(@\file_get_contents(App::getEnv('_APP_HOME', 'http://localhost') . '/v1/health/version'), true);
if ($version && isset($version['version'])) {
2022-05-24 02:54:50 +12:00
if (\version_compare($version['version'], App::getEnv('_APP_VERSION', 'UNKNOWN')) === 0) {
Console::info('You are running the latest version of ' . APP_NAME . '! 🥳');
} else {
Console::info('A new version (' . $version['version'] . ') is available! 🥳' . "\n");
}
} else {
2022-05-24 02:54:50 +12:00
Console::error('Failed to check for a newer version' . "\n");
2020-06-15 20:01:48 +12:00
}
}
} catch (\Throwable $th) {
2022-05-24 02:54:50 +12:00
Console::error('Failed to check for a newer version' . "\n");
2020-06-15 20:01:48 +12:00
}
2020-10-30 09:11:16 +13:00
});