<?php
namespace App\Controller;
use App\Entity\TMA\VPSInfo;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Notifier\ChatterInterface;
use Symfony\Component\Notifier\Message\ChatMessage;
/**
* @Route("/crontabvps")
*/
class CronVPSController extends AbstractController
{
/**
* @Route("/ping-vps/757545575457575457", name="cron_pingvps")
*/
public function pingvps(EntityManagerInterface $entityManager, ChatterInterface $chatter)
{
//purge old backup-vps
var_dump($this->supprimerVieuxBackups());
$allVPS = $entityManager->getRepository(VPSInfo::class)->findBy(['paused' => [false, null]]);
$failVPS = '';
$sendError = false;
foreach ($allVPS as $vpsInfo) {
$failping = $this->pingOne($vpsInfo, $entityManager);
if ($failping) {
$sendError = true;
$failVPS .= $vpsInfo->getUrl().' ';
}
}
if ($sendError) {
$message = (new ChatMessage('<@Kelkii> VPS KO !! '.$failVPS))
->transport('slack');
$chatter->send($message);
}
exit;
}
public function pingOne(VPSInfo $vpsInfo, EntityManagerInterface $entityManager)
{
$useragent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/" . rand(90, 110) . '.' . rand(0, 99) . '.' . rand(0, 9999) . " Safari/537.36";
$headers = [
"Try" => "Trying",
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language: fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7',
'Connection: keep-alive',
'Cache-Control: max-age=0',
'Upgrade-Insecure-Requests: 1',
'User-Agent: ' . $useragent,
];
$username = $vpsInfo->getHtuser();
$password = $vpsInfo->getHtpassword();
$handle = curl_init('https://'.$vpsInfo->getUrl().'/?botping=vpsinfo');
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($handle, CURLOPT_TIMEOUT, 30);
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($handle, CURLOPT_VERBOSE, 1);
curl_setopt($handle, CURLOPT_USERAGENT, $useragent);
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($handle, CURLOPT_HEADER, true);
if ($username && $password) {
curl_setopt($handle, CURLOPT_USERPWD, "$username:$password");
curl_setopt($handle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
}
curl_exec($handle);
$infos = curl_getinfo($handle);
if (isset($infos['http_code']) && $infos['http_code'] == 200) {
$vpsInfo->setHs(false);
$failping = false;
} else {
$vpsInfo->setHs(true);
$failping = true;
}
if (isset($infos['total_time_us'])) {
$vpsInfo->setPingduration((int) $infos['total_time_us']);
}
$vpsInfo->setPingDate(new \DateTime());
$entityManager->persist($vpsInfo);
$entityManager->flush();
return $failping;
}
function supprimerVieuxBackups() {
$dir = __DIR__ . '/../../../vps_stats_depot/';
var_dump($dir);
if (!is_dir($dir)) {
echo "Le répertoire n'existe pas : " . $dir . "\n";
return [];
}
$seuil = strtotime('-6 months');
$fichiersSupprimes = [];
$elements = scandir($dir);
foreach ($elements as $element) {
// Ignorer '.' et '..'
if ($element === '.' || $element === '..') {
continue;
}
// Vérifier que le fichier commence par "backup-vps"
if (strpos($element, 'backup-vps') !== 0) {
continue;
}
$cheminComplet = $dir . $element;
// Ne traiter que les fichiers (pas les dossiers)
if (!is_file($cheminComplet)) {
continue;
}
// Vérifier la date de modification
$dateModif = filemtime($cheminComplet);
if ($dateModif !== false && $dateModif < $seuil) {
if (unlink($cheminComplet)) {
$fichiersSupprimes[] = $element;
} else {
echo "Échec de la suppression : $element\n";
}
}
}
return $fichiersSupprimes;
}
}