asdad
All checks were successful
Deploy / deploy-staging (push) Successful in 31s
Deploy / deploy-production (push) Has been skipped

This commit is contained in:
2026-04-22 00:03:19 +02:00
parent 82ad817ad3
commit 1d948f0508
4 changed files with 248 additions and 6 deletions

View File

@@ -0,0 +1,91 @@
<?php
use App\Database;
use App\Repository\KeaHostMetadataRepository;
use App\Repository\KeaHostRepository;
header('Content-Type: application/json; charset=utf-8');
header('Cache-Control: no-store');
$module = modules()->get('kea');
$fallback = $module['db_defaults'] ?? [];
$settings = modules()->settings('kea');
$metadataFallback = is_array($module['metadata_db_defaults'] ?? null) ? $module['metadata_db_defaults'] : [];
$metadataConfig = is_array($settings['metadata_db'] ?? null)
? array_replace($metadataFallback, $settings['metadata_db'])
: $metadataFallback;
try {
$metadataRepo = null;
$pdo = modules()->modulePdo('kea', $fallback);
if (!empty($metadataConfig['driver']) && !empty($metadataConfig['dbname'])) {
try {
$metadataRepo = new KeaHostMetadataRepository(Database::createFromArray($metadataConfig));
$metadataRepo->ensureSchema();
} catch (\Throwable) {
$metadataRepo = null;
}
}
$repo = new KeaHostRepository($pdo, $metadataRepo);
$hosts = $repo->findAll(200);
$stats = [
'total' => $repo->countReservations() + $repo->countLeases(),
'reservations' => $repo->countReservations(),
'leases' => $repo->countLeases(),
'groups' => [],
'free_ips' => [],
];
foreach ($hosts as $host) {
$group = trim((string)($host['metadata']['group_name'] ?? ''));
if ($group !== '') {
$stats['groups'][$group] = ($stats['groups'][$group] ?? 0) + 1;
}
}
if ($metadataRepo !== null) {
$stats['free_ips'] = array_map(
static fn(array $ips): int => count($ips),
$metadataRepo->availableIpsByGroup(
array_merge($repo->usedIpAddresses(), $metadataRepo->desiredIps()),
4096
)
);
}
$rows = array_map(static function (array $host): array {
return [
'source' => (string)($host['source'] ?? 'reservation'),
'host_id' => (string)($host['host_id'] ?? '0'),
'display_name' => (string)($host['metadata']['device_name'] ?? $host['metadata']['real_name'] ?? $host['display_name'] ?? $host['hostname'] ?? 'Unbekannt'),
'ipv4_address' => (string)($host['ipv4_address'] ?? ''),
'dhcp_identifier' => (string)($host['dhcp_identifier'] ?? ''),
'last_seen_at' => (string)($host['last_seen_at'] ?? '-'),
'lease_expires_at' => (string)($host['lease_expires_at'] ?? '-'),
'real_name' => (string)($host['metadata']['real_name'] ?? '-'),
'location' => (string)($host['metadata']['location'] ?? '-'),
'group_name' => (string)($host['metadata']['group_name'] ?? '-'),
];
}, $hosts);
echo json_encode([
'ok' => true,
'stats' => [
'total' => (int)$stats['total'],
'reservations' => (int)$stats['reservations'],
'leases' => (int)$stats['leases'],
'groups' => count($stats['groups']),
'free_ips' => array_sum($stats['free_ips']),
],
'rows' => $rows,
'updated_at' => date('H:i:s'),
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
} catch (\Throwable $e) {
http_response_code(500);
echo json_encode([
'ok' => false,
'error' => $e->getMessage(),
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}
exit;