네트워크 이력 DB 저장 전환

This commit is contained in:
seo
2026-07-25 22:28:58 +09:00
parent bc89dcf9c0
commit 30c0cb4663
4 changed files with 187 additions and 74 deletions
+151 -1
View File
@@ -996,6 +996,152 @@ function network_info(): array
];
}
function primary_network_interface(array $network): ?array
{
$rows = is_array($network['interfaces'] ?? null) ? $network['interfaces'] : [];
$preferred = (string)($network['default_interface'] ?? 'eth0');
foreach ($rows as $row) {
if (($row['name'] ?? '') === $preferred) {
return $row;
}
}
foreach ($rows as $row) {
if (($row['name'] ?? '') === 'eth0') {
return $row;
}
}
foreach ($rows as $row) {
if (($row['carrier'] ?? '') === 'up' && ($row['name'] ?? '') !== 'br0') {
return $row;
}
}
return $rows[0] ?? null;
}
function add_network_usage_log(array $network): void
{
$iface = primary_network_interface($network);
if (!$iface) {
return;
}
$name = (string)($iface['name'] ?? '');
$rxBytes = isset($iface['rx_bytes']) ? (int)$iface['rx_bytes'] : -1;
$txBytes = isset($iface['tx_bytes']) ? (int)$iface['tx_bytes'] : -1;
$rxPackets = isset($iface['rx_packets']) ? (int)$iface['rx_packets'] : -1;
$txPackets = isset($iface['tx_packets']) ? (int)$iface['tx_packets'] : -1;
if ($name === '' || $rxBytes < 0 || $txBytes < 0 || $rxPackets < 0 || $txPackets < 0) {
return;
}
$pdo = db();
$stmt = $pdo->prepare("
SELECT
recorded_at,
rx_bytes,
tx_bytes,
rx_packets,
tx_packets,
TIMESTAMPDIFF(MICROSECOND, recorded_at, NOW(3)) AS elapsed_us
FROM network_usage_logs
WHERE iface = :iface
ORDER BY id DESC
LIMIT 1
");
$stmt->execute([':iface' => $name]);
$previous = $stmt->fetch() ?: null;
$rxMbps = null;
$txMbps = null;
$rxPps = null;
$txPps = null;
if ($previous) {
$seconds = max(0.0, (float)($previous['elapsed_us'] ?? 0) / 1000000);
if ($seconds < 0.8) {
return;
}
$rxDelta = $rxBytes - (int)$previous['rx_bytes'];
$txDelta = $txBytes - (int)$previous['tx_bytes'];
$rxPacketDelta = $rxPackets - (int)$previous['rx_packets'];
$txPacketDelta = $txPackets - (int)$previous['tx_packets'];
if ($seconds > 0 && $seconds <= 60 && $rxDelta >= 0 && $txDelta >= 0 && $rxPacketDelta >= 0 && $txPacketDelta >= 0) {
$rxMbps = round(($rxDelta * 8) / $seconds / 1000000, 4);
$txMbps = round(($txDelta * 8) / $seconds / 1000000, 4);
$rxPps = round($rxPacketDelta / $seconds, 2);
$txPps = round($txPacketDelta / $seconds, 2);
}
}
$stmt = $pdo->prepare("
INSERT INTO network_usage_logs
(
iface,
rx_bytes,
tx_bytes,
rx_packets,
tx_packets,
rx_mbps,
tx_mbps,
rx_pps,
tx_pps,
create_ip
)
VALUES
(
:iface,
:rx_bytes,
:tx_bytes,
:rx_packets,
:tx_packets,
:rx_mbps,
:tx_mbps,
:rx_pps,
:tx_pps,
:create_ip
)
");
$stmt->execute([
':iface' => $name,
':rx_bytes' => $rxBytes,
':tx_bytes' => $txBytes,
':rx_packets' => $rxPackets,
':tx_packets' => $txPackets,
':rx_mbps' => $rxMbps,
':tx_mbps' => $txMbps,
':rx_pps' => $rxPps,
':tx_pps' => $txPps,
':create_ip' => $_SERVER['REMOTE_ADDR'] ?? 'cli',
]);
}
function network_usage_history(int $limit = 240): array
{
$limit = max(1, min(1500, $limit));
$stmt = db()->query("
SELECT
recorded_at AS time,
iface,
rx_mbps,
tx_mbps,
rx_pps,
tx_pps
FROM network_usage_logs
WHERE rx_mbps IS NOT NULL
AND tx_mbps IS NOT NULL
AND rx_pps IS NOT NULL
AND tx_pps IS NOT NULL
ORDER BY id DESC
LIMIT {$limit}
");
return array_reverse($stmt->fetchAll());
}
function latest_sensor(): array
{
$stmt = db()->query("
@@ -4382,6 +4528,9 @@ function collect_snapshot(bool $applyFan = true): array
$processes = process_resource_data((int)setting_value('display.process_limit'));
$fanSpike = fan_spike_analysis($history, $fan, $system, $processes);
$network = network_info();
add_network_usage_log($network);
$networkHistory = network_usage_history(240);
$shouldLogNotice = !empty($fanSpike['alert_started'])
|| (!empty($fanSpike['active']) && (bool)setting_value('notice.log_during_alert'));
@@ -4402,7 +4551,8 @@ function collect_snapshot(bool $applyFan = true): array
'battery' => $battery,
'wifi' => wifi_data(),
'network' => network_info(),
'network' => $network,
'network_history' => $networkHistory,
'history' => $history,
'processes' => $processes,
'custom_services' => custom_systemd_services(),