55 lines
1.5 KiB
PHP
55 lines
1.5 KiB
PHP
<?php
|
|
require __DIR__ . '/../lib/app.php';
|
|
auth_require();
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
$cfg = app_load_config();
|
|
$cap_gb = (int)($cfg['cap_gb'] ?? 0);
|
|
$cap_reset_day = (int)($cfg['cap_reset_day'] ?? 1);
|
|
$rate_mbps = (int)($cfg['rate_mbps'] ?? 0);
|
|
|
|
$stateDir = '/var/lib/snowpanel';
|
|
$statsFile = $stateDir . '/stats.json';
|
|
$metaFile = $stateDir . '/meta.json';
|
|
|
|
$usage = [
|
|
'start_ts' => null,
|
|
'period_label' => null,
|
|
'rx' => 0, 'tx' => 0, 'total' => 0,
|
|
'cap_bytes' => $cap_gb > 0 ? $cap_gb * 1024*1024*1024 : 0,
|
|
'cap_hit' => false,
|
|
];
|
|
|
|
if (is_file($metaFile)) {
|
|
$m = json_decode((string)file_get_contents($metaFile), true);
|
|
if (is_array($m)) {
|
|
foreach (['start_ts','period_label','rx','tx','total','cap_bytes','cap_hit'] as $k) {
|
|
if (array_key_exists($k, $m)) $usage[$k] = $m[$k];
|
|
}
|
|
}
|
|
}
|
|
|
|
$current_rate_mbps = 0.0;
|
|
if (is_file($statsFile)) {
|
|
$s = json_decode((string)file_get_contents($statsFile), true);
|
|
$arr = is_array($s) ? ($s['data'] ?? []) : [];
|
|
$n = count($arr);
|
|
if ($n >= 2) {
|
|
$a = $arr[$n-2]; $b = $arr[$n-1];
|
|
$dt = max(1, (int)$b['t'] - (int)$a['t']);
|
|
$dr = max(0, (int)$b['read'] - (int)$a['read']);
|
|
$dw = max(0, (int)$b['written'] - (int)$a['written']);
|
|
$current_rate_mbps = (($dr + $dw) * 8.0) / $dt / 1_000_000.0;
|
|
}
|
|
}
|
|
|
|
echo json_encode([
|
|
'ok' => true,
|
|
'cap_gb' => $cap_gb,
|
|
'cap_reset_day' => $cap_reset_day,
|
|
'rate_mbps' => $rate_mbps,
|
|
'usage' => $usage,
|
|
'current_rate_mbps' => $current_rate_mbps
|
|
]);
|