<?php
///////////////////////////////////////////////////
//2010 (C)
//Карпухин Павел
///////////////////////////////////////////////////

require_once "/var/www/manydhcp/config_a.php";
require_once "/var/www/manydhcp/utils/functions.php";

//*************************************
$ip = "127.0.0.1";
$doing = "Запущена проверка";
$who = "Robot";
//*************************************

$query_srvlog = "SELECT name, descr, vkl, ping_ok FROM tbl_srv WHERE vkl = '1' AND ping_ok = 'no' ORDER BY name ";
 $res_srvlog = mysqli_query($dbcnx,$query_srvlog);
   while($result_srvlog = mysqli_fetch_array($res_srvlog,MYSQLI_ASSOC))
    {
      if($result_srvlog['name'] !== "") {$srvlog .= $result_srvlog['name']."(".$result_srvlog['descr']."), ";}
    }

 if(mysqli_numrows($res_srvlog)>0) {$what = "Проверка доступности ip-адресов<br />(исключения - ".substr($srvlog,0,-2).")";}
   else {$what = "Проверка доступности ip-адресов";}

$querylog = mysqli_query($dbcnx,"INSERT INTO tbl_logs ( `log_id` , `data` , `ip` , `who` , `doing` , `what` ) VALUES ( NULL , NOW() , '$ip' , '$who' , '$doing' , '$what')");
//***************************************

$delimiter = ".";
echo `clear`;
echo "Utilites for ping ip-adresses of project ManyDHCP starting\r\n\r\n";

$query_ip = "SELECT tbl_ip.*, tbl_srv.vkl, tbl_srv.ping_ok
    FROM tbl_ip
    JOIN tbl_srv ON tbl_ip.srv_id=tbl_srv.srv_id
    WHERE tbl_ip.vkl_ip ='1' AND tbl_srv.vkl = '1' AND tbl_srv.ping_ok = 'yes' ORDER BY tbl_ip.ip_addr ";
 $res_ip = mysqli_query($dbcnx,$query_ip);
  if(!$res_ip) {exit();}
   while($result_ip = mysqli_fetch_array($res_ip,MYSQLI_ASSOC))
    {
    $addr = explode($delimiter,$result_ip['ip_addr'],4);
    $oct1 = substr($addr['0'],0,1);
     if($oct1 == '0')
      {
       $oct_res1 = substr($addr['0'],1,1);
        if($oct_res1 == '0')
         {
          $oct_result1 = substr($addr['0'],2,3);
         } else {$oct_result1 = substr($addr['0'],1,3);}
      } else {$oct_result1 = $addr['0'];}

    $oct2 = substr($addr['1'],0,1);
     if($oct2 == '0')
      {
       $oct_res2 = substr($addr['1'],1,1);
        if($oct_res2 == '0')
         {
          $oct_result2 = substr($addr['1'],2,3);
         } else {$oct_result2 = substr($addr['1'],1,3);}
      } else {$oct_result2 = $addr['1'];}

    $oct3 = substr($addr['2'],0,1);
     if($oct3 == '0')
      {
       $oct_res3 = substr($addr['2'],1,1);
        if($oct_res3 == '0')
         {
          $oct_result3 = substr($addr['2'],2,3);
         } else {$oct_result3 = substr($addr['2'],1,3);}
      } else {$oct_result3 = $addr['2'];}

     $oct4 = substr($addr['3'],0,1);
      if($oct4 == '0')
       {
        $oct_res4 = substr($addr['3'],1,1);
         if($oct_res4 == '0')
          {
           $oct_result4 = substr($addr['3'],2,3);
          } else {$oct_result4 = substr($addr['3'],1,3);}
        } else {$oct_result4 = $addr['3'];}
     $ip_addres = $oct_result1.".".$oct_result2.".".$oct_result3.".".$oct_result4;

    $check_ip = exec("ping -c 1 -i 0.2 ".$ip_addres, $errno, $status);
    if($status == 0)
     {
     $status = "\033[32m\033[1mOK\033[0m\033[0m";
     $query_on = "UPDATE tbl_ip SET state_addr='0', data = CURRENT_DATE(), last_acc = CURRENT_DATE() WHERE ip_id = ".$result_ip['ip_id']." AND state_addr='1'";
      $res_on = mysqli_query($dbcnx,$query_on);
       if(!$res_on) {exit();}
     }
     else
      {
      $status = "\033[33m\033[1mOffline\033[0m\033[0m";
      $query_off = "UPDATE tbl_ip SET state_addr='1', data = CURRENT_DATE() WHERE ip_id = ".$result_ip['ip_id']." AND state_addr='0'";
       $res_off = mysqli_query($dbcnx,$query_off);
        if(!$res_off) {exit();}
      }
    echo $ip_addres." - ".$status."\r\n";
    }
    echo "\r\n\033[32m\033[1mOperation complete.\033[0m\033[0m\r\n";
?>