Ignore:
Timestamp:
Jul 14, 2014, 10:15:00 PM (10 years ago)
Author:
chronos
Message:
  • Opraveno: Skript pro import online stavu rozhraní a záznam změn.
File:
1 edited

Legend:

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

    r663 r667  
    1010$Period = 60; // every 60 seconds
    1111
     12
    1213while(1)
    1314{
    1415  $StartTime = time();
     16
     17  // Load all interfaces to memory
     18  $Interfaces = array();
     19  $DbResult = $System->Database->select('NetworkInterface', '`Id`, `LocalIP` AS `IP`, `Online`, 0 AS `NewOnline`');
     20  while($DbRow = $DbResult->fetch_assoc())
     21    $Interfaces[$DbRow['IP']] = $DbRow;
    1522
    1623  // Load netwatch status from all DHCP routers
     
    2734    foreach($List as $Properties)
    2835    {
     36      $IP = $Properties['host'];
    2937      if($Properties['status'] == 'up') $Online = 1;
    3038        else $Online = 0;
     
    3240      if($Online)
    3341      {
    34         // Set only online state
    35         $DbResult = $System->Database->select('NetworkInterface', '`Id`, `Online`', '`LocalIP` = "'.$Properties['host'].'";');
    36         while($DbRow = $DbResult->fetch_assoc())
    37         {
    38           $LastOnline = $DbRow['Online'];
    39           $Interface = $DbRow['Id'];
    40 
    41           // Record state changes
    42           if($Online != $LastOnline)
    43           {
    44             $System->Database->insert('NetworkInterfaceUpDown', array(
    45               'Interface' => $Interface, 'State' => $Online, 'Time' => TimeToMysqlDateTime($StartTime)));
    46           }
    47         }
    48 
    49         $DbResult = $System->Database->update('NetworkInterface', '`LocalIP` = "'.$Properties['host'].'"',
    50           array('Online' => 1, 'LastOnline' => TimeToMysqlDateTime($StartTime)));
     42        if(array_key_exists($IP, $Interfaces))
     43          $Interfaces[$IP]['NewOnline'] = 1;
     44          else echo('IP '.$IP.' not found.'."\n");
    5145      }
    5246    }
    5347  }
    5448
    55   // Set offline state for not updated interfaces
    56   $DbResult = $System->Database->select('NetworkInterface', 'Id', '`LastOnline` < "'.TimeToMysqlDateTime($StartTime).'"');
     49  foreach($Interfaces as $Index => $Interface)
     50  {
     51    // Update last online time if still online
     52    if($Interface['NewOnline'])
     53      $DbResult = $System->Database->update('NetworkInterface', '`Id` = "'.$Interface['Id'].'"',
     54        array('LastOnline' => TimeToMysqlDateTime($StartTime)));
     55
     56        if($Interface['Online'] != $Interface['NewOnline'])
     57    {
     58        // Online state changed
     59      $System->Database->insert('NetworkInterfaceUpDown', array(
     60        'Interface' => $Interface['Id'], 'State' => $Interface['NewOnline'],
     61           'Time' => TimeToMysqlDateTime($StartTime)));
     62      $System->Database->update('NetworkInterface', '`Id` = "'.$Interface['Id'].'"',
     63        array('Online' => $Interface['NewOnline']));
     64    }
     65  }
     66
     67  // Set offline all interfaces which were not updated as online
     68  $DbResult = $System->Database->select('NetworkInterface', '*', '(`Online` = 1) AND '.
     69    '(`LastOnline` < "'.TimeToMysqlDateTime($StartTime).'")');
    5770  while($DbRow = $DbResult->fetch_assoc())
    5871  {
    59     $System->Database->insert('NetworkInterfaceUpDown', array(
    60       'Interface' => $DbRow['Id'], 'State' => 0, 'Time' => TimeToMysqlDateTime($StartTime)));
     72        echo('IP '.$DbRow['LocalIP'].' online but time not updated.'."\n");
    6173  }
    62   $DbResult = $System->Database->update('NetworkInterface', '`LastOnline` < "'.TimeToMysqlDateTime($StartTime).'"', array('Online' => 0));
     74  $DbResult = $System->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  }
    6380
    6481  // Update device online state
     
    87104  sleep($Delay);
    88105}
     106
Note: See TracChangeset for help on using the changeset viewer.