Changeset 667
- Timestamp:
- Jul 14, 2014, 10:15:00 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Modules/NetworkConfigRouterOS/Generators/NetwatchImport.php
r663 r667 10 10 $Period = 60; // every 60 seconds 11 11 12 12 13 while(1) 13 14 { 14 15 $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; 15 22 16 23 // Load netwatch status from all DHCP routers … … 27 34 foreach($List as $Properties) 28 35 { 36 $IP = $Properties['host']; 29 37 if($Properties['status'] == 'up') $Online = 1; 30 38 else $Online = 0; … … 32 40 if($Online) 33 41 { 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"); 51 45 } 52 46 } 53 47 } 54 48 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).'")'); 57 70 while($DbRow = $DbResult->fetch_assoc()) 58 71 { 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"); 61 73 } 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 } 63 80 64 81 // Update device online state … … 87 104 sleep($Delay); 88 105 } 106
Note:
See TracChangeset
for help on using the changeset viewer.