$_v54_load, 'disk' => $_v54_disk_pct, 'fpm' => $_v54_fpm]; @file_put_contents($ts_file, time() . ',' . $_v54_map[$metric] . chr(10), FILE_APPEND); $lines = array_slice(file($ts_file), -max(10, $window)); $pts = []; foreach ($lines as $l) { $parts = explode(',', trim($l)); if (count($parts) >= 2) $pts[] = ['x'=>(float)$parts[0], 'y'=>(float)$parts[1]]; } $n = count($pts); if ($n < 5) { echo json_encode(['ok'=>false, 'error'=>'insufficient_data', 'n'=>$n]); exit; } // Linear regression y = a + b*x $sx = $sy = $sxy = $sxx = 0; foreach ($pts as $p) { $sx += $p['x']; $sy += $p['y']; $sxy += $p['x']*$p['y']; $sxx += $p['x']*$p['x']; } $b = ($n*$sxy - $sx*$sy) / ($n*$sxx - $sx*$sx + 0.0001); $a = ($sy - $b*$sx) / $n; // Predict next hour $next_hour = time() + 3600; $predicted = $a + $b * $next_hour; // Alert thresholds $thresholds = ['load'=>5.0, 'disk'=>85, 'fpm'=>150]; $alert = $predicted >= ($thresholds[$metric] ?? 999); $actions = []; if ($alert) { if ($metric === 'load') $actions[] = 'systemctl restart php-fpm'; if ($metric === 'disk') $actions[] = 'find /tmp -mmin +60 -delete; docker image prune -f'; if ($metric === 'fpm') $actions[] = 'systemctl reload php-fpm'; } echo json_encode([ 'ok'=>true, 'metric'=>$metric, 'n_samples'=>$n, 'regression'=>['slope'=>$b, 'intercept'=>$a], 'predicted_next_hour'=>round($predicted, 2), 'threshold'=>$thresholds[$metric] ?? null, 'alert'=>$alert, 'recommended_actions'=>$actions, ]);