Files
html/api/wevia-master-autonomous.php
2026-04-16 02:28:32 +02:00

75 lines
3.7 KiB
PHP

<?php
header('Content-Type: application/json');
$start=microtime(true);
$results=['ts'=>date('c'),'actions'=>[],'fixes'=>[],'status'=>'running'];
// 1. SELF-DIAGNOSE: check all critical services
$checks=[
['name'=>'Chatbot','url'=>'https://weval-consulting.com/api/weval-chatbot-api.php','expect'=>200],
['name'=>'Master','url'=>'https://weval-consulting.com/api/wevia-master-api.php?health','expect'=>200],
['name'=>'Director','url'=>'https://weval-consulting.com/api/wevia-director.php?health','expect'=>200],
['name'=>'Ecosystem','url'=>'https://weval-consulting.com/api/ecosystem-health.php','expect'=>200],
['name'=>'NonReg','url'=>'https://weval-consulting.com/api/nonreg-api.php?cat=all','expect'=>200],
['name'=>'Blog','url'=>'https://weval-consulting.com/api/actualites/index.php','expect'=>200],
['name'=>'OSS','url'=>'https://weval-consulting.com/api/oss-cache.json','expect'=>200],
['name'=>'Brain','url'=>'https://weval-consulting.com/api/wevia-master-brain.json','expect'=>200],
['name'=>'Ollama','url'=>'http://127.0.0.1:11434/api/version','expect'=>200],
['name'=>'Qdrant','url'=>'http://127.0.0.1:6333/collections','expect'=>200],
['name'=>'DeerFlow','url'=>'http://127.0.0.1:2024/ok','expect'=>200],
['name'=>'MiroFish','url'=>'http://127.0.0.1:5001/health','expect'=>200],
['name'=>'Plausible','url'=>'http://127.0.0.1:8000','expect'=>200],
['name'=>'SearXNG','url'=>'http://127.0.0.1:8080','expect'=>200],
['name'=>'Mattermost','url'=>'http://127.0.0.1:8065','expect'=>200],
['name'=>'Kuma','url'=>'http://127.0.0.1:3088','expect'=>200],
];
$pass=$fail=$fixed=0;
foreach($checks as $c){
$ch=curl_init($c['url']);
curl_setopt_array($ch,[CURLOPT_RETURNTRANSFER=>1,CURLOPT_TIMEOUT=>5,CURLOPT_SSL_VERIFYPEER=>0,CURLOPT_FOLLOWLOCATION=>1]);
$body=curl_exec($ch);$code=curl_getinfo($ch,CURLINFO_HTTP_CODE);curl_close($ch);
$ok=in_array($code,[200,301,302]);
if($ok){$pass++;}
else{
$fail++;
$results['actions'][]=['service'=>$c['name'],'code'=>$code,'action'=>'DOWN'];
// 2. SELF-FIX: restart known services
$restarts=[
'DeerFlow'=>'/opt/wevia-brain/deerflow-start.sh',
'Paperclip'=>'/opt/wevia-brain/paperclip-start.sh',
];
if(isset($restarts[$c['name']])){
exec($restarts[$c['name']].' 2>&1',$out);
$results['fixes'][]=['service'=>$c['name'],'script'=>$restarts[$c['name']]];
$fixed++;
}
}
}
// 3. SELF-TEST: chatbot functional test
$chatTest=curl_init('https://weval-consulting.com/api/weval-ia');
curl_setopt_array($chatTest,[CURLOPT_POST=>1,CURLOPT_POSTFIELDS=>json_encode(['message'=>'test','language'=>'fr','widget'=>true]),CURLOPT_HTTPHEADER=>['Content-Type: application/json'],CURLOPT_RETURNTRANSFER=>1,CURLOPT_TIMEOUT=>25,CURLOPT_SSL_VERIFYPEER=>0]);
$chatR=curl_exec($chatTest);curl_close($chatTest);
$chatD=json_decode($chatR,true);
$chatOk=!empty($chatR);
$results['chatbot_functional']=$chatOk;
if($chatOk)$pass++;else $fail++;
// 4. SELF-REPORT: disk, docker, crons
$results['disk']=trim(shell_exec("df / --output=pcent|tail -1"));
$results['docker']=(int)trim(shell_exec("docker ps -q|wc -l"));
$results['summary']=['pass'=>$pass,'fail'=>$fail,'fixed'=>$fixed,'total'=>$pass+$fail];
$results['score']=round($pass/($pass+$fail)*100,1);
$results['elapsed_ms']=round((microtime(true)-$start)*1000);
// 5. TELEGRAM ALERT if fails > 2
if($fail>2){
$msg="WEVIA MASTER ALERT: {$fail} services DOWN. Score: {$results['score']}%";
@file_get_contents("https://api.telegram.org/bot8544624912/sendMessage?chat_id=7605775322&text=".urlencode($msg));
$results['telegram']='alerted';
}
$results['status']='complete';
echo json_encode($results,JSON_PRETTY_PRINT);