src/Controller/CronVPSController.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\TMA\VPSInfo;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Symfony\Component\Notifier\ChatterInterface;
  8. use Symfony\Component\Notifier\Message\ChatMessage;
  9. /**
  10.  * @Route("/crontabvps")
  11.  */
  12. class CronVPSController extends AbstractController
  13. {
  14.     /**
  15.      * @Route("/ping-vps/757545575457575457", name="cron_pingvps")
  16.      */
  17.     public function pingvps(EntityManagerInterface $entityManagerChatterInterface $chatter)
  18.     {
  19.         //purge old backup-vps
  20.         var_dump($this->supprimerVieuxBackups());
  21.         $allVPS $entityManager->getRepository(VPSInfo::class)->findBy(['paused' => [falsenull]]);
  22.         $failVPS '';
  23.         $sendError false;
  24.         foreach ($allVPS as $vpsInfo) {
  25.             $failping $this->pingOne($vpsInfo$entityManager);
  26.             if ($failping) {
  27.                 $sendError true;
  28.                 $failVPS .= $vpsInfo->getUrl().'  ';
  29.             }
  30.         }
  31.         if ($sendError) {
  32.             $message = (new ChatMessage('<@Kelkii> VPS KO !! '.$failVPS))
  33.                 ->transport('slack');
  34.             $chatter->send($message);
  35.         }
  36.         exit;
  37.     }
  38.     public function pingOne(VPSInfo $vpsInfoEntityManagerInterface $entityManager)
  39.     {
  40.         $useragent "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/" rand(90110) . '.' rand(099) . '.' rand(09999) . " Safari/537.36";
  41.         $headers = [
  42.             "Try" => "Trying",
  43.             'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  44.             'Accept-Language: fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7',
  45.             'Connection: keep-alive',
  46.             'Cache-Control: max-age=0',
  47.             'Upgrade-Insecure-Requests: 1',
  48.             'User-Agent: ' $useragent,
  49.         ];
  50.         $username $vpsInfo->getHtuser();
  51.         $password $vpsInfo->getHtpassword();
  52.         $handle curl_init('https://'.$vpsInfo->getUrl().'/?botping=vpsinfo');
  53.         curl_setopt($handleCURLOPT_RETURNTRANSFERTRUE);
  54.         curl_setopt($handleCURLOPT_TIMEOUT30);
  55.         curl_setopt($handleCURLOPT_FOLLOWLOCATIONTRUE);
  56.         curl_setopt($handleCURLOPT_SSL_VERIFYHOST0);
  57.         curl_setopt($handleCURLOPT_SSL_VERIFYPEER0);
  58.         curl_setopt($handleCURLOPT_VERBOSE1);
  59.         curl_setopt($handleCURLOPT_USERAGENT$useragent);
  60.         curl_setopt($handleCURLOPT_HTTPHEADER$headers);
  61.         curl_setopt($handleCURLOPT_HEADERtrue);
  62.         if ($username && $password) {
  63.             curl_setopt($handleCURLOPT_USERPWD"$username:$password");
  64.             curl_setopt($handleCURLOPT_HTTPAUTHCURLAUTH_BASIC);
  65.         }
  66.         curl_exec($handle);
  67.         $infos curl_getinfo($handle);
  68.         if (isset($infos['http_code']) && $infos['http_code'] == 200) {
  69.             $vpsInfo->setHs(false);
  70.             $failping false;
  71.         } else {
  72.             $vpsInfo->setHs(true);
  73.             $failping true;
  74.         }
  75.         if (isset($infos['total_time_us'])) {
  76.             $vpsInfo->setPingduration((int) $infos['total_time_us']);
  77.         }
  78.         $vpsInfo->setPingDate(new \DateTime());
  79.         $entityManager->persist($vpsInfo);
  80.         $entityManager->flush();
  81.         return $failping;
  82.     }
  83.     function supprimerVieuxBackups() {
  84.         $dir __DIR__ '/../../../vps_stats_depot/';
  85.         var_dump($dir);
  86.         if (!is_dir($dir)) {
  87.             echo "Le répertoire n'existe pas : " $dir "\n";
  88.             return [];
  89.         }
  90.         $seuil strtotime('-6 months');
  91.         $fichiersSupprimes = [];
  92.         $elements scandir($dir);
  93.         foreach ($elements as $element) {
  94.             // Ignorer '.' et '..'
  95.             if ($element === '.' || $element === '..') {
  96.                 continue;
  97.             }
  98.             // Vérifier que le fichier commence par "backup-vps"
  99.             if (strpos($element'backup-vps') !== 0) {
  100.                 continue;
  101.             }
  102.             $cheminComplet $dir $element;
  103.             // Ne traiter que les fichiers (pas les dossiers)
  104.             if (!is_file($cheminComplet)) {
  105.                 continue;
  106.             }
  107.             // Vérifier la date de modification
  108.             $dateModif filemtime($cheminComplet);
  109.             if ($dateModif !== false && $dateModif $seuil) {
  110.                 if (unlink($cheminComplet)) {
  111.                     $fichiersSupprimes[] = $element;
  112.                 } else {
  113.                     echo "Échec de la suppression : $element\n";
  114.                 }
  115.             }
  116.         }
  117.         return $fichiersSupprimes;
  118.     }
  119. }