Ignore:
Timestamp:
Mar 16, 2016, 10:01:02 PM (9 years ago)
Author:
chronos
Message:
  • Added: UpDown history for network ports.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Modules/NetworkConfigLinux/Generators/CheckPorts.php

    r815 r818  
    33class ConfigCheckPorts extends NetworkConfigItem
    44{
    5   function CheckPortStatus($IP, $Port)
     5  function CheckPortStatus($IP, $Port, $Protocol = 'tcp')
    66  {
    77    $Timeout = 1;
    88    $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))
    1013    {
    1114      fclose($Socket);
     
    2124    // Load all ports to memory
    2225    $Ports = array();
    23     $DbResult = $this->Database->query('SELECT `NetworkPort`.`Id`, `NetworkPort`.`Number`, '.
     26    $DbResult = $this->Database->query('SELECT `NetworkPort`.`Id`, `NetworkPort`.`Number`, `NetworkPort`.`Protocol`, '.
    2427      '`NetworkPort`.`Online`, 0 AS `NewOnline`, `NetworkInterface`.`LocalIP` AS `LocalIP` '.
    2528      'FROM `NetworkPort` '.
     
    3134    foreach($Ports as $Index => $Port)
    3235    {
    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']);
    3439
    3540      // Update last online time if still online
     
    3742      {
    3843        $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'].')');
    4059      }
    4160    }
    42  
     61    $DbResult = $this->Database->update('NetworkDevice', '`LastOnline` < "'.TimeToMysqlDateTime($StartTime).'"', array('Online' => 0));
     62
    4363    // Set offline all ports which were not updated as online
    4464    $DbResult = $this->Database->select('NetworkPort', '*', '(`Online` = 1) AND '.
Note: See TracChangeset for help on using the changeset viewer.