LuminaWebsite/admin/server.php

346 lines
20 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* Speicher & Traffic: was die Lumina-Website belegt (Dateien, Datenbank), wie viel auf dem Server noch frei ist
* und wie viel die Website uebertraegt. Nur fuer Entwickler und Administratoren - alle anderen sehen die
* 404-Seite. Die Zahlen sammelt includes/server_stats_collect.php als root auf dem Server, diese Seite zeigt sie
* an und fragt nur die Tabellengroessen selbst ab.
*/
require_once __DIR__ . '/../includes/bootstrap.php';
require_login();
require_once __DIR__ . '/../includes/server_stats.php';
if (!server_stats_can_view()) {
show_not_found();
}
$stats = server_stats_load();
$weekdays = ['So.', 'Mo.', 'Di.', 'Mi.', 'Do.', 'Fr.', 'Sa.'];
$monthNames = [1 => 'Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember'];
/** Breite eines Balkens: Anteil an $max in Prozent - sichtbar, sobald ueberhaupt etwas da ist. */
$barWidth = static fn (float $value, float $max): string => $max > 0 && $value > 0
? number_format(max(0.8, min(100, $value / $max * 100)), 2, '.', '') . '%'
: '0';
$signed = static fn (float $bytes): string => ($bytes >= 0 ? '+' : '−') . format_bytes(abs($bytes));
$pageTitle = 'Speicher & Traffic';
$containerWide = true;
include __DIR__ . '/../includes/header.php';
?>
<div class="st-page">
<?php if ($stats === null): ?>
<div class="panel">
<h1>Speicher &amp; Traffic</h1>
<p class="text-muted">
Noch gibt es keine Zahlen. Die sammelt ein kleines Skript auf dem Server
(<code>includes/server_stats_collect.php</code>, als root per Timer – die Einrichtung steht im Kopf der Datei)
und legt sie hier ab: <code><?= e(server_stats_file()) ?></code>
</p>
</div>
<?php else: ?>
<?php
$generatedAt = (int) strtotime((string) $stats['generated_at']);
$stale = $generatedAt < time() - SERVER_STATS_STALE_MINUTES * 60;
// ---- Website: Dateien (Sammelskript) und Datenbank (hier abgefragt, sonst der Stand des Skripts) ----
$site = $stats['site'];
$dbTables = server_stats_db_tables();
$dbBytes = $dbTables ? (float) array_sum(array_column($dbTables, 'bytes')) : (float) ($site['db_bytes'] ?? 0);
$fileBytes = (float) $site['total'];
$siteTotal = $fileBytes + $dbBytes;
$partBytes = array_column($site['parts'], 'bytes', 'key') + ['db' => $dbBytes];
$history = is_array($stats['history'] ?? null) ? $stats['history'] : [];
$growthWeek = server_stats_growth($history, 7);
$growthMonth = server_stats_growth($history, 30);
// ---- Server: nur, wie viel Platz noch ist ----
$disk = $stats['disk'];
$diskTotal = (float) $disk['total'];
$diskUsed = (float) $disk['used'];
$diskFree = (float) $disk['free'];
$usedPercent = $diskTotal > 0 ? $diskUsed / $diskTotal * 100 : 0;
$diskState = $usedPercent >= SERVER_STATS_DISK_CRITICAL ? 'critical' : ($usedPercent >= SERVER_STATS_DISK_WARN ? 'warning' : 'ok');
$diskOther = max(0, $diskUsed - $siteTotal);
// ---- Traffic: Tageswerte, die letzten 30 Tage lueckenlos (ab dem ersten gezaehlten Tag) ----
$traffic = is_array($stats['traffic'] ?? null) ? $stats['traffic'] : [];
$trafficDays = is_array($traffic['days'] ?? null) ? $traffic['days'] : [];
$since = !empty($traffic['since']) ? (string) $traffic['since'] : null;
$today = date('Y-m-d');
$chartDays = [];
for ($i = 29; $i >= 0; $i--) {
$date = date('Y-m-d', strtotime("-$i days"));
$row = $trafficDays[$date] ?? ['out' => 0, 'in' => 0, 'requests' => 0];
$chartDays[$date] = $row + ['bytes' => (float) $row['out'] + (float) $row['in'], 'counted' => $since !== null && $date >= $since];
}
$chartMax = server_stats_nice_max((float) max(array_column($chartDays, 'bytes')));
$sumToday = server_stats_traffic_sum([$trafficDays[$today] ?? []]);
$sumYesterday = server_stats_traffic_sum([$trafficDays[date('Y-m-d', strtotime('-1 day'))] ?? []]);
$sumWeek = server_stats_traffic_sum(array_slice($chartDays, -7, null, true));
$months = [];
foreach ($trafficDays as $date => $row) {
$key = substr((string) $date, 0, 7);
$months[$key] ??= ['out' => 0, 'in' => 0, 'requests' => 0];
foreach (['out', 'in', 'requests'] as $field) {
$months[$key][$field] += (int) ($row[$field] ?? 0);
}
}
krsort($months);
$sumMonth = server_stats_traffic_sum([$months[date('Y-m')] ?? []]);
?>
<?php // ============================================================ Kopf: das Wichtigste auf einen Blick ?>
<div class="panel st-head">
<div class="st-title">
<h1>Speicher &amp; Traffic</h1>
<span class="st-stand text-muted">Stand <?= e(date('d.m.Y, H:i', $generatedAt)) ?> Uhr · alle 15 Minuten neu</span>
</div>
<p class="text-muted">Was die Lumina-Website belegt und überträgt – Dateien, Datenbank und Datenvolumen.</p>
<?php if ($stale): ?>
<div class="st-notice st-notice-warning"><?= icon('alert') ?> <span><strong>Die Zahlen sind veraltet.</strong>
Das Sammelskript auf dem Server hat zuletzt <?= e(last_active_label(date('Y-m-d H:i:s', $generatedAt))) ?> geschrieben – läuft der Timer <code>lumina-stats.timer</code> noch?</span></div>
<?php endif; ?>
<?php if ($diskState !== 'ok'): ?>
<div class="st-notice st-notice-<?= $diskState ?>"><?= icon('alert') ?> <span>
<strong><?= $diskState === 'critical' ? 'Der Speicher des Servers ist fast voll.' : 'Der Speicher des Servers wird knapp.' ?></strong>
<?= e(format_count($usedPercent)) ?> % sind belegt, frei sind noch <?= e(format_bytes($diskFree)) ?>.
Ist die Platte voll, kann die Datenbank nicht mehr schreiben und die Website zeigt Fehler.</span></div>
<?php endif; ?>
<div class="st-kpis">
<div class="st-kpi st-kpi-main">
<span class="st-kpi-icon"><?= icon('home') ?></span>
<div>
<span class="st-kpi-label">Website belegt</span>
<span class="st-kpi-value"><?= e(format_bytes($siteTotal)) ?></span>
<span class="st-kpi-note"><?= e(format_bytes($fileBytes)) ?> Dateien · <?= e(format_bytes($dbBytes)) ?> Datenbank</span>
</div>
</div>
<div class="st-kpi">
<span class="st-kpi-icon"><?= icon('trend') ?></span>
<div>
<span class="st-kpi-label">Zuwachs in 7 Tagen</span>
<?php if ($growthWeek === null): ?>
<span class="st-kpi-value st-kpi-empty">–</span>
<span class="st-kpi-note">Verlauf beginnt gerade erst</span>
<?php else: ?>
<span class="st-kpi-value"><?= e($signed($growthWeek)) ?></span>
<span class="st-kpi-note"><?= $growthMonth !== null ? 'in 30 Tagen ' . e($signed($growthMonth)) : 'Dateien und Datenbank' ?></span>
<?php endif; ?>
</div>
</div>
<div class="st-kpi">
<span class="st-kpi-icon"><?= icon('transfer') ?></span>
<div>
<span class="st-kpi-label">Traffic heute</span>
<span class="st-kpi-value"><?= e(format_bytes($sumToday['bytes'])) ?></span>
<span class="st-kpi-note"><?= e(format_count($sumToday['requests'])) ?> Aufrufe</span>
</div>
</div>
<div class="st-kpi">
<span class="st-kpi-icon"><?= icon('server') ?></span>
<div>
<span class="st-kpi-label">Frei auf dem Server</span>
<span class="st-kpi-value"><?= e(format_bytes($diskFree)) ?></span>
<span class="st-kpi-note">von <?= e(format_bytes($diskTotal)) ?> · <?= e(format_count($usedPercent)) ?> % belegt</span>
</div>
</div>
</div>
</div>
<?php // ============================================================ Woraus die Website besteht ?>
<div class="panel">
<h2>Woraus die Website besteht</h2>
<p class="text-muted">Dateien unter <code><?= e((string) $site['path']) ?></code> und die Datenbank – zusammen <?= e(format_bytes($siteTotal)) ?>.</p>
<div class="st-stack" role="img" aria-label="Aufteilung des Speichers der Website – die Werte stehen in der Liste darunter">
<?php foreach (SERVER_STATS_PARTS as $key => [$label, $color]): ?>
<?php $bytes = (float) ($partBytes[$key] ?? 0); ?>
<?php if ($bytes > 0): ?>
<span class="st-seg" style="flex-grow: <?= e(number_format($bytes / max(1, $siteTotal) * 1000, 2, '.', '')) ?>; background: <?= e($color) ?>"
title="<?= e($label . ': ' . format_bytes($bytes) . ' (' . format_share($bytes, $siteTotal) . ')') ?>"></span>
<?php endif; ?>
<?php endforeach; ?>
</div>
<ul class="st-legend">
<?php foreach (SERVER_STATS_PARTS as $key => [$label, $color]): ?>
<?php $bytes = (float) ($partBytes[$key] ?? 0); ?>
<li>
<i class="st-dot" style="background: <?= e($color) ?>"></i>
<span class="st-legend-name"><?= e($label) ?></span>
<span class="st-legend-value"><?= e(format_bytes($bytes)) ?></span>
<span class="st-legend-share"><?= e(format_share($bytes, $siteTotal)) ?></span>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php // ============================================================ Uploads und Datenbank im Einzelnen ?>
<div class="st-two">
<div class="panel">
<h2><i class="st-dot" style="background: <?= e(SERVER_STATS_PARTS['uploads'][1]) ?>"></i> Hochgeladene Dateien</h2>
<?php if ($site['uploads']): ?>
<?php $uploadMax = (float) max(array_column($site['uploads'], 'bytes')); ?>
<ul class="st-rows">
<?php foreach ($site['uploads'] as $upload): ?>
<li title="assets/uploads/<?= e((string) $upload['dir']) ?>">
<div class="st-row-head">
<span class="st-row-name"><?= e(SERVER_STATS_UPLOAD_LABELS[$upload['dir']] ?? (string) $upload['dir']) ?></span>
<span class="st-row-meta"><?= e(format_count((int) $upload['files'])) ?> <?= (int) $upload['files'] === 1 ? 'Datei' : 'Dateien' ?></span>
<span class="st-row-value"><?= e(format_bytes($upload['bytes'])) ?></span>
</div>
<span class="st-track" aria-hidden="true"><span class="st-fill" style="width: <?= e($barWidth((float) $upload['bytes'], $uploadMax)) ?>; background: <?= e(SERVER_STATS_PARTS['uploads'][1]) ?>"></span></span>
</li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p class="text-muted">Noch nichts hochgeladen.</p>
<?php endif; ?>
</div>
<div class="panel">
<h2><i class="st-dot" style="background: <?= e(SERVER_STATS_PARTS['db'][1]) ?>"></i> Datenbank</h2>
<?php if ($dbTables): ?>
<?php
$dbTop = array_slice($dbTables, 0, 9);
$dbRest = array_slice($dbTables, 9);
$dbMax = (float) $dbTop[0]['bytes'];
?>
<ul class="st-rows">
<?php foreach ($dbTop as $table): ?>
<li>
<div class="st-row-head">
<span class="st-row-name"><code><?= e($table['name']) ?></code></span>
<span class="st-row-meta" title="Bei dieser Datenbank eine Schätzung">ca. <?= e(format_count($table['rows'])) ?> Zeilen</span>
<span class="st-row-value"><?= e(format_bytes($table['bytes'])) ?></span>
</div>
<span class="st-track" aria-hidden="true"><span class="st-fill" style="width: <?= e($barWidth((float) $table['bytes'], $dbMax)) ?>; background: <?= e(SERVER_STATS_PARTS['db'][1]) ?>"></span></span>
</li>
<?php endforeach; ?>
<?php if ($dbRest): ?>
<li class="st-row-rest">
<div class="st-row-head">
<span class="st-row-name">Übrige <?= count($dbRest) ?> Tabellen</span>
<span class="st-row-value"><?= e(format_bytes(array_sum(array_column($dbRest, 'bytes')))) ?></span>
</div>
</li>
<?php endif; ?>
</ul>
<?php else: ?>
<p class="text-muted">Die Tabellengrößen lassen sich gerade nicht abfragen.</p>
<?php endif; ?>
</div>
</div>
<?php // ============================================================ Traffic ?>
<div class="panel">
<h2>Traffic der Website</h2>
<p class="text-muted">
Datenvolumen und Aufrufe von lumina-ascendens.de<?= $since !== null ? ', gezählt seit dem ' . e(date('d.m.Y', (int) strtotime($since))) : '' ?>.
Der Server merkt sich dafür nur Zeitpunkt und Datenmenge jedes Aufrufs – keine IP-Adressen, keine Seiten.
</p>
<?php if (!$trafficDays): ?>
<div class="st-notice"><?= icon('alert') ?> <span>Noch nichts gezählt – die ersten Werte erscheinen mit dem nächsten Lauf des Sammelskripts.</span></div>
<?php else: ?>
<div class="st-minis">
<?php foreach (['Heute' => $sumToday, 'Gestern' => $sumYesterday, 'Letzte 7 Tage' => $sumWeek, $monthNames[(int) date('n')] => $sumMonth] as $label => $sum): ?>
<div class="st-mini">
<span class="st-kpi-label"><?= e((string) $label) ?></span>
<span class="st-mini-value"><?= e(format_bytes($sum['bytes'])) ?></span>
<span class="st-kpi-note"><?= e(format_count($sum['requests'])) ?> Aufrufe</span>
</div>
<?php endforeach; ?>
</div>
<h3>Datenvolumen je Tag – letzte 30 Tage</h3>
<div class="st-chart" role="img" aria-label="Säulendiagramm: Datenvolumen je Tag in den letzten 30 Tagen – die Werte stehen in der Tabelle darunter">
<div class="st-grid" aria-hidden="true">
<span style="bottom: 100%"><i><?= e(format_bytes($chartMax)) ?></i></span>
<span style="bottom: 50%"><i><?= e(format_bytes($chartMax / 2)) ?></i></span>
<span style="bottom: 0"><i>0</i></span>
</div>
<div class="st-cols">
<?php $index = 0; ?>
<?php foreach ($chartDays as $date => $row): ?>
<?php
$time = (int) strtotime($date);
$dayLabel = $weekdays[(int) date('w', $time)] . ' ' . date('d.m.', $time);
$height = $row['bytes'] > 0 ? max(1.5, $row['bytes'] / $chartMax * 100) : 0;
?>
<div class="st-col<?= $date === $today ? ' is-today' : '' ?>" tabindex="0">
<span class="st-col-bar" style="height: <?= e(number_format($height, 2, '.', '')) ?>%"></span>
<span class="st-tip">
<strong><?= e($dayLabel) ?><?= $date === $today ? ' (heute, läuft noch)' : '' ?></strong>
<?php if ($row['counted']): ?>
<?= e(format_bytes($row['bytes'])) ?> · <?= e(format_count($row['requests'])) ?> Aufrufe
<?php else: ?>
noch nicht gezählt
<?php endif; ?>
</span>
<?php if ($index % 5 === 4 || $date === $today): ?>
<span class="st-col-label"><?= $date === $today ? 'heute' : e(date('d.m.', $time)) ?></span>
<?php endif; ?>
</div>
<?php $index++; ?>
<?php endforeach; ?>
</div>
</div>
<details class="st-details">
<summary>Als Tabelle anzeigen</summary>
<?php if (count($months) > 1): ?>
<h3>Monate</h3>
<div class="table-scroll">
<table class="st-table">
<thead><tr><th>Monat</th><th class="st-num">Datenvolumen</th><th class="st-num">Gesendet</th><th class="st-num">Empfangen</th><th class="st-num">Aufrufe</th></tr></thead>
<tbody>
<?php foreach ($months as $key => $row): ?>
<tr>
<td><?= e($monthNames[(int) substr((string) $key, 5, 2)] . ' ' . substr((string) $key, 0, 4)) ?></td>
<td class="st-num"><strong><?= e(format_bytes($row['out'] + $row['in'])) ?></strong></td>
<td class="st-num"><?= e(format_bytes($row['out'])) ?></td>
<td class="st-num"><?= e(format_bytes($row['in'])) ?></td>
<td class="st-num"><?= e(format_count($row['requests'])) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<h3>Tage</h3>
<?php endif; ?>
<div class="table-scroll">
<table class="st-table">
<thead><tr><th>Tag</th><th class="st-num">Datenvolumen</th><th class="st-num">Gesendet</th><th class="st-num">Empfangen</th><th class="st-num">Aufrufe</th></tr></thead>
<tbody>
<?php foreach (array_reverse($chartDays, true) as $date => $row): ?>
<?php if (!$row['counted']) { continue; } ?>
<?php $time = (int) strtotime($date); ?>
<tr>
<td><?= e($weekdays[(int) date('w', $time)] . ' ' . date('d.m.Y', $time)) ?></td>
<td class="st-num"><strong><?= e(format_bytes($row['bytes'])) ?></strong></td>
<td class="st-num"><?= e(format_bytes($row['out'])) ?></td>
<td class="st-num"><?= e(format_bytes($row['in'])) ?></td>
<td class="st-num"><?= e(format_count($row['requests'])) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</details>
<?php endif; ?>
</div>
<?php // ============================================================ Platz auf dem Server ?>
<div class="panel">
<h2>Platz auf dem Server</h2>
<p class="text-muted">Die Website teilt sich die Festplatte mit anderem – hier nur, wie viel davon noch frei ist.</p>
<div class="st-capacity st-capacity-<?= $diskState ?>" role="img" aria-label="<?= e(format_count($usedPercent)) ?> % der Festplatte belegt">
<span class="st-cap-site" style="width: <?= e($barWidth($siteTotal, $diskTotal)) ?>" title="Lumina-Website: <?= e(format_bytes($siteTotal)) ?>"></span>
<span class="st-cap-other" style="width: <?= e($barWidth($diskOther, $diskTotal)) ?>" title="Übriges auf dem Server: <?= e(format_bytes($diskOther)) ?>"></span>
</div>
<ul class="st-legend st-legend-inline">
<li><i class="st-dot st-cap-site"></i><span class="st-legend-name">Lumina-Website</span><span class="st-legend-value"><?= e(format_bytes($siteTotal)) ?></span></li>
<li><i class="st-dot st-cap-other"></i><span class="st-legend-name">Übriges auf dem Server</span><span class="st-legend-value"><?= e(format_bytes($diskOther)) ?></span></li>
<li><i class="st-dot st-cap-free"></i><span class="st-legend-name">Frei</span><span class="st-legend-value"><?= e(format_bytes($diskFree)) ?></span></li>
</ul>
</div>
<?php endif; ?>
</div>
<?php include __DIR__ . '/../includes/footer.php'; ?>