1
0
Fork 0
mirror of synced 2024-06-02 19:04:49 +12:00
appwrite/app/controllers/api/health.php

256 lines
9.2 KiB
PHP
Raw Normal View History

2019-05-09 18:54:39 +12:00
<?php
global $utopia, $request, $response, $register, $project;
use Utopia\Exception;
use Appwrite\Storage\Devices\Local;
use Appwrite\Storage\Storage;
2019-08-21 03:43:01 +12:00
use Appwrite\ClamAV\Network;
2019-05-09 18:54:39 +12:00
$utopia->get('/v1/health')
2020-05-17 03:22:30 +12:00
->desc('Get HTTP')
2020-05-01 22:07:53 +12:00
->label('scope', 'public')
2020-01-27 19:14:14 +13:00
->label('sdk.platform', [APP_PLATFORM_SERVER])
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')
2019-05-09 18:54:39 +12:00
->action(
2019-09-20 21:08:24 +12:00
function () use ($response) {
$response->json(['status' => 'OK']);
2019-05-09 18:54:39 +12:00
}
);
$utopia->get('/v1/health/db')
2020-05-17 03:22:30 +12:00
->desc('Get DB')
2020-05-01 22:24:36 +12:00
->label('scope', 'public')
2020-01-27 19:14:14 +13:00
->label('sdk.platform', [APP_PLATFORM_SERVER])
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')
2019-05-09 18:54:39 +12:00
->action(
function () use ($response, $register) {
2019-05-09 18:54:39 +12:00
$register->get('db'); /* @var $db PDO */
2019-09-20 21:08:24 +12:00
$response->json(['status' => 'OK']);
2019-05-09 18:54:39 +12:00
}
);
$utopia->get('/v1/health/cache')
2020-05-17 03:22:30 +12:00
->desc('Get Cache')
2020-05-01 22:24:36 +12:00
->label('scope', 'public')
2020-01-27 19:14:14 +13:00
->label('sdk.platform', [APP_PLATFORM_SERVER])
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')
2019-05-09 18:54:39 +12:00
->action(
function () use ($response, $register) {
2019-05-09 18:54:39 +12:00
$register->get('cache'); /* @var $cache Predis\Client */
2019-09-20 21:08:24 +12:00
$response->json(['status' => 'OK']);
2019-05-09 18:54:39 +12:00
}
);
$utopia->get('/v1/health/time')
2020-05-17 03:22:30 +12:00
->desc('Get Time')
2020-05-01 22:24:36 +12:00
->label('scope', 'public')
2020-01-27 19:14:14 +13:00
->label('sdk.platform', [APP_PLATFORM_SERVER])
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')
2019-05-09 18:54:39 +12:00
->action(
function () use ($response) {
/*
2019-05-09 18:54:39 +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
/* Create a socket and connect to NTP server */
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_connect($sock, $host, 123);
/* Send request */
$msg = "\010".str_repeat("\0", 47);
2019-05-09 18:54:39 +12:00
socket_send($sock, $msg, strlen($msg), 0);
/* Receive response and close socket */
socket_recv($sock, $recv, 48, MSG_WAITALL);
socket_close($sock);
/* Interpret response */
$data = unpack('N12', $recv);
$timestamp = sprintf('%u', $data[9]);
/* 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;
$diff = ($timestamp - time());
if ($diff > $gap || $diff < ($gap * -1)) {
2019-05-09 18:54:39 +12:00
throw new Exception('Server time gaps detected');
}
$response->json(['remote' => $timestamp, 'local' => time(), 'diff' => $diff]);
}
);
2020-05-01 20:39:45 +12:00
$utopia->get('/v1/health/queue/webhooks')
2020-05-17 03:22:30 +12:00
->desc('Get Webhooks Queue')
2020-05-01 22:24:36 +12:00
->label('scope', 'public')
2020-01-27 19:14:14 +13:00
->label('sdk.platform', [APP_PLATFORM_SERVER])
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')
2019-05-09 18:54:39 +12:00
->action(
function () use ($response) {
2019-12-26 23:00:13 +13:00
$response->json(['size' => Resque::size('v1-webhooks')]);
2019-05-09 18:54:39 +12:00
}
);
2020-05-01 20:39:45 +12:00
$utopia->get('/v1/health/queue/tasks')
2020-05-17 03:22:30 +12:00
->desc('Get Tasks Queue')
2020-05-01 22:24:36 +12:00
->label('scope', 'public')
2020-05-01 20:39:45 +12:00
->label('sdk.platform', [APP_PLATFORM_SERVER])
->label('sdk.namespace', 'health')
->label('sdk.method', 'getQueueTasks')
->label('sdk.description', '/docs/references/health/get-queue-tasks.md')
->action(
function () use ($response) {
$response->json(['size' => Resque::size('v1-tasks')]);
}
);
$utopia->get('/v1/health/queue/logs')
2020-05-17 03:22:30 +12:00
->desc('Get Logs Queue')
2020-05-01 22:24:36 +12:00
->label('scope', 'public')
2020-05-01 20:39:45 +12:00
->label('sdk.platform', [APP_PLATFORM_SERVER])
->label('sdk.namespace', 'health')
->label('sdk.method', 'getQueueLogs')
->label('sdk.description', '/docs/references/health/get-queue-logs.md')
->action(
function () use ($response) {
$response->json(['size' => Resque::size('v1-audit')]);
}
);
$utopia->get('/v1/health/queue/usage')
2020-05-17 03:22:30 +12:00
->desc('Get Usage Queue')
2020-05-01 22:24:36 +12:00
->label('scope', 'public')
2020-05-01 20:39:45 +12:00
->label('sdk.platform', [APP_PLATFORM_SERVER])
->label('sdk.namespace', 'health')
->label('sdk.method', 'getQueueUsage')
->label('sdk.description', '/docs/references/health/get-queue-usage.md')
->action(
function () use ($response) {
$response->json(['size' => Resque::size('v1-usage')]);
}
);
$utopia->get('/v1/health/queue/certificates')
2020-05-17 03:22:30 +12:00
->desc('Get Certificate Queue')
2020-05-01 22:24:36 +12:00
->label('scope', 'public')
2020-05-01 20:39:45 +12:00
->label('sdk.platform', [APP_PLATFORM_SERVER])
->label('sdk.namespace', 'health')
->label('sdk.method', 'getQueueCertificates')
->label('sdk.description', '/docs/references/health/get-queue-certificates.md')
->action(
function () use ($response) {
$response->json(['size' => Resque::size('v1-usage')]);
}
);
2019-05-09 18:54:39 +12:00
$utopia->get('/v1/health/storage/local')
2020-05-17 03:22:30 +12:00
->desc('Get Local Storage')
2020-05-01 22:24:36 +12:00
->label('scope', 'public')
2020-01-27 19:14:14 +13:00
->label('sdk.platform', [APP_PLATFORM_SERVER])
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')
2019-05-09 18:54:39 +12:00
->action(
function () use ($response) {
2020-02-20 01:41:23 +13:00
$device = new Local(APP_STORAGE_UPLOADS.'/');
2019-05-09 18:54:39 +12:00
2019-10-01 17:57:41 +13:00
if (!is_readable($device->getRoot().'/..')) {
2019-05-09 18:54:39 +12:00
throw new Exception('Device is not readable');
}
2020-05-01 22:24:36 +12:00
if (!is_writable($device->getRoot().'/../uploads')) {
throw new Exception('Device uploads dir is not writable');
}
if (!is_writable($device->getRoot().'/../cache')) {
throw new Exception('Device cache dir is not writable');
}
2019-05-09 18:54:39 +12:00
2020-05-01 22:24:36 +12:00
if (!is_writable($device->getRoot().'/../config')) {
throw new Exception('Device config dir is not writable');
}
if (!is_writable($device->getRoot().'/../certificates')) {
throw new Exception('Device certificates dir is not writable');
2019-05-09 18:54:39 +12:00
}
2019-09-20 21:08:24 +12:00
$response->json(['status' => 'OK']);
2019-05-09 18:54:39 +12:00
}
);
2020-05-17 03:22:30 +12:00
$utopia->get('/v1/health/anti-virus')
->desc('Get Anti virus')
2020-05-01 22:24:36 +12:00
->label('scope', 'public')
2020-01-27 19:14:14 +13:00
->label('sdk.platform', [APP_PLATFORM_SERVER])
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')
2019-05-09 18:54:39 +12:00
->action(
function () use ($response) {
2019-08-21 03:43:01 +12:00
$antiVirus = new Network('clamav', 3310);
2019-05-09 18:54:39 +12:00
$response->json([
'status' => (@$antiVirus->ping()) ? 'online' : 'offline',
'version' => @$antiVirus->version(),
]);
}
);
2020-05-01 20:39:45 +12:00
$utopia->get('/v1/health/stats') // Currently only used internally
2020-05-17 03:22:30 +12:00
->desc('Get System Stats')
2019-05-09 18:54:39 +12:00
->label('scope', 'god')
2020-05-01 20:39:45 +12:00
// ->label('sdk.platform', [APP_PLATFORM_SERVER])
// ->label('sdk.namespace', 'health')
// ->label('sdk.method', 'getStats')
2019-05-09 18:54:39 +12:00
->label('docs', false)
->action(
2019-09-20 21:08:24 +12:00
function () use ($response, $register) {
2019-05-09 18:54:39 +12:00
$device = Storage::getDevice('local');
$cache = $register->get('cache');
2019-05-09 18:54:39 +12:00
$cacheStats = $cache->info();
$response
->json([
'server' => [
'name' => 'nginx',
'version' => shell_exec('nginx -v 2>&1'),
],
2019-05-09 18:54:39 +12:00
'storage' => [
'used' => Storage::human($device->getDirectorySize($device->getRoot().'/')),
'partitionTotal' => Storage::human($device->getPartitionTotalSpace()),
'partitionFree' => Storage::human($device->getPartitionFreeSpace()),
2019-05-09 18:54:39 +12:00
],
'cache' => [
'uptime' => (isset($cacheStats['uptime_in_seconds'])) ? $cacheStats['uptime_in_seconds'] : 0,
'clients' => (isset($cacheStats['connected_clients'])) ? $cacheStats['connected_clients'] : 0,
'hits' => (isset($cacheStats['keyspace_hits'])) ? $cacheStats['keyspace_hits'] : 0,
'misses' => (isset($cacheStats['keyspace_misses'])) ? $cacheStats['keyspace_misses'] : 0,
'memory_used' => (isset($cacheStats['used_memory'])) ? $cacheStats['used_memory'] : 0,
'memory_used_human' => (isset($cacheStats['used_memory_human'])) ? $cacheStats['used_memory_human'] : 0,
'memory_used_peak' => (isset($cacheStats['used_memory_peak'])) ? $cacheStats['used_memory_peak'] : 0,
'memory_used_peak_human' => (isset($cacheStats['used_memory_peak_human'])) ? $cacheStats['used_memory_peak_human'] : 0,
],
2019-05-09 18:54:39 +12:00
]);
}
);