Ignore:
Timestamp:
Mar 9, 2016, 10:07:18 PM (9 years ago)
Author:
chronos
Message:
  • Added: Notify callback for checking state of network ports. Network ports can be defined as subtable under network interface.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Modules/Network/Network.php

    r813 r814  
    120120  function DoStart()
    121121  {
    122     $this->System->ModuleManager->Modules['Notify']->RegisterCheck('NetworkReachability', array($this, 'ReachabilityCheck'));
     122    $this->System->ModuleManager->Modules['Notify']->RegisterCheck('NetworkReachability',
     123      array($this, 'ReachabilityCheck'));
     124    $this->System->ModuleManager->Modules['Notify']->RegisterCheck('NetworkPort',
     125      array($this, 'PortCheck'));
     126   
    123127   
    124128    $this->System->RegisterPage('network', 'PageNetwork');
     
    188192        'ExternalIP' => array('Type' => 'IPv4Address', 'Caption' => 'Veřejná IPv4', 'Default' => '', 'Null' => true),
    189193        'Device' => array('Type' => 'TNetworkDevice', 'Caption' => 'Zařízení', 'Default' => ''),
     194        'Enabled' => array('Type' => 'Boolean', 'Caption' => 'Povoleno', 'Default' => '1'),
    190195        'Online' => array('Type' => 'TOnlineState', 'Caption' => 'Běží', 'Default' => '0', 'ReadOnly' => true),
    191196        'LastOnline' => array('Type' => 'DateTime', 'Caption' => 'Naposledy běželo', 'Default' => '', 'ReadOnly' => true),
     
    194199        'Signal' => array('Type' => 'TNetworkSignalListInterface', 'Caption' => 'Signál', 'Default' => ''),
    195200        'Wireless' => array('Type' => 'TNetworkInterfaceWirelessListInterface', 'Caption' => 'Bezdrátové spoje', 'Default' => ''),
     201        'Ports' => array('Type' => 'TDevicePortListInterface', 'Caption' => 'Síťové porty', 'Default' => ''),
    196202      ),
    197203    ));
     
    225231        'AddressRangeIPv6' => array('Type' => 'String', 'Caption' => 'Rozsah adres IPv6', 'Default' => ''),
    226232        'Configure' => array('Type' => 'Boolean', 'Caption' => 'Nastavovat', 'Default' => ''),
     233      ),
     234    ));
     235    $this->System->FormManager->RegisterClass('NetworkPort', array(
     236      'Title' => 'Síťový port',
     237      'Table' => 'NetworkPort',
     238      'Items' => array(
     239        'Name' => array('Type' => 'String', 'Caption' => 'Jméno', 'Default' => ''),
     240        'Number' => array('Type' => 'Integer', 'Caption' => 'Číslo', 'Default' => ''),
     241        'Interface' => array('Type' => 'TNetworkInterface', 'Caption' => 'Rozhraní', 'Default' => ''),
     242        'Enabled' => array('Type' => 'Boolean', 'Caption' => 'Povoleno', 'Default' => '1'),
     243        'Online' => array('Type' => 'TOnlineState', 'Caption' => 'Běží', 'Default' => '0', 'ReadOnly' => true),
     244        'LastOnline' => array('Type' => 'DateTime', 'Caption' => 'Naposledy běželo', 'Default' => '', 'ReadOnly' => true),
    227245      ),
    228246    ));
     
    510528      'Filter' => '1',
    511529    ));
     530    $this->System->FormManager->RegisterFormType('TDevicePortListInterface', array(
     531      'Type' => 'ManyToOne',
     532      'Table' => 'NetworkPort',
     533      'Id' => 'Id',
     534      'Ref' => 'Interface',
     535      'Filter' => '1',
     536    ));
    512537    $this->System->FormManager->RegisterFormType('TNetworkLinkType', array(
    513538      'Type' => 'Reference',
     
    654679    }
    655680  }
     681
     682  function PortCheckList($Title, $OnlineNow, $OnlinePrevious, $MinDuration)
     683  {
     684    $Time = time();
     685    $OnlineText = array('<span style="color:red;">Nedostupný</span>', '<span style="color:green;">Dostupný</span>');
     686    $Output = '';
     687    $Condition = 'WHERE (`NetworkPort`.`Online` = '.$OnlineNow.') AND (`NetworkPort`.`OnlineNotify` = '.$OnlinePrevious.') '.
     688      'AND (`NetworkPort`.`LastOnline` <= "'.TimeToMysqlDateTime($Time - $MinDuration).'")';
     689    $DbResult3 = $this->Database->query('SELECT CONCAT(CONCAT_WS("-", `NetworkDevice`.`Name`, NULLIF(`NetworkInterface`.`Name`, "")), ":", `NetworkPort`.`Number`) AS `Name`, '.
     690      '`NetworkPort`.`Online`, `NetworkPort`.`LastOnline` FROM `NetworkPort` '.
     691      'LEFT JOIN `NetworkInterface` ON `NetworkInterface`.`Id` = `NetworkPort`.`Interface` '.
     692      'LEFT JOIN `NetworkDevice` ON `NetworkDevice`.`Id` = `NetworkInterface`.`Device` '.
     693      $Condition.' AND (`NetworkDevice`.`PermanentOnline`=1) AND (`NetworkDevice`.`Used`=1) AND '.
     694      '(`NetworkInterface`.`LocalIP` != "") AND (`NetworkPort`.`Enabled` = 1)'.
     695      'ORDER BY `Name` ASC');
     696    echo($this->Database->LastQuery);
     697    if($DbResult3->num_rows > 0)
     698    {
     699      $Output .= $Title.'<br/>';
     700      $Output .= '<table>'.
     701        '<tr><th align="center">Jméno</th><th align="center">Stav</th><th align="center">Čas</th><th align="center">Trvání</th></tr>'."\n";
     702      while($Item = $DbResult3->fetch_assoc())
     703      {
     704        $Duration = $Time - MysqlDateTimeToTime($Item['LastOnline']);
     705        $DurationText = sprintf('%02d', floor($Duration / 3600 % 24)).':'.
     706          sprintf('%02d', floor($Duration / 60 % 60)).':'.
     707          sprintf('%02d', floor($Duration % 60));
     708        $Days = floor($Duration / (60 * 60 * 24));
     709        if($Days > 0) $DurationText = $Days.' dnů '.$DurationText;
     710 
     711        $Output .= '<tr><td>'.$Item['Name'].'</td><td>'.$OnlineText[$Item['Online']].
     712          '</td><td>'.$Item['LastOnline'].'</td><td>'.$DurationText.'</td></tr>'."\n";
     713      }
     714      $Output .= '</table><br/>'."\n";
     715    }
     716    $this->Database->query('UPDATE `NetworkPort` SET `OnlineNotify` = `Online` '.
     717      $Condition);
     718    return $Output;
     719  }
     720 
     721  function PortCheck()
     722  {
     723    $Output = '';
     724    $Output = $this->PortCheckList('Nově online', 1, 0, 0);
     725    $Output .= $this->PortCheckList('Nově offline', 0, 1, 5 * 60);
     726    if($Output != '') $Output .= $this->PortCheckList('Stále offline', 0, 0, 0);
     727    return($Output);
     728  }
    656729}
Note: See TracChangeset for help on using the changeset viewer.