first commit
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
use SyslogDash\SyslogQuery;
|
||||
use SyslogDash\GeoIP;
|
||||
|
||||
$sq = new SyslogQuery();
|
||||
$format = $_GET['format'] ?? 'csv';
|
||||
$period = (int)($_GET['period'] ?? 30);
|
||||
if (!in_array($period, [7, 30, 90, 365, 0])) $period = 30;
|
||||
$report = $_GET['report'] ?? 'logs';
|
||||
|
||||
$filename = 'syslog-export-' . date('Ymd') . '.' . $format;
|
||||
|
||||
switch ($report) {
|
||||
case 'logs':
|
||||
$filters = [];
|
||||
foreach (['host','country','tag','facility','priority','search','from','to'] as $k) {
|
||||
if (!empty($_GET[$k])) $filters[$k] = $_GET[$k];
|
||||
}
|
||||
$filters['sort'] = 'ASC';
|
||||
$result = $sq->getLogs($filters, 1, 100000);
|
||||
$data = $result['rows'];
|
||||
$fields = ['ID','ReceivedAt','FromHost','Facility','Priority','SysLogTag','Message'];
|
||||
break;
|
||||
|
||||
case 'daily':
|
||||
$data = $sq->getDailyStats($period);
|
||||
$fields = ['Datum','Berichten','Hosts'];
|
||||
break;
|
||||
|
||||
case 'hosts':
|
||||
$data = $sq->getHostStats($period);
|
||||
$fields = ['Host','Aantal'];
|
||||
break;
|
||||
|
||||
case 'countries':
|
||||
$data = $sq->getCountryStats($period);
|
||||
$fields = ['Code','Aantal'];
|
||||
break;
|
||||
|
||||
case 'severity':
|
||||
$data = $sq->getSeverityStats($period);
|
||||
$fields = ['Priority','Severity','Aantal'];
|
||||
array_walk($data, fn(&$r) => $r['SeverityName'] = SyslogQuery::severityName((int)$r['Priority']));
|
||||
$fields = ['Priority','SeverityName','Aantal'];
|
||||
break;
|
||||
|
||||
default:
|
||||
$data = [];
|
||||
$fields = [];
|
||||
}
|
||||
|
||||
if ($format === 'json') {
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
header("Content-Disposition: attachment; filename=\"{$filename}\"");
|
||||
echo json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
|
||||
header('Content-Type: text/csv; charset=utf-8');
|
||||
header("Content-Disposition: attachment; filename=\"{$filename}\"");
|
||||
echo "\xEF\xBB\xBF"; // BOM for Excel UTF-8
|
||||
|
||||
$out = fopen('php://output', 'w');
|
||||
fputcsv($out, $fields, ';');
|
||||
foreach ($data as $row) {
|
||||
$line = [];
|
||||
foreach ($fields as $f) {
|
||||
$line[] = $row[$f] ?? '';
|
||||
}
|
||||
fputcsv($out, $line, ';');
|
||||
}
|
||||
fclose($out);
|
||||
Reference in New Issue
Block a user