1
0
Fork 0
mirror of synced 2024-05-19 12:12:36 +12:00
appwrite/app/controllers/api/health.php

315 lines
11 KiB
PHP
Raw Normal View History

2019-05-09 18:54:39 +12:00
<?php
2020-06-29 05:31:21 +12:00
use Utopia\App;
2019-05-09 18:54:39 +12:00
use Utopia\Exception;
2021-01-22 21:28:33 +13:00
use Utopia\Storage\Device\Local;
use Utopia\Storage\Storage;
2019-08-21 03:43:01 +12:00
use Appwrite\ClamAV\Network;
2020-12-28 06:57:35 +13:00
use Appwrite\Event\Event;
2019-05-09 18:54:39 +12:00
2020-06-29 05:31:21 +12:00
App::get('/v1/health')
2020-05-17 03:22:30 +12:00
->desc('Get HTTP')
2020-06-26 06:32:12 +12:00
->groups(['api', 'health'])
2020-05-17 03:34:37 +12:00
->label('scope', 'health.read')
2021-04-16 19:22:17 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
2019-05-09 18:54:39 +12:00
->label('sdk.namespace', 'health')
2020-01-31 05:18:46 +13:00
->label('sdk.method', 'get')
2020-05-01 20:39:45 +12:00
->label('sdk.description', '/docs/references/health/get.md')
2020-12-27 04:10:45 +13:00
->inject('response')
2020-06-30 09:43:34 +12:00
->action(function ($response) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2020-06-30 09:43:34 +12:00
$response->json(['status' => 'OK']);
2020-12-27 04:10:45 +13:00
});
2019-05-09 18:54:39 +12:00
2020-06-29 05:31:21 +12:00
App::get('/v1/health/version')
->desc('Get Version')
2020-06-26 06:32:12 +12:00
->groups(['api', 'health'])
->label('scope', 'public')
2020-12-27 04:10:45 +13:00
->inject('response')
2020-06-30 09:43:34 +12:00
->action(function ($response) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2020-06-30 09:43:34 +12:00
$response->json(['version' => APP_VERSION_STABLE]);
2020-12-27 04:10:45 +13:00
});
2020-06-29 05:31:21 +12:00
App::get('/v1/health/db')
2020-05-17 03:22:30 +12:00
->desc('Get DB')
2020-06-26 06:32:12 +12:00
->groups(['api', 'health'])
2020-05-17 03:34:37 +12:00
->label('scope', 'health.read')
2021-04-16 19:22:17 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
2019-05-09 18:54:39 +12:00
->label('sdk.namespace', 'health')
->label('sdk.method', 'getDB')
2020-05-01 20:39:45 +12:00
->label('sdk.description', '/docs/references/health/get-db.md')
2020-12-27 04:10:45 +13:00
->inject('response')
2021-06-28 19:19:33 +12:00
->inject('app')
->action(function ($response, $app) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-06-28 19:19:33 +12:00
/** @var Utopia\App $app */
$app->getResource('db');
2020-06-30 09:43:34 +12:00
$response->json(['status' => 'OK']);
2020-12-27 04:10:45 +13:00
});
2019-05-09 18:54:39 +12:00
2020-06-29 05:31:21 +12:00
App::get('/v1/health/cache')
2020-05-17 03:22:30 +12:00
->desc('Get Cache')
2020-06-26 06:32:12 +12:00
->groups(['api', 'health'])
2020-05-17 03:34:37 +12:00
->label('scope', 'health.read')
2021-04-16 19:22:17 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
2019-05-09 18:54:39 +12:00
->label('sdk.namespace', 'health')
->label('sdk.method', 'getCache')
2020-05-01 20:39:45 +12:00
->label('sdk.description', '/docs/references/health/get-cache.md')
2020-12-27 04:10:45 +13:00
->inject('response')
2021-06-28 19:19:33 +12:00
->inject('app')
->action(function ($response, $app) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2021-06-28 19:19:33 +12:00
/** @var Utopia\App $register */
$app->getResource('cache');
2019-05-09 18:54:39 +12:00
2020-06-30 09:43:34 +12:00
$response->json(['status' => 'OK']);
2020-12-27 04:10:45 +13:00
});
2019-05-09 18:54:39 +12:00
2020-06-29 05:31:21 +12:00
App::get('/v1/health/time')
2020-05-17 03:22:30 +12:00
->desc('Get Time')
2020-06-26 06:32:12 +12:00
->groups(['api', 'health'])
2020-05-17 03:34:37 +12:00
->label('scope', 'health.read')
2021-04-16 19:22:17 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
2019-05-09 18:54:39 +12:00
->label('sdk.namespace', 'health')
->label('sdk.method', 'getTime')
2020-05-01 20:39:45 +12:00
->label('sdk.description', '/docs/references/health/get-time.md')
2020-12-27 04:10:45 +13:00
->inject('response')
2020-06-30 09:43:34 +12:00
->action(function ($response) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2019-05-09 18:54:39 +12:00
2020-06-30 09:43:34 +12:00
/*
* Code from: @see https://www.beliefmedia.com.au/query-ntp-time-server
*/
$host = 'time.google.com'; // https://developers.google.com/time/
$gap = 60; // Allow [X] seconds gap
2019-05-09 18:54:39 +12:00
2020-06-30 09:43:34 +12:00
/* Create a socket and connect to NTP server */
$sock = \socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
2019-05-09 18:54:39 +12:00
2020-06-30 09:43:34 +12:00
\socket_connect($sock, $host, 123);
2019-05-09 18:54:39 +12:00
2020-06-30 09:43:34 +12:00
/* Send request */
$msg = "\010".\str_repeat("\0", 47);
2019-05-09 18:54:39 +12:00
2020-06-30 09:43:34 +12:00
\socket_send($sock, $msg, \strlen($msg), 0);
2019-05-09 18:54:39 +12:00
2020-06-30 09:43:34 +12:00
/* Receive response and close socket */
\socket_recv($sock, $recv, 48, MSG_WAITALL);
\socket_close($sock);
2019-05-09 18:54:39 +12:00
2020-06-30 09:43:34 +12:00
/* Interpret response */
$data = \unpack('N12', $recv);
$timestamp = \sprintf('%u', $data[9]);
2019-05-09 18:54:39 +12:00
2020-06-30 09:43:34 +12:00
/* NTP is number of seconds since 0000 UT on 1 January 1900
Unix time is seconds since 0000 UT on 1 January 1970 */
$timestamp -= 2208988800;
2019-05-09 18:54:39 +12:00
2020-06-30 09:43:34 +12:00
$diff = ($timestamp - \time());
2019-05-09 18:54:39 +12:00
2020-06-30 09:43:34 +12:00
if ($diff > $gap || $diff < ($gap * -1)) {
throw new Exception('Server time gaps detected');
2019-05-09 18:54:39 +12:00
}
2020-06-30 09:43:34 +12:00
$response->json(['remote' => $timestamp, 'local' => \time(), 'diff' => $diff]);
2020-12-27 04:10:45 +13:00
});
2019-05-09 18:54:39 +12:00
2020-06-29 05:31:21 +12:00
App::get('/v1/health/queue/webhooks')
2020-05-17 03:22:30 +12:00
->desc('Get Webhooks Queue')
2020-06-26 06:32:12 +12:00
->groups(['api', 'health'])
2020-05-17 03:34:37 +12:00
->label('scope', 'health.read')
2021-04-16 19:22:17 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
2019-05-09 18:54:39 +12:00
->label('sdk.namespace', 'health')
2020-05-01 20:39:45 +12:00
->label('sdk.method', 'getQueueWebhooks')
->label('sdk.description', '/docs/references/health/get-queue-webhooks.md')
2020-12-27 04:10:45 +13:00
->inject('response')
2020-06-30 09:43:34 +12:00
->action(function ($response) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2020-06-30 09:43:34 +12:00
2020-12-28 06:57:35 +13:00
$response->json(['size' => Resque::size(Event::WEBHOOK_QUEUE_NAME)]);
2020-06-30 09:43:34 +12:00
}, ['response']);
2019-05-09 18:54:39 +12:00
2020-06-29 05:31:21 +12:00
App::get('/v1/health/queue/tasks')
2020-05-17 03:22:30 +12:00
->desc('Get Tasks Queue')
2020-06-26 06:32:12 +12:00
->groups(['api', 'health'])
2020-05-17 03:34:37 +12:00
->label('scope', 'health.read')
2021-04-16 19:22:17 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
2020-05-01 20:39:45 +12:00
->label('sdk.namespace', 'health')
->label('sdk.method', 'getQueueTasks')
->label('sdk.description', '/docs/references/health/get-queue-tasks.md')
2020-12-27 04:10:45 +13:00
->inject('response')
2020-06-30 09:43:34 +12:00
->action(function ($response) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2020-06-30 09:43:34 +12:00
2020-12-28 06:57:35 +13:00
$response->json(['size' => Resque::size(Event::TASK_QUEUE_NAME)]);
2020-06-30 09:43:34 +12:00
}, ['response']);
2020-05-01 20:39:45 +12:00
2020-06-29 05:31:21 +12:00
App::get('/v1/health/queue/logs')
->desc('Get Logs Queue')
2020-06-26 06:32:12 +12:00
->groups(['api', 'health'])
2020-05-17 03:34:37 +12:00
->label('scope', 'health.read')
2021-04-16 19:22:17 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
2020-05-01 20:39:45 +12:00
->label('sdk.namespace', 'health')
->label('sdk.method', 'getQueueLogs')
->label('sdk.description', '/docs/references/health/get-queue-logs.md')
2020-12-27 04:10:45 +13:00
->inject('response')
2020-06-30 09:43:34 +12:00
->action(function ($response) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2020-06-30 09:43:34 +12:00
2020-12-28 06:57:35 +13:00
$response->json(['size' => Resque::size(Event::AUDITS_QUEUE_NAME)]);
2020-06-30 09:43:34 +12:00
}, ['response']);
2020-05-01 20:39:45 +12:00
2020-06-29 05:31:21 +12:00
App::get('/v1/health/queue/usage')
2020-05-17 03:22:30 +12:00
->desc('Get Usage Queue')
2020-06-26 06:32:12 +12:00
->groups(['api', 'health'])
2020-05-17 03:34:37 +12:00
->label('scope', 'health.read')
2021-04-16 19:22:17 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
2020-05-01 20:39:45 +12:00
->label('sdk.namespace', 'health')
->label('sdk.method', 'getQueueUsage')
->label('sdk.description', '/docs/references/health/get-queue-usage.md')
2020-12-27 04:10:45 +13:00
->inject('response')
2020-06-30 09:43:34 +12:00
->action(function ($response) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2020-06-30 09:43:34 +12:00
$response->json(['size' => Resque::size(Event::USAGE_QUEUE_NAME)]);
2020-06-30 09:43:34 +12:00
}, ['response']);
2020-05-01 20:39:45 +12:00
2020-06-29 05:31:21 +12:00
App::get('/v1/health/queue/certificates')
2020-05-17 03:22:30 +12:00
->desc('Get Certificate Queue')
2020-06-26 06:32:12 +12:00
->groups(['api', 'health'])
2020-05-17 03:34:37 +12:00
->label('scope', 'health.read')
2021-04-16 19:22:17 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
2020-05-01 20:39:45 +12:00
->label('sdk.namespace', 'health')
->label('sdk.method', 'getQueueCertificates')
->label('sdk.description', '/docs/references/health/get-queue-certificates.md')
2020-12-27 04:10:45 +13:00
->inject('response')
2020-06-30 09:43:34 +12:00
->action(function ($response) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2020-06-30 09:43:34 +12:00
2020-12-28 06:57:35 +13:00
$response->json(['size' => Resque::size(Event::CERTIFICATES_QUEUE_NAME)]);
2020-06-30 09:43:34 +12:00
}, ['response']);
2020-05-01 20:39:45 +12:00
2020-06-29 05:31:21 +12:00
App::get('/v1/health/queue/functions')
2020-05-17 03:34:37 +12:00
->desc('Get Functions Queue')
2020-06-26 06:32:12 +12:00
->groups(['api', 'health'])
2020-05-17 03:34:37 +12:00
->label('scope', 'health.read')
2021-04-16 19:22:17 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
2020-05-17 03:34:37 +12:00
->label('sdk.namespace', 'health')
->label('sdk.method', 'getQueueFunctions')
->label('sdk.description', '/docs/references/health/get-queue-functions.md')
2020-12-27 04:10:45 +13:00
->inject('response')
2020-06-30 09:43:34 +12:00
->action(function ($response) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2020-06-30 09:43:34 +12:00
2020-12-28 06:57:35 +13:00
$response->json(['size' => Resque::size(Event::FUNCTIONS_QUEUE_NAME)]);
2020-06-30 09:43:34 +12:00
}, ['response']);
2020-05-17 03:34:37 +12:00
2020-06-29 05:31:21 +12:00
App::get('/v1/health/storage/local')
2020-05-17 03:22:30 +12:00
->desc('Get Local Storage')
2020-06-26 06:32:12 +12:00
->groups(['api', 'health'])
2020-05-17 03:34:37 +12:00
->label('scope', 'health.read')
2021-04-16 19:22:17 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
2019-05-09 18:54:39 +12:00
->label('sdk.namespace', 'health')
->label('sdk.method', 'getStorageLocal')
2020-05-01 20:39:45 +12:00
->label('sdk.description', '/docs/references/health/get-storage-local.md')
2020-12-27 04:10:45 +13:00
->inject('response')
2020-06-30 09:43:34 +12:00
->action(function ($response) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2020-06-30 09:43:34 +12:00
foreach ([
'Uploads' => APP_STORAGE_UPLOADS,
'Cache' => APP_STORAGE_CACHE,
'Config' => APP_STORAGE_CONFIG,
'Certs' => APP_STORAGE_CERTIFICATES
] as $key => $volume) {
$device = new Local($volume);
if (!\is_readable($device->getRoot())) {
throw new Exception('Device '.$key.' dir is not readable');
2019-05-09 18:54:39 +12:00
}
2020-06-30 09:43:34 +12:00
if (!\is_writable($device->getRoot())) {
throw new Exception('Device '.$key.' dir is not writable');
}
2019-05-09 18:54:39 +12:00
}
2020-06-30 09:43:34 +12:00
$response->json(['status' => 'OK']);
2020-12-27 04:10:45 +13:00
});
2019-05-09 18:54:39 +12:00
2020-06-29 05:31:21 +12:00
App::get('/v1/health/anti-virus')
2020-05-17 03:22:30 +12:00
->desc('Get Anti virus')
2020-06-26 06:32:12 +12:00
->groups(['api', 'health'])
2020-05-17 03:34:37 +12:00
->label('scope', 'health.read')
2021-04-16 19:22:17 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
2019-05-09 18:54:39 +12:00
->label('sdk.namespace', 'health')
2020-05-17 03:22:30 +12:00
->label('sdk.method', 'getAntiVirus')
2020-05-01 20:39:45 +12:00
->label('sdk.description', '/docs/references/health/get-storage-anti-virus.md')
2020-12-27 04:10:45 +13:00
->inject('response')
2020-06-30 09:43:34 +12:00
->action(function ($response) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2019-05-09 18:54:39 +12:00
2020-06-30 09:43:34 +12:00
if (App::getEnv('_APP_STORAGE_ANTIVIRUS') === 'disabled') { // Check if scans are enabled
2021-03-20 20:14:02 +13:00
return $response->json([
'status' => 'disabled',
'version' => '',
]);
2019-05-09 18:54:39 +12:00
}
2020-06-30 09:43:34 +12:00
2021-01-03 08:45:59 +13:00
$antiVirus = new Network(App::getEnv('_APP_STORAGE_ANTIVIRUS_HOST', 'clamav'),
(int) App::getEnv('_APP_STORAGE_ANTIVIRUS_PORT', 3310));
try {
$response->json([
'status' => (@$antiVirus->ping()) ? 'online' : 'offline',
'version' => @$antiVirus->version(),
]);
2021-06-10 20:26:16 +12:00
} catch( \Exception $e) {
$response->json([
'status' => 'offline',
'version' => '',
]);
}
2020-12-27 04:10:45 +13:00
});
2019-05-09 18:54:39 +12:00
2020-06-29 05:31:21 +12:00
App::get('/v1/health/stats') // Currently only used internally
2020-05-17 03:22:30 +12:00
->desc('Get System Stats')
2020-06-26 06:32:12 +12:00
->groups(['api', 'health'])
2021-05-13 02:53:25 +12:00
->label('scope', 'root')
2021-04-16 19:22:17 +12:00
// ->label('sdk.auth', [APP_AUTH_TYPE_KEY])
2020-05-01 20:39:45 +12:00
// ->label('sdk.namespace', 'health')
// ->label('sdk.method', 'getStats')
2019-05-09 18:54:39 +12:00
->label('docs', false)
2020-12-27 04:10:45 +13:00
->inject('response')
->inject('register')
2020-06-30 09:43:34 +12:00
->action(function ($response, $register) {
2020-10-30 02:50:49 +13:00
/** @var Appwrite\Utopia\Response $response */
2020-06-30 09:43:34 +12:00
/** @var Utopia\Registry\Registry $register */
2020-07-15 09:20:46 +12:00
$device = Storage::getDevice('files');
2020-06-30 09:43:34 +12:00
$cache = $register->get('cache');
$cacheStats = $cache->info();
$response
->json([
'storage' => [
'used' => Storage::human($device->getDirectorySize($device->getRoot().'/')),
'partitionTotal' => Storage::human($device->getPartitionTotalSpace()),
'partitionFree' => Storage::human($device->getPartitionFreeSpace()),
],
'cache' => [
2020-10-15 10:34:57 +13:00
'uptime' => $cacheStats['uptime_in_seconds'] ?? 0,
'clients' => $cacheStats['connected_clients'] ?? 0,
'hits' => $cacheStats['keyspace_hits'] ?? 0,
'misses' => $cacheStats['keyspace_misses'] ?? 0,
'memory_used' => $cacheStats['used_memory'] ?? 0,
'memory_used_human' => $cacheStats['used_memory_human'] ?? 0,
'memory_used_peak' => $cacheStats['used_memory_peak'] ?? 0,
'memory_used_peak_human' => $cacheStats['used_memory_peak_human'] ?? 0,
2020-06-30 09:43:34 +12:00
],
]);
2020-12-27 04:10:45 +13:00
});