48 lines
1.6 KiB
PHP
48 lines
1.6 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
header('Access-Control-Allow-Origin: *');
|
|
$d = [];
|
|
|
|
// PHP Sessions
|
|
$sp = session_save_path() ?: '/var/lib/php/sessions';
|
|
$sf = glob($sp.'/sess_*');
|
|
$d['php_sessions'] = count($sf);
|
|
$active = 0;
|
|
foreach($sf as $f) { if(time()-filemtime($f)<1800) $active++; }
|
|
$d['active_sessions'] = $active;
|
|
|
|
// Nginx access log last 200 lines
|
|
$log = '/var/log/nginx/access.log';
|
|
$lines = file_exists($log) ? array_slice(file($log), max(0, count(file($log))-200)) : [];
|
|
$ips = []; $pages = []; $codes = []; $timeline = []; $methods = [];
|
|
foreach($lines as $l) {
|
|
if(preg_match('/^(\S+).*\[([^\]]+)\]\s+"(\S+)\s+(\S+)[^"]*"\s+(\d+)\s+(\d+)/', $l, $m)) {
|
|
$ips[$m[1]] = ($ips[$m[1]]??0)+1;
|
|
$pages[$m[4]] = ($pages[$m[4]]??0)+1;
|
|
$codes[$m[5]] = ($codes[$m[5]]??0)+1;
|
|
$methods[$m[3]] = ($methods[$m[3]]??0)+1;
|
|
$h = substr($m[2],12,5);
|
|
$timeline[$h] = ($timeline[$h]??0)+1;
|
|
}
|
|
}
|
|
arsort($ips); arsort($pages); ksort($timeline);
|
|
$d['unique_ips'] = count($ips);
|
|
$d['top_ips'] = array_slice($ips, 0, 10, true);
|
|
$d['top_pages'] = array_slice($pages, 0, 20, true);
|
|
$d['status_codes'] = $codes;
|
|
$d['methods'] = $methods;
|
|
$d['timeline'] = $timeline;
|
|
$d['total_requests'] = count($lines);
|
|
|
|
// SHM cache
|
|
$d['shm_cache_files'] = count(glob('/dev/shm/wevia_*'));
|
|
|
|
// System
|
|
$d['uptime'] = trim(shell_exec('uptime -p 2>/dev/null'));
|
|
$d['load'] = sys_getloadavg();
|
|
$d['memory_mb'] = round(memory_get_usage(true)/1048576, 1);
|
|
$d['disk_pct'] = trim(shell_exec("df / --output=pcent | tail -1"));
|
|
$d['timestamp'] = date('c');
|
|
|
|
echo json_encode($d, JSON_PRETTY_PRINT);
|