Files
weval-consulting/api/monitoring-dashboard.php

51 lines
2.1 KiB
PHP

<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
$start = microtime(true);
// Gather all metrics
$metrics = ['timestamp' => date('c'), 'services' => [], 'infra' => []];
// Services check
$checks = [
'site' => 'https://weval-consulting.com/',
'wevia' => 'https://weval-consulting.com/wevia',
'manager' => 'https://weval-consulting.com/ops-center.html',
'deerflow' => 'https://deerflow.weval-consulting.com/',
'crm' => 'https://crm.weval-consulting.com/',
'wevads' => 'https://weval-consulting.com/wevads-ia/',
'api-wevia' => 'https://weval-consulting.com/api/weval-ia',
'api-ethica' => 'https://weval-consulting.com/api/ethica-stats-api.php',
'ollama' => 'http://localhost:11435/',
'gateway' => 'http://localhost:8001/health',
'n8n' => 'http://localhost:5678/',
];
$up = 0;
foreach ($checks as $name => $url) {
$ch = curl_init($url);
curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER=>1, CURLOPT_TIMEOUT=>5, CURLOPT_SSL_VERIFYPEER=>0, CURLOPT_FOLLOWLOCATION=>1]);
curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$time = round(curl_getinfo($ch, CURLINFO_TOTAL_TIME) * 1000);
curl_close($ch);
$ok = in_array($code, [200, 302]);
if ($ok) $up++;
$metrics['services'][$name] = ['status' => $ok ? 'up' : 'down', 'code' => $code, 'ms' => $time];
}
// Infra
$metrics['infra']['disk'] = trim(shell_exec("df / --output=pcent | tail -1") ?: '?');
exec("free -m | grep Mem", $mem);
if ($mem) { $p = preg_split('/\s+/', trim($mem[0])); $metrics['infra']['ram_used'] = round(($p[2]??0)/1024,1).'G'; $metrics['infra']['ram_total'] = round(($p[1]??0)/1024,1).'G'; }
$oj = @file_get_contents('http://localhost:11435/api/tags');
$metrics['infra']['ollama_models'] = $oj ? count(json_decode($oj,true)['models'] ?? []) : 0;
$metrics['infra']['uptime'] = trim(shell_exec("uptime -p 2>/dev/null | sed 's/up //'") ?: '?');
// Summary
$total = count($checks);
$metrics['summary'] = ['up' => $up, 'total' => $total, 'pct' => round($up/$total*100), 'scan_ms' => round((microtime(true)-$start)*1000)];
echo json_encode($metrics, JSON_PRETTY_PRINT);