Release
This commit is contained in:
33
web/api/stats.php
Normal file
33
web/api/stats.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
require __DIR__ . '/../lib/app.php';
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
$rows=[];
|
||||
$state='/var/lib/snowpanel/stats.json';
|
||||
if (is_file($state)) {
|
||||
$j=json_decode((string)@file_get_contents($state),true);
|
||||
if (is_array($j) && isset($j['data']) && is_array($j['data'])) $rows=$j['data'];
|
||||
}
|
||||
if (count($rows)<2) {
|
||||
$lines=[];
|
||||
$out=@shell_exec('sudo /usr/local/bin/snowpanel-logdump 20000 info 2>/dev/null');
|
||||
if (is_string($out) && $out!=='') $lines=preg_split('/\r?\n/',trim($out));
|
||||
else @exec('journalctl -u snowflake-proxy --since "48 hours ago" -o short-iso --no-pager 2>/dev/null',$lines);
|
||||
$cum_rx=0; $cum_tx=0; $tmp=[];
|
||||
foreach($lines as $line){
|
||||
if(!preg_match('~^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[+\-]\d{2}:\d{2})?).*Traffic\s+Relayed.*?([0-9]+)\s*KB\s*,\s*([0-9]+)\s*KB~i',$line,$m)) continue;
|
||||
$ts=strtotime($m[1]); if($ts===false) continue;
|
||||
$tx_kb=(int)$m[2]; $rx_kb=(int)$m[3];
|
||||
$cum_rx+=$rx_kb*1024; $cum_tx+=$tx_kb*1024;
|
||||
$tmp[]=['t'=>$ts,'read'=>$cum_rx,'written'=>$cum_tx];
|
||||
}
|
||||
if(!empty($tmp)) $rows=$tmp;
|
||||
}
|
||||
usort($rows,function($a,$b){return $a['t']<=>$b['t'];});
|
||||
if(count($rows)===1){
|
||||
$d0=$rows[0];
|
||||
array_unshift($rows,['t'=>$d0['t']-60,'read'=>$d0['read'],'written'=>$d0['written']]);
|
||||
}
|
||||
$cut=time()-172800;
|
||||
$rows=array_values(array_filter($rows,function($r)use($cut){return (int)$r['t']>=$cut;}));
|
||||
echo json_encode(['ok'=>true,'data'=>$rows],JSON_UNESCAPED_SLASHES);
|
||||
Reference in New Issue
Block a user