Files
syslogdash/pages/logs.php
T
2026-07-30 16:26:21 +02:00

149 lines
7.9 KiB
PHP

<?php
use SyslogDash\SyslogQuery;
$sq = new SyslogQuery();
$filters = [
'host' => $_GET['host'] ?? '',
'country' => $_GET['country'] ?? '',
'tag' => $_GET['tag'] ?? '',
'facility' => $_GET['facility'] ?? '',
'priority' => $_GET['priority'] ?? '',
'search' => $_GET['search'] ?? '',
'from' => $_GET['from'] ?? '',
'to' => $_GET['to'] ?? '',
'sort' => $_GET['sort'] ?? 'DESC',
'sort_col' => $_GET['sort_col'] ?? 'ReceivedAt',
];
$filters = array_filter($filters, fn($v) => $v !== '');
$page = max(1, (int)($_GET['p'] ?? 1));
$result = $sq->getLogs($filters, $page, 50);
$hosts = $sq->getDistinct('FromHost');
$facilities = $sq->getDistinct('Facility');
$priorities = $sq->getDistinct('Priority');
function sortUrl($col, $filters, $currentSort, $currentCol) {
$dir = 'ASC';
if ($currentCol === $col && $currentSort === 'ASC') $dir = 'DESC';
$params = array_merge($filters, ['sort_col' => $col, 'sort' => $dir, 'p' => 1]);
return '?page=logs&' . http_build_query($params);
}
function filterUrl($filters, $override) {
$params = array_merge($filters, $override, ['p' => 1]);
return '?page=logs&' . http_build_query($params);
}
?>
<div class="d-flex justify-content-between align-items-center mb-4">
<h2 class="mb-0"><i class="bi bi-list-ul me-2"></i>Logs</h2>
<div>
<a href="?page=export&<?= http_build_query($_GET) ?>" class="btn btn-sm btn-outline-success me-1"><i class="bi bi-download"></i> CSV</a>
<a href="?page=export&format=json&<?= http_build_query($_GET) ?>" class="btn btn-sm btn-outline-info"><i class="bi bi-download"></i> JSON</a>
</div>
</div>
<div class="card shadow-sm mb-4">
<div class="card-header bg-white"><i class="bi bi-funnel me-2"></i>Filters</div>
<div class="card-body">
<form method="GET" action="" class="row g-3">
<input type="hidden" name="page" value="logs">
<div class="col-md-2">
<label class="form-label small">Host</label>
<select name="host" class="form-select form-select-sm">
<option value="">Alle</option>
<?php foreach ($hosts as $h): ?>
<option value="<?= htmlspecialchars($h) ?>" <?= ($filters['host'] ?? '') === $h ? 'selected' : '' ?>><?= htmlspecialchars($h) ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-2">
<label class="form-label small">Facility</label>
<select name="facility" class="form-select form-select-sm">
<option value="">Alle</option>
<?php foreach ($facilities as $f): ?>
<option value="<?= $f ?>" <?= ($filters['facility'] ?? '') === (string)$f ? 'selected' : '' ?>><?= SyslogQuery::facilityName((int)$f) ?> (<?= $f ?>)</option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-2">
<label class="form-label small">Severity</label>
<select name="priority" class="form-select form-select-sm">
<option value="">Alle</option>
<?php foreach ($priorities as $p): ?>
<option value="<?= $p ?>" <?= ($filters['priority'] ?? '') === (string)$p ? 'selected' : '' ?>><?= SyslogQuery::severityName((int)$p) ?> (<?= $p ?>)</option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-2">
<label class="form-label small">Vanaf</label>
<input type="date" name="from" class="form-control form-control-sm" value="<?= htmlspecialchars($filters['from'] ?? '') ?>">
</div>
<div class="col-md-2">
<label class="form-label small">Tot</label>
<input type="date" name="to" class="form-control form-control-sm" value="<?= htmlspecialchars($filters['to'] ?? '') ?>">
</div>
<div class="col-md-2">
<label class="form-label small">Zoeken</label>
<input type="text" name="search" class="form-control form-control-sm" placeholder="zoek in bericht..." value="<?= htmlspecialchars($filters['search'] ?? '') ?>">
</div>
<div class="col-12">
<button type="submit" class="btn btn-primary btn-sm"><i class="bi bi-search me-1"></i>Filteren</button>
<a href="?page=logs" class="btn btn-outline-secondary btn-sm"><i class="bi bi-x"></i>Wissen</a>
<small class="text-muted ms-2"><?= number_format($result['total'], 0, ',', '.') ?> resultaten (pagina <?= $result['page'] ?>/<?= $result['pages'] ?>)</small>
</div>
</form>
</div>
</div>
<div class="card shadow-sm">
<div class="card-body p-0">
<div class="table-responsive">
<table class="table table-sm table-hover mb-0" style="font-size:0.85rem;">
<thead class="table-light">
<tr>
<th><a href="<?= sortUrl('ReceivedAt', $filters, $filters['sort'] ?? 'DESC', $filters['sort_col'] ?? 'ReceivedAt') ?>" class="text-decoration-none text-dark">Ontvangen <?= ($filters['sort_col'] ?? 'ReceivedAt') === 'ReceivedAt' ? (($filters['sort'] ?? 'DESC') === 'ASC' ? '↑' : '↓') : '' ?></a></th>
<th><a href="<?= sortUrl('FromHost', $filters, $filters['sort'] ?? 'DESC', $filters['sort_col'] ?? 'ReceivedAt') ?>" class="text-decoration-none text-dark">Host <?= ($filters['sort_col'] ?? '') === 'FromHost' ? (($filters['sort'] ?? 'DESC') === 'ASC' ? '↑' : '↓') : '' ?></a></th>
<th>Facility</th>
<th>Severity</th>
<th>Tag</th>
<th>Bericht</th>
</tr>
</thead>
<tbody>
<?php foreach ($result['rows'] as $row): ?>
<tr>
<td class="text-nowrap small"><?= date('d-m-Y H:i:s', strtotime($row['ReceivedAt'])) ?></td>
<td class="text-nowrap">
<a href="?page=logs&host=<?= urlencode($row['FromHost']) ?>" class="text-decoration-none"><?= htmlspecialchars($row['FromHost']) ?></a>
</td>
<td><span class="badge bg-light text-dark"><?= SyslogQuery::facilityName((int)$row['Facility']) ?></span></td>
<td><span class="badge bg-<?= SyslogQuery::severityClass((int)$row['Priority']) ?>"><?= SyslogQuery::severityName((int)$row['Priority']) ?></span></td>
<td class="font-monospace small text-muted"><?= htmlspecialchars($row['SysLogTag'] ?? '') ?></td>
<td style="max-width:400px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;" title="<?= htmlspecialchars($row['Message']) ?>"><?= htmlspecialchars(mb_substr($row['Message'] ?? '', 0, 120)) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
<?php if ($result['pages'] > 1): ?>
<nav class="mt-3">
<ul class="pagination pagination-sm justify-content-center">
<li class="page-item <?= $result['page'] <= 1 ? 'disabled' : '' ?>">
<a class="page-link" href="<?= filterUrl($filters, ['p' => $result['page'] - 1]) ?>">Vorige</a>
</li>
<?php for ($i = 1; $i <= $result['pages']; $i++): ?>
<li class="page-item <?= $i === $result['page'] ? 'active' : '' ?>">
<a class="page-link" href="<?= filterUrl($filters, ['p' => $i]) ?>"><?= $i ?></a>
</li>
<?php endfor; ?>
<li class="page-item <?= $result['page'] >= $result['pages'] ? 'disabled' : '' ?>">
<a class="page-link" href="<?= filterUrl($filters, ['p' => $result['page'] + 1]) ?>">Volgende</a>
</li>
</ul>
</nav>
<?php endif; ?>
<?php $sq = null; ?>