- Timestamp:
- Mar 16, 2016, 10:01:02 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Modules/NetworkConfigLinux/Generators/CheckPorts.php
r815 r818 3 3 class ConfigCheckPorts extends NetworkConfigItem 4 4 { 5 function CheckPortStatus($IP, $Port )5 function CheckPortStatus($IP, $Port, $Protocol = 'tcp') 6 6 { 7 7 $Timeout = 1; 8 8 $State = 0; 9 if($Socket = @fsockopen($IP, $Port, $ErrorNumber, $ErrorString, $Timeout)) 9 if($Protocol = 'tcp') $Prefix = ''; 10 else if($Protocol = 'udp') $Prefix = 'udp://'; 11 else throw new Exception('Unsupported protocol "'.$Protocol.'"'); 12 if($Socket = @fsockopen($Prefix.$IP, $Port, $ErrorNumber, $ErrorString, $Timeout)) 10 13 { 11 14 fclose($Socket); … … 21 24 // Load all ports to memory 22 25 $Ports = array(); 23 $DbResult = $this->Database->query('SELECT `NetworkPort`.`Id`, `NetworkPort`.`Number`, '.26 $DbResult = $this->Database->query('SELECT `NetworkPort`.`Id`, `NetworkPort`.`Number`, `NetworkPort`.`Protocol`, '. 24 27 '`NetworkPort`.`Online`, 0 AS `NewOnline`, `NetworkInterface`.`LocalIP` AS `LocalIP` '. 25 28 'FROM `NetworkPort` '. … … 31 34 foreach($Ports as $Index => $Port) 32 35 { 33 $Port['NewOnline'] = $this->CheckPortStatus($Port['LocalIP'], $Port['Number']); 36 if($Port['Protocol'] == 0) $Port['Protocol'] = 'tcp'; 37 if($Port['Protocol'] == 1) $Port['Protocol'] = 'udp'; 38 $Port['NewOnline'] = $this->CheckPortStatus($Port['LocalIP'], $Port['Number'], $Port['Protocol']); 34 39 35 40 // Update last online time if still online … … 37 42 { 38 43 $DbResult = $this->Database->update('NetworkPort', '`Id` = "'.$Port['Id'].'"', 39 array('LastOnline' => TimeToMysqlDateTime($StartTime))); 44 array('Online' => 1, 'LastOnline' => TimeToMysqlDateTime($StartTime))); 45 } 46 47 // Update UpDown table 48 if($Port['Online'] != $Port['NewOnline']) 49 { 50 // Online state changed 51 $DbResult = $this->Database->query('INSERT INTO `NetworkPortUpDown` (`Port`, 52 `State`, `Time`, `Duration`) VALUES ('.$Port['Id'].', '.$Port['NewOnline'].', "'. 53 TimeToMysqlDateTime($StartTime).'", NULL)'); 54 // Update previous record duration in UpDown table 55 $this->Database->query('UPDATE `NetworkPortUpDown` AS `TM` SET `Duration` = TIMESTAMPDIFF(SECOND, '. 56 '`TM`.`Time`, (SELECT `Time` FROM (SELECT * FROM `NetworkPortUpDown`) AS `TA` WHERE (`TA`.`Time` > `TM`.`Time`) '. 57 'AND (`TA`.`Port`=`TM`.`Port`) ORDER BY `TA`.`Time` ASC LIMIT 1)) '. 58 'WHERE (`TM`.`Duration` IS NULL) AND (`TM`.`Interface` ='.$Port['Id'].')'); 40 59 } 41 60 } 42 61 $DbResult = $this->Database->update('NetworkDevice', '`LastOnline` < "'.TimeToMysqlDateTime($StartTime).'"', array('Online' => 0)); 62 43 63 // Set offline all ports which were not updated as online 44 64 $DbResult = $this->Database->select('NetworkPort', '*', '(`Online` = 1) AND '.
Note:
See TracChangeset
for help on using the changeset viewer.