Ignore:
Timestamp:
Jan 8, 2016, 11:00:11 PM (9 years ago)
Author:
chronos
Message:
  • Modified: Network configure actions now can be executed through cmd.php interface using "php cmd.php config <action>".
File:
1 edited

Legend:

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

    r738 r781  
    11<?php
    2 if(isset($_SERVER['REMOTE_ADDR'])) die();
    32
    4 $Enabled = 1;
    5 $ClassesEnabled = 1;
    6 $SessionDisable = true;
    7 include_once(dirname(__FILE__).'/../../../Application/System.php');
    8 $System = new System();
    9 $System->ShowPage = false;
    10 $System->Run();
    11 $PathQueue = array('queue', 'tree');
     3class ConfigRouterOSQueue extends NetworkConfigItem
     4{
     5  var $UsedNames;
    126
    13 $Routerboard = new Routerboard();
    14 $Routerboard->UserName = $Config['MainRouter']['UserName'];
    15 $Routerboard->Timeout = $Config['MainRouter']['ConnectTimeout'];
    16 $Routerboard->HostName = $Config['MainRouter']['HostName'];
    17 $Routerboard->Debug = true;
     7  function CheckName($Name)
     8  {
     9    if(in_array($Name, $this->UsedNames)) die("\n".'Duplicate name: '.$Name);
     10      else $this->UsedNames[] = $Name;
     11  }
    1812
    19 $UsedNames = array();
     13  function Run()
     14  {
     15    $PathQueue = array('queue', 'tree');
    2016
    21 function CheckName($Name)
    22 {
    23   global $UsedNames;
     17    $Routerboard = new Routerboard();
     18    $Routerboard->UserName = $this->System->Config['MainRouter']['UserName'];
     19    $Routerboard->Timeout = $this->System->Config['MainRouter']['ConnectTimeout'];
     20    $Routerboard->HostName = $this->System->Config['MainRouter']['HostName'];
     21    $Routerboard->Debug = true;
    2422
    25   if(in_array($Name, $UsedNames)) die("\n".'Duplicate name: '.$Name);
    26   else $UsedNames[] = $Name;
    27 }
     23    $this->UsedNames = array();
    2824
    29 $Finance = &$System->Modules['Finance'];
    30 $Finance->LoadMonthParameters(0);
     25    $Finance = &$this->System->Modules['Finance'];
     26    $Finance->LoadMonthParameters(0);
    3127
    32 // Generate traffic shaping rules
    33 //$TotalMaxSpeedIn = 4048; //$RealMaxSpeed; //1536;
    34 //TotalMaxSpeedOut = 3048; //$RealMaxSpeed; //1536;
    35 //$UsersMaxSpeedIn = 1900; //$MaxSpeed;
    36 //$UsersMaxSpeedOut = 1900; //$MaxSpeed;
     28    // Generate traffic shaping rules
     29    //$TotalMaxSpeedIn = 4048; //$RealMaxSpeed; //1536;
     30    //TotalMaxSpeedOut = 3048; //$RealMaxSpeed; //1536;
     31    //$UsersMaxSpeedIn = 1900; //$MaxSpeed;
     32    //$UsersMaxSpeedOut = 1900; //$MaxSpeed;
    3733
    38 $InDivider = 1;
    39 $OutDivider = 1;
    40 $TotalMaxSpeedIn = round($Finance->RealMaxSpeed / $InDivider) * 1000;
    41 $TotalMaxSpeedOut = round($Finance->RealMaxSpeed / $OutDivider) * 1000;
    42 $UsersMaxSpeedIn = round($Finance->MaxSpeed / $InDivider) * 1000;
    43 $UsersMaxSpeedOut = round($Finance->MaxSpeed / $OutDivider) * 1000;
    44 $VoipMaxSpeedIn = $TotalMaxSpeedIn - 136;
    45 $VoipMaxSpeedOut = $TotalMaxSpeedOut - 136;
    46 $VoipSpeedIn = 100; //$SpeedReserve;
    47 $VoipSpeedOut = 100; //$SpeedReserve;
    48 $OutInterface = 'eth1';
    49 $InInterface = 'ifb0';
    50 $InetInterface = $Config['MainRouter']['InetInterface'];
     34    $InDivider = 1;
     35    $OutDivider = 1;
     36    $TotalMaxSpeedIn = round($Finance->RealMaxSpeed / $InDivider) * 1000;
     37    $TotalMaxSpeedOut = round($Finance->RealMaxSpeed / $OutDivider) * 1000;
     38    $UsersMaxSpeedIn = round($Finance->MaxSpeed / $InDivider) * 1000;
     39    $UsersMaxSpeedOut = round($Finance->MaxSpeed / $OutDivider) * 1000;
     40    $VoipMaxSpeedIn = $TotalMaxSpeedIn - 136;
     41    $VoipMaxSpeedOut = $TotalMaxSpeedOut - 136;
     42    $VoipSpeedIn = 100; //$SpeedReserve;
     43    $VoipSpeedOut = 100; //$SpeedReserve;
     44    $OutInterface = 'eth1';
     45    $InInterface = 'ifb0';
     46    $InetInterface = $this->System->Config['MainRouter']['InetInterface'];
    5147
    52 $DbResult = $System->Database->select('Service', '*', '(`ChangeAction` IS NULL) AND (`Id`='.TARIFF_FREE.')');
    53 if($DbResult->num_rows == 1)
    54 {
    55   $Service = $DbResult->fetch_array();
    56   $FreeInetSpeed = $Service['InternetSpeedMax'] * 1000;
    57 } else $FreeInetSpeed = 0;
     48    $DbResult = $this->Database->select('Service', '*', '(`ChangeAction` IS NULL) AND (`Id`='.TARIFF_FREE.')');
     49    if($DbResult->num_rows == 1)
     50    {
     51      $Service = $DbResult->fetch_array();
     52      $FreeInetSpeed = $Service['InternetSpeedMax'] * 1000;
     53    } else $FreeInetSpeed = 0;
    5854
    59 $ItemsQueue = array();
     55    $ItemsQueue = array();
    6056
    61 // Root of tree and main limit
    62 $ItemsQueue[] = array('name' => 'main-out', 'limit-at' => $UsersMaxSpeedIn, 'max-limit' => $UsersMaxSpeedIn, 'parent' => 'global');
    63 $ItemsQueue[] = array('name' => 'main-in', 'limit-at' => $UsersMaxSpeedOut, 'max-limit' => $UsersMaxSpeedOut, 'parent' => 'global');
     57    // Root of tree and main limit
     58    $ItemsQueue[] = array('name' => 'main-out', 'limit-at' => $UsersMaxSpeedIn, 'max-limit' => $UsersMaxSpeedIn, 'parent' => 'global');
     59    $ItemsQueue[] = array('name' => 'main-in', 'limit-at' => $UsersMaxSpeedOut, 'max-limit' => $UsersMaxSpeedOut, 'parent' => 'global');
    6460
    65 // Divide rules by subnet number
    66 $DbResult = $System->Database->query('SELECT `Id`, `Name`, `AddressRange`, `Mask` FROM `NetworkSubnet`');
    67 // Slow free internet
    68 $PacketMark = GetMarkByComment('free-out');
    69 $ItemsQueue[] = array('name' => 'free-out', 'limit-at' => $FreeInetSpeed, 'max-limit' => $FreeInetSpeed, 'parent' => 'main-out', 'packet-mark' => $PacketMark);
    70 $PacketMark = GetMarkByComment('free-in');
    71 $ItemsQueue[] = array('name' => 'free-in', 'limit-at' => $FreeInetSpeed, 'max-limit' => $FreeInetSpeed, 'parent' => 'main-in', 'packet-mark' => $PacketMark);
     61    // Divide rules by subnet number
     62    $DbResult = $this->Database->query('SELECT `Id`, `Name`, `AddressRange`, `Mask` FROM `NetworkSubnet`');
     63    // Slow free internet
     64    $PacketMark = GetMarkByComment('free-out');
     65    $ItemsQueue[] = array('name' => 'free-out', 'limit-at' => $FreeInetSpeed, 'max-limit' => $FreeInetSpeed, 'parent' => 'main-out', 'packet-mark' => $PacketMark);
     66    $PacketMark = GetMarkByComment('free-in');
     67    $ItemsQueue[] = array('name' => 'free-in', 'limit-at' => $FreeInetSpeed, 'max-limit' => $FreeInetSpeed, 'parent' => 'main-in', 'packet-mark' => $PacketMark);
    7268
    73 // Process users
    74 $DbResult = $System->Database->query('SELECT `Member`.*, `Subject`.`Name` FROM `Member` LEFT JOIN `Subject` ON `Subject`.`Id` = `Member`.`Subject` WHERE `Member`.`Blocked`=0');
    75 while($Member = $DbResult->fetch_assoc())
    76 {
    77   $ServiceIndex = 1;
    78   echo('Zákazník '.$Member['Name']."\n");
    79   $DbResult4 = $System->Database->query('SELECT `Service`.*, `ServiceCustomerRel`.`Id` AS `RelId` FROM `ServiceCustomerRel` '.
    80     'JOIN `Service` ON `Service`.`Id` = `ServiceCustomerRel`.`Service` '.
    81     'WHERE (`ServiceCustomerRel`.`Customer` = '.$Member['Id'].') AND (`ServiceCustomerRel`.`ChangeAction` IS NULL) '.
    82     'AND (`Service`.`InternetSpeedMax` > 0) AND (`Service`.`InternetSpeedMin` > 0)');
    83   while($Service = $DbResult4->fetch_assoc())
    84   {
    85     echo('Služba '.$Service['Name'].': ');
    86     $MemberName = RouterOSIdent($Member['Name'].'-'.$Member['Id'].'-'.$ServiceIndex);
    87     $SpeedIn = round($Service['InternetSpeedMin'] / $InDivider) * 1000;
    88     $SpeedOut = round($Service['InternetSpeedMin'] / $OutDivider) * 1000;
    89     $UserMaxSpeedIn = round($Service['InternetSpeedMax'] / $InDivider) * 1000;
    90     $UserMaxSpeedOut = round($Service['InternetSpeedMax'] / $OutDivider) * 1000;
    91     $ItemsQueue[] = array('name' => $MemberName.'-out', 'limit-at' => $SpeedIn, 'max-limit' => $UserMaxSpeedIn, 'parent' => 'main-out');
    92     $ItemsQueue[] = array('name' => $MemberName.'-in', 'limit-at' => $SpeedOut, 'max-limit' => $UserMaxSpeedOut, 'parent' => 'main-in');
    93     CheckName($MemberName.'-out');
    94     CheckName($MemberName.'-in');
     69    // Process users
     70    $DbResult = $this->Database->query('SELECT `Member`.*, `Subject`.`Name` FROM `Member` LEFT JOIN `Subject` ON `Subject`.`Id` = `Member`.`Subject` WHERE `Member`.`Blocked`=0');
     71    while($Member = $DbResult->fetch_assoc())
     72    {
     73      $ServiceIndex = 1;
     74      echo('Zákazník '.$Member['Name']."\n");
     75      $DbResult4 = $this->Database->query('SELECT `Service`.*, `ServiceCustomerRel`.`Id` AS `RelId` FROM `ServiceCustomerRel` '.
     76          'JOIN `Service` ON `Service`.`Id` = `ServiceCustomerRel`.`Service` '.
     77          'WHERE (`ServiceCustomerRel`.`Customer` = '.$Member['Id'].') AND (`ServiceCustomerRel`.`ChangeAction` IS NULL) '.
     78          'AND (`Service`.`InternetSpeedMax` > 0) AND (`Service`.`InternetSpeedMin` > 0)');
     79      while($Service = $DbResult4->fetch_assoc())
     80      {
     81        echo('Služba '.$Service['Name'].': ');
     82        $MemberName = RouterOSIdent($Member['Name'].'-'.$Member['Id'].'-'.$ServiceIndex);
     83        $SpeedIn = round($Service['InternetSpeedMin'] / $InDivider) * 1000;
     84        $SpeedOut = round($Service['InternetSpeedMin'] / $OutDivider) * 1000;
     85        $UserMaxSpeedIn = round($Service['InternetSpeedMax'] / $InDivider) * 1000;
     86        $UserMaxSpeedOut = round($Service['InternetSpeedMax'] / $OutDivider) * 1000;
     87        $ItemsQueue[] = array('name' => $MemberName.'-out', 'limit-at' => $SpeedIn, 'max-limit' => $UserMaxSpeedIn, 'parent' => 'main-out');
     88        $ItemsQueue[] = array('name' => $MemberName.'-in', 'limit-at' => $SpeedOut, 'max-limit' => $UserMaxSpeedOut, 'parent' => 'main-in');
     89        $this->CheckName($MemberName.'-out');
     90        $this->CheckName($MemberName.'-in');
    9591
    96     $Filter = '(`Used` = 1) AND (`Service` = '.$Service['RelId'].')';
    97     $DbResult2 = $System->Database->select('NetworkDevice', 'COUNT(*)', $Filter);
    98     $Row = $DbResult2->fetch_row();
    99     $HostCount = $Row[0];
    100     if($HostCount > 0)
    101     {
    102       $HostSpeedIn = round($SpeedIn / $HostCount);
    103       $HostSpeedOut = round($SpeedOut / $HostCount);
    104     } else
    105     {
    106       $HostSpeedIn = $SpeedIn;
    107       $HostSpeedOut = $SpeedOut;
    108     }
     92        $Filter = '(`Used` = 1) AND (`Service` = '.$Service['RelId'].')';
     93        $DbResult2 = $this->Database->select('NetworkDevice', 'COUNT(*)', $Filter);
     94        $Row = $DbResult2->fetch_row();
     95        $HostCount = $Row[0];
     96        if($HostCount > 0)
     97        {
     98          $HostSpeedIn = round($SpeedIn / $HostCount);
     99          $HostSpeedOut = round($SpeedOut / $HostCount);
     100        } else
     101        {
     102          $HostSpeedIn = $SpeedIn;
     103          $HostSpeedOut = $SpeedOut;
     104        }
    109105
    110     $DbResult2 = $System->Database->select('NetworkDevice', '*', $Filter);
    111     while($Device = $DbResult2->fetch_assoc())
    112     {
    113       $DbResult3 = $System->Database->select('NetworkInterface', '*', '`Device` = '.$Device['Id'].' AND `LocalIP` != ""');
    114       while($Interface = $DbResult3->fetch_assoc())
    115       {
    116         $Name = $Device['Name'];
    117         if($Interface['Name'] != '') $Name .= '-'.$Interface['Name'];
    118         $Name = RouterOSIdent($Name);
    119         echo($Name.', ');
    120         $IPParts = explode('.', $Interface['LocalIP']);
    121         $Subnet = $IPParts[2];
    122         $PacketMark = GetMarkByComment($Name.'-out');
    123         $ItemsQueue[] = array('name' => $Name.'-out', 'limit-at' => $HostSpeedIn, 'max-limit' => $UserMaxSpeedIn, 'parent' => $MemberName.'-out', 'packet-mark' => $PacketMark);
    124         $PacketMark = GetMarkByComment($Name.'-in');
    125         $ItemsQueue[] = array('name' => $Name.'-in', 'limit-at' => $HostSpeedOut, 'max-limit' => $UserMaxSpeedOut, 'parent' => $MemberName.'-in', 'packet-mark' => $PacketMark);
    126         CheckName($Name.'-out');
    127         CheckName($Name.'-in');
     106        $DbResult2 = $this->Database->select('NetworkDevice', '*', $Filter);
     107        while($Device = $DbResult2->fetch_assoc())
     108        {
     109          $DbResult3 = $this->Database->select('NetworkInterface', '*', '`Device` = '.$Device['Id'].' AND `LocalIP` != ""');
     110          while($Interface = $DbResult3->fetch_assoc())
     111          {
     112            $Name = $Device['Name'];
     113            if($Interface['Name'] != '') $Name .= '-'.$Interface['Name'];
     114            $Name = RouterOSIdent($Name);
     115            echo($Name.', ');
     116            $IPParts = explode('.', $Interface['LocalIP']);
     117            $Subnet = $IPParts[2];
     118            $PacketMark = GetMarkByComment($Name.'-out');
     119            $ItemsQueue[] = array('name' => $Name.'-out', 'limit-at' => $HostSpeedIn, 'max-limit' => $UserMaxSpeedIn, 'parent' => $MemberName.'-out', 'packet-mark' => $PacketMark);
     120            $PacketMark = GetMarkByComment($Name.'-in');
     121            $ItemsQueue[] = array('name' => $Name.'-in', 'limit-at' => $HostSpeedOut, 'max-limit' => $UserMaxSpeedOut, 'parent' => $MemberName.'-in', 'packet-mark' => $PacketMark);
     122            $this->CheckName($Name.'-out');
     123            $this->CheckName($Name.'-in');
     124          }
     125        }
     126
     127        $DbResult2 = $this->Database->select('NetworkSubnet', '*', '`Service`='.$Service['RelId']);
     128        while($Subnet = $DbResult2->fetch_assoc())
     129        {
     130          $Subnet['Name'] = RouterOSIdent('subnet-'.$Subnet['Name']);
     131          echo($Subnet['Name'].', ');
     132          $IPParts = explode('.', $Subnet['AddressRange']);
     133          $SubnetNumber = $IPParts[2];
     134          $PacketMark = GetMarkByComment($Subnet['Name'].'-out');
     135          $ItemsQueue[] = array('name' => $Subnet['Name'].'-out', 'limit-at' => $HostSpeedIn, 'max-limit' => $UserMaxSpeedIn, 'parent' => $MemberName.'-out', 'packet-mark' => $PacketMark);
     136          $PacketMark = GetMarkByComment($Subnet['Name'].'-in');
     137          $ItemsQueue[] = array('name' => $Subnet['Name'].'-in', 'limit-at' => $HostSpeedOut, 'max-limit' => $UserMaxSpeedOut, 'parent' => $MemberName.'-in', 'packet-mark' => $PacketMark);
     138          $this->CheckName($Subnet['Name'].'-out');
     139          $this->CheckName($Subnet['Name'].'-in');
     140        }
     141        echo("\n");
     142        $ServiceIndex++;
    128143      }
    129144    }
    130145
    131     $DbResult2 = $System->Database->select('NetworkSubnet', '*', '`Service`='.$Service['RelId']);
    132     while($Subnet = $DbResult2->fetch_assoc())
    133     {
    134       $Subnet['Name'] = RouterOSIdent('subnet-'.$Subnet['Name']);
    135       echo($Subnet['Name'].', ');
    136       $IPParts = explode('.', $Subnet['AddressRange']);
    137       $SubnetNumber = $IPParts[2];
    138       $PacketMark = GetMarkByComment($Subnet['Name'].'-out');
    139       $ItemsQueue[] = array('name' => $Subnet['Name'].'-out', 'limit-at' => $HostSpeedIn, 'max-limit' => $UserMaxSpeedIn, 'parent' => $MemberName.'-out', 'packet-mark' => $PacketMark);
    140       $PacketMark = GetMarkByComment($Subnet['Name'].'-in');
    141       $ItemsQueue[] = array('name' => $Subnet['Name'].'-in', 'limit-at' => $HostSpeedOut, 'max-limit' => $UserMaxSpeedOut, 'parent' => $MemberName.'-in', 'packet-mark' => $PacketMark);
    142       CheckName($Subnet['Name'].'-out');
    143       CheckName($Subnet['Name'].'-in');
    144     }
    145     echo("\n");
    146     $ServiceIndex++;
     146    //print_r($ItemsQueue);
     147    $Routerboard->ListUpdate($PathQueue, array('name', 'limit-at', 'max-limit', 'parent', 'packet-mark'), $ItemsQueue, array(), true);
    147148  }
    148149}
    149 
    150 //print_r($ItemsQueue);
    151 $Routerboard->ListUpdate($PathQueue, array('name', 'limit-at', 'max-limit', 'parent', 'packet-mark'), $ItemsQueue, array(), true);
Note: See TracChangeset for help on using the changeset viewer.