Ignore:
Timestamp:
Dec 31, 2016, 12:19:25 AM (8 years ago)
Author:
chronos
Message:
  • Fixed: Use SQL transaction for updating network device online state.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Modules/NetworkConfigRouterOS/Generators/NetwatchImport.php

    r835 r841  
    1313      $Interfaces[$DbRow['IP']] = $DbRow;
    1414
    15       // Load netwatch status from all DHCP routers
    16       $DbResult3 = $this->Database->query('SELECT `DHCP`, `AddressRange`, `Mask` FROM `NetworkSubnet` '.
    17           'WHERE (`Configure` = 1) AND (`Member` IS NULL) GROUP BY DHCP');
    18       while($Subnet = $DbResult3->fetch_assoc())
     15    // Load netwatch status from all DHCP routers
     16    $DbResult3 = $this->Database->query('SELECT `DHCP` FROM `NetworkSubnet` '.
     17      'WHERE (`Configure` = 1) AND (`Member` IS NULL) GROUP BY `DHCP`');
     18    while($Subnet = $DbResult3->fetch_assoc())
     19    {
     20      echo('router '.$Subnet['DHCP']."\n");
     21      $Routerboard = new RouterosAPI();
     22      $Routerboard->Connect($Subnet['DHCP'], $this->System->Config['API']['UserName'],
     23        $this->System->Config['API']['Password']);
     24      if(!$Routerboard->Connected) continue;
     25      $Routerboard->Write('/tool/netwatch/getall', false);
     26      $Routerboard->Write('=.proplist=host,status');
     27      $Read = $Routerboard->Read(false);
     28      $List = $Routerboard->ParseResponse($Read);
     29      foreach($List as $Properties)
    1930      {
    20         echo($Subnet['AddressRange'].'/'.$Subnet['Mask'].' on router '.$Subnet['DHCP']."\n");
    21         $Routerboard = new RouterosAPI();
    22         $Routerboard->Connect($Subnet['DHCP'], $this->System->Config['API']['UserName'],
    23           $this->System->Config['API']['Password']);
    24         if(!$Routerboard->Connected) continue;
    25         $Routerboard->Write('/tool/netwatch/getall', false);
    26         $Routerboard->Write('=.proplist=host,status');
    27         $Read = $Routerboard->Read(false);
    28         $List = $Routerboard->ParseResponse($Read);
    29         foreach($List as $Properties)
    30         {
    31           $IP = $Properties['host'];
    32           if($Properties['status'] == 'up') $Online = 1;
     31        $IP = $Properties['host'];
     32        if($Properties['status'] == 'up') $Online = 1;
    3333          else $Online = 0;
    3434
    35           if($Online)
    36           {
    37             if(array_key_exists($IP, $Interfaces))
    38               $Interfaces[$IP]['NewOnline'] = 1;
    39               else echo('IP '.$IP.' not found.'."\n");
    40           }
     35        if($Online)
     36        {
     37          if(array_key_exists($IP, $Interfaces))
     38            $Interfaces[$IP]['NewOnline'] = 1;
     39            else echo('IP '.$IP.' not found.'."\n");
    4140        }
    4241      }
     42    }
    4343
    44       foreach($Interfaces as $Index => $Interface)
     44    $this->Database->query('BEGIN');
     45
     46    foreach($Interfaces as $Index => $Interface)
     47    {
     48      // Update last online time if still online
     49      if($Interface['NewOnline'])
     50        $DbResult = $this->Database->update('NetworkInterface', '`Id` = "'.$Interface['Id'].'"',
     51          array('LastOnline' => TimeToMysqlDateTime($StartTime)));
     52
     53      if($Interface['Online'] != $Interface['NewOnline'])
    4554      {
    46         // Update last online time if still online
    47         if($Interface['NewOnline'])
    48           $DbResult = $this->Database->update('NetworkInterface', '`Id` = "'.$Interface['Id'].'"',
    49               array('LastOnline' => TimeToMysqlDateTime($StartTime)));
     55        // Online state changed
     56        $DbResult = $this->Database->query('INSERT INTO `NetworkInterfaceUpDown` (`Interface`,
     57         `State`, `Time`, `Duration`) VALUES ('.$Interface['Id'].', '.$Interface['NewOnline'].', "'.
     58          TimeToMysqlDateTime($StartTime).'", NULL)');
     59        // Update previous record duration in UpDown table
     60        $this->Database->query('UPDATE `NetworkInterfaceUpDown` AS `TM` SET `Duration` = TIMESTAMPDIFF(SECOND, '.
     61          '`TM`.`Time`, (SELECT `Time` FROM (SELECT * FROM `NetworkInterfaceUpDown`) AS `TA` WHERE (`TA`.`Time` > `TM`.`Time`) '.
     62          'AND (`TA`.`Interface`=`TM`.`Interface`) ORDER BY `TA`.`Time` ASC LIMIT 1)) '.
     63          'WHERE (`TM`.`Duration` IS NULL) AND (`TM`.`Interface` ='.$Interface['Id'].')');
     64        $this->Database->update('NetworkInterface', '`Id` = "'.$Interface['Id'].'"',
     65          array('Online' => $Interface['NewOnline']));
     66      }
     67    }
    5068
    51         if($Interface['Online'] != $Interface['NewOnline'])
    52         {
    53           // Online state changed
    54           $DbResult = $this->Database->query('INSERT INTO `NetworkInterfaceUpDown` (`Interface`,
    55            `State`, `Time`, `Duration`) VALUES ('.$Interface['Id'].', '.$Interface['NewOnline'].', "'.
    56             TimeToMysqlDateTime($StartTime).'", NULL)');
    57           // Update previous record duration in UpDown table
    58           $this->Database->query('UPDATE `NetworkInterfaceUpDown` AS `TM` SET `Duration` = TIMESTAMPDIFF(SECOND, '.
    59             '`TM`.`Time`, (SELECT `Time` FROM (SELECT * FROM `NetworkInterfaceUpDown`) AS `TA` WHERE (`TA`.`Time` > `TM`.`Time`) '.
    60             'AND (`TA`.`Interface`=`TM`.`Interface`) ORDER BY `TA`.`Time` ASC LIMIT 1)) '.
    61             'WHERE (`TM`.`Duration` IS NULL) AND (`TM`.`Interface` ='.$Interface['Id'].')');
    62           $this->Database->update('NetworkInterface', '`Id` = "'.$Interface['Id'].'"',
    63             array('Online' => $Interface['NewOnline']));
    64         }
    65       }
     69    // Set offline all interfaces which were not updated as online
     70    $DbResult = $this->Database->select('NetworkInterface', '*', '(`Online` = 1) AND '.
     71      '(`LastOnline` < "'.TimeToMysqlDateTime($StartTime).'")');
     72    while($DbRow = $DbResult->fetch_assoc())
     73    {
     74      echo('IP '.$DbRow['LocalIP'].' online but time not updated.'."\n");
     75    }
     76    $DbResult = $this->Database->select('NetworkInterface', '*', '(`Online` = 0) AND '.
     77      '(`LastOnline` >= "'.TimeToMysqlDateTime($StartTime).'")');
     78    while($DbRow = $DbResult->fetch_assoc())
     79    {
     80      echo('IP '.$DbRow['LocalIP'].' not online but time updated.'."\n");
     81    }
    6682
    67       // Set offline all interfaces which were not updated as online
    68       $DbResult = $this->Database->select('NetworkInterface', '*', '(`Online` = 1) AND '.
    69           '(`LastOnline` < "'.TimeToMysqlDateTime($StartTime).'")');
    70       while($DbRow = $DbResult->fetch_assoc())
    71       {
    72         echo('IP '.$DbRow['LocalIP'].' online but time not updated.'."\n");
    73       }
    74       $DbResult = $this->Database->select('NetworkInterface', '*', '(`Online` = 0) AND '.
    75           '(`LastOnline` >= "'.TimeToMysqlDateTime($StartTime).'")');
    76       while($DbRow = $DbResult->fetch_assoc())
    77       {
    78         echo('IP '.$DbRow['LocalIP'].' not online but time updated.'."\n");
    79       }
     83    // Update device online state
     84    $DbResult = $this->Database->select('NetworkInterface', '`Device`, SUM(`Online`) AS `SumOnline`', '`Online` = 1 GROUP BY `Device`');
     85    while($Device = $DbResult->fetch_assoc())
     86    {
     87      if($Device['SumOnline'] > 0)
     88        $this->Database->update('NetworkDevice', 'Id='.$Device['Device'], array('LastOnline' => TimeToMysqlDateTime($StartTime), 'Online' => 1));
     89    }
     90    $this->Database->update('NetworkDevice', '`LastOnline` < "'.TimeToMysqlDateTime($StartTime).'"', array('Online' => 0));
    8091
    81       // Update device online state
    82       $DbResult = $this->Database->select('NetworkInterface', '`Device`, SUM(`Online`) AS `SumOnline`', '`Online` = 1 GROUP BY `Device`');
    83       while($Device = $DbResult->fetch_assoc())
    84       {
    85         if($Device['SumOnline'] > 0)
    86           $this->Database->update('NetworkDevice', 'Id='.$Device['Device'], array('LastOnline' => TimeToMysqlDateTime($StartTime), 'Online' => 1));
    87       }
    88       $DbResult = $this->Database->update('NetworkDevice', '`LastOnline` < "'.TimeToMysqlDateTime($StartTime).'"', array('Online' => 0));
     92    $this->Database->query('COMMIT');
    8993  }
    9094
Note: See TracChangeset for help on using the changeset viewer.