Changeset 241 for trunk


Ignore:
Timestamp:
Aug 2, 2009, 4:56:48 PM (15 years ago)
Author:
george
Message:
  • Upraveno: Výrazné zrychlení načítání seznamů pomocí print terse.
  • Upraveno: Pro použití nové třídy Routerboard přizpůsoben i skript pro generování řízení toku.
Location:
trunk/system
Files:
4 deleted
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/system/generators/traffic_shaping_routerboard.php

    r214 r241  
    66$SessionDisable = true;
    77include_once('../../global.php');
     8include('../routerboard.php');
     9$PathFirewall = array('ip', 'firewall', 'mangle');
     10$PathQueue = array('queue', 'tree');
     11
     12$Routerboard = new Routerboard();
     13$Routerboard->UserName = $Config['MainRouter']['UserName'];
     14$Routerboard->Timeout = $Config['MainRouter']['ConnectTimeout'];
     15$Routerboard->HostName = $Config['MainRouter']['HostName'];
     16
    817$Finance = &$System->Modules['Finance'];
    918$Finance->LoadMonthParameters(0);
     
    3039$FreeInetSpeed = 64 * 1024;
    3140
    32 $Commands = array();
    33 $Commands[] = '/ip firewall mangle { remove [find] }';
    34 $Commands[] = '/queue tree { remove [find] }';
     41$ItemsMangle = array();
     42$ItemsQueue = array();
    3543
    3644// Root of tree and main limit
    3745$PacketMark = 1;
    38 $Commands[] = ' /queue tree add name=main-out limit-at='.$UsersMaxSpeedIn.' max-limit='.$UsersMaxSpeedIn.' parent=global-out';
    39 $Commands[] = ' /ip firewall mangle add chain=forward out-interface='.$InetInterface.' action=jump jump-target=inet-out';
    40 $Commands[] = ' /queue tree add name=main-in limit-at='.$UsersMaxSpeedOut.' max-limit='.$UsersMaxSpeedOut.' parent=global-out';
    41 $Commands[] = ' /ip firewall mangle add chain=forward in-interface='.$InetInterface.' action=jump jump-target=inet-in';
     46$ItemsQueue[] = array('name' => 'main-out', 'limit-at' => $UsersMaxSpeedIn, 'max-limit' => $UsersMaxSpeedIn, 'parent' => 'global-out', 'queue' => 'wireless-default');
     47$ItemsFirewall[] = array('chain' => 'forward', 'out-interface' => $InetInterface, 'action' => 'jump', 'jump-target' => 'inet-out');
     48$ItemsQueue[] = array('name' => 'main-in', 'limit-at' => $UsersMaxSpeedOut, 'max-limit' => $UsersMaxSpeedOut, 'parent' => 'global-out', 'queue' => 'wireless-default');
     49$ItemsFirewall[] = array('chain' => 'forward', 'in-interface' => $InetInterface, 'action' => 'jump', 'jump-target' => 'inet-in');
    4250
    4351// Divide rules by subnet number
     
    4755  $SubnetParts = explode('.', $Subnet['AddressRange']);
    4856  $SubnetNumber = $SubnetParts[2];
    49   $Commands[] = ' /ip firewall mangle add chain=inet-out src-address='.$Subnet['AddressRange'].'/'.$Subnet['Mask'].' out-interface='.$InetInterface.' action=jump jump-target=inet-out-'.$SubnetNumber;
    50   $Commands[] = ' /ip firewall mangle add chain=inet-in dst-address='.$Subnet['AddressRange'].'/'.$Subnet['Mask'].' in-interface='.$InetInterface.' action=jump jump-target=inet-in-'.$SubnetNumber;
     57  $ItemsFirewall[] = array('chain' => 'inet-out', 'src-address' => $Subnet['AddressRange'].'/'.$Subnet['Mask'], 'out-interface' => $InetInterface, 'action' => 'jump', 'jump-target' => 'inet-out-'.$SubnetNumber);
     58  $ItemsFirewall[] = array('chain' => 'inet-in', 'dst-address' => $Subnet['AddressRange'].'/'.$Subnet['Mask'], 'in-interface' => $InetInterface, 'action' => 'jump', 'jump-target' => 'inet-in-'.$SubnetNumber);
    5159}
    5260
    5361// Slow free internet
    54 $Commands[] = ' /ip firewall mangle add chain=inet-out out-interface='.$InetInterface.' action=mark-packet new-packet-mark='.$PacketMark.' comment="Free inet"';
    55 $Commands[] = ' /queue tree add name=free-out limit-at='.$FreeInetSpeed.' max-limit='.$FreeInetSpeed.' parent=main-out packet-mark='.$PacketMark.' queue=wireless-default';
     62$ItemsFirewall[] = array('chain' => 'inet-out', 'out-interface' => $InetInterface, 'action' => 'mark-packet', 'new-packet-mark' => $PacketMark, 'comment' => 'Free_inet', 'passthrough' => 'no');
     63$ItemsQueue[] = array('name' => 'free-out', 'limit-at' => $FreeInetSpeed, 'max-limit' => $FreeInetSpeed, 'parent' => 'main-out', 'packet-mark' => $PacketMark, 'queue' => 'wireless-default');
    5664$PacketMark++;
    57 $Commands[] = ' /ip firewall mangle add chain=inet-in in-interface='.$InetInterface.' action=mark-packet new-packet-mark='.$PacketMark.' comment="Free inet"';
    58 $Commands[] = ' /queue tree add name=free-in limit-at='.$FreeInetSpeed.' max-limit='.$FreeInetSpeed.' parent=main-in packet-mark='.$PacketMark.' queue=wireless-default';
     65$ItemsFirewall[] = array('chain' => 'inet-in', 'in-interface' => $InetInterface, 'action' => 'mark-packet', 'new-packet-mark' => $PacketMark, 'comment' => 'Free_inet', 'passthrough' => 'no');
     66$ItemsQueue[] = array('name' => 'free-in', 'limit-at' => $FreeInetSpeed, 'max-limit' => $FreeInetSpeed, 'parent' => 'main-in', 'packet-mark' =>$PacketMark, 'queue' => 'wireless-default');
    5967$PacketMark++;
    6068
    6169// Process users
    62 $Index = 0;
    6370$DbResult = $Database->query('SELECT Member.*, Subject.Name FROM Member LEFT JOIN Subject ON Subject.Id = Member.Subject');
    6471while($Member = $DbResult->fetch_assoc())
    6572{
    66   $Member['Name'] = RouterOSIdent($Member['Name']);
     73  $Member['Name'] = RouterOSIdent($Member['Name'].'-'.$Member['Id'] );
    6774
    6875  echo('Uživatel '.$Member['Name'].': ');
     
    7279  $UserMaxSpeedIn = round($Tariff['SpeedMax'] / $InDivider);
    7380  $UserMaxSpeedOut = round($Tariff['SpeedMax'] / $OutDivider);
    74   $Commands[] = ' /queue tree add name='.$Member['Name'].'-out limit-at='.$SpeedIn.' max-limit='.$UserMaxSpeedIn.' parent=main-out queue=wireless-default';
    75   $Commands[] = ' /queue tree add name='.$Member['Name'].'-in limit-at='.$SpeedOut.' max-limit='.$UserMaxSpeedOut.' parent=main-in queue=wireless-default';
     81  $ItemsQueue[] = array('name' => $Member['Name'].'-out', 'limit-at' => $SpeedIn, 'max-limit' => $UserMaxSpeedIn, 'parent' => 'main-out', 'queue' => 'wireless-default');
     82  $ItemsQueue[] = array('name' => $Member['Name'].'-in', 'limit-at' => $SpeedOut, 'max-limit' => $UserMaxSpeedOut, 'parent' => 'main-in', 'queue' => 'wireless-default');
    7683
    7784  $DbResult2 = $Database->select('NetworkDevice', 'COUNT(*)', 'Used = 1 AND Member='.$Member['Id']);
     
    100107      $IPParts = explode('.', $Interface['LocalIP']);
    101108      $Subnet = $IPParts[2];
    102       $Commands[] = ' /ip firewall mangle add chain=inet-out-'.$Subnet.' src-address='.$Interface['LocalIP'].' out-interface='.$InetInterface.' action=mark-packet new-packet-mark='.$PacketMark.' passthrough=no comment="'.$Name.'"';
    103       $Commands[] = ' /queue tree add name='.$Name.'-out limit-at='.$HostSpeedIn.' max-limit='.$UserMaxSpeedIn.' parent='.$Member['Name'].'-out packet-mark='.$PacketMark.' queue=wireless-default';
     109      $ItemsFirewall[] = array('chain' => 'inet-out-'.$Subnet, 'src-address' => $Interface['LocalIP'], 'out-interface' => $InetInterface, 'action' => 'mark-packet', 'new-packet-mark' => $PacketMark, 'passthrough' => 'no', 'comment' => $Name);
     110      $ItemsQueue[] = array('name' => $Name.'-out', 'limit-at' => $HostSpeedIn, 'max-limit' => $UserMaxSpeedIn, 'parent' => $Member['Name'].'-out', 'packet-mark' => $PacketMark, 'queue' => 'wireless-default');
    104111      $PacketMark++;
    105       $Commands[] = ' /ip firewall mangle add chain=inet-in-'.$Subnet.' dst-address='.$Interface['LocalIP'].' in-interface='.$InetInterface.' action=mark-packet new-packet-mark='.$PacketMark.' passthrough=no comment="'.$Name.'"';
    106       $Commands[] = ' /queue tree add name='.$Name.'-in limit-at='.$HostSpeedOut.' max-limit='.$UserMaxSpeedOut.' parent='.$Member['Name'].'-in packet-mark='.$PacketMark.' queue=wireless-default';
     112      $ItemsFirewall[] = array('chain' => 'inet-in-'.$Subnet, 'dst-address' => $Interface['LocalIP'], 'in-interface' => $InetInterface, 'action' => 'mark-packet', 'new-packet-mark' => $PacketMark, 'passthrough' => 'no', 'comment' => $Name);
     113      $ItemsQueue[] = array('name' => $Name.'-in', 'limit-at' => $HostSpeedOut, 'max-limit' => $UserMaxSpeedOut, 'parent' => $Member['Name'].'-in', 'packet-mark' => $PacketMark, 'queue' => 'wireless-default');
    107114      $PacketMark++;
    108 
    109       // Posílej po menších částech
    110       if($Index > 50)
    111       {
    112         //print_r($Commands);
    113         $Commands = addslashes(implode(';', $Commands));
    114         $Command = '/usr/bin/ssh -l '.$Config['MainRouter']['UserName'].' -i id_dsa '.$Config['MainRouter']['HostName'].' "'.$Commands.'"';
    115         $Output = array();
    116         exec($Command, $Output);
    117         print_r($Output);
    118         $Commands = array();
    119         $Index = 0;
    120       }
    121       $Index++;
    122115    }
    123116  }
     
    130123    $IPParts = explode('.', $Subnet['AddressRange']);
    131124    $SubnetNumber = $IPParts[2];
    132     $Commands[] = ' /ip firewall mangle add chain=inet-out-'.$SubnetNumber.' src-address='.$Subnet['AddressRange'].'/'.$Subnet['Mask'].' out-interface='.$InetInterface.' action=mark-packet new-packet-mark='.$PacketMark.' passthrough=no comment="'.$Subnet['Name'].'"';
    133     $Commands[] = ' /queue tree add name='.$Subnet['Name'].'-out limit-at='.$HostSpeedIn.' max-limit='.$UserMaxSpeedIn.' parent='.$Member['Name'].'-out packet-mark='.$PacketMark.' queue=wireless-default';
     125    $ItemsFirewall[] = array('chain' => 'inet-out-'.$SubnetNumber, 'src-address' => $Subnet['AddressRange'].'/'.$Subnet['Mask'], 'out-interface' =>  $InetInterface, 'action' => 'mark-packet', 'new-packet-mark' => $PacketMark, 'passthrough' => 'no', 'comment' => $Subnet['Name']);
     126    $ItemsQueue[] = array('name' => $Subnet['Name'].'-out', 'limit-at' => $HostSpeedIn, 'max-limit' => $UserMaxSpeedIn, 'parent' => $Member['Name'].'-out', 'packet-mark' => $PacketMark, 'queue' => 'wireless-default');
    134127    $PacketMark++;
    135     $Commands[] = ' /ip firewall mangle add chain=inet-in-'.$SubnetNumber.' dst-address='.$Subnet['AddressRange'].'/'.$Subnet['Mask'].' in-interface='.$InetInterface.' action=mark-packet new-packet-mark='.$PacketMark.' passthrough=no comment="'.$Subnet['Name'].'"';
    136     $Commands[] = ' /queue tree add name='.$Subnet['Name'].'-in limit-at='.$HostSpeedOut.' max-limit='.$UserMaxSpeedOut.' parent='.$Member['Name'].'-in packet-mark='.$PacketMark.' queue=wireless-default';
     128    $ItemsFirewall[] = array('chain' => 'inet-in-'.$SubnetNumber, 'dst-address' => $Subnet['AddressRange'].'/'.$Subnet['Mask'], 'in-interface' => $InetInterface, 'action' => 'mark-packet', 'new-packet-mark' => $PacketMark, 'passthrough' => 'no', 'comment' => $Subnet['Name']);
     129    $ItemsQueue[] = array('name' => $Subnet['Name'].'-in', 'limit-at' => $HostSpeedOut, 'max-limit' => $UserMaxSpeedOut, 'parent' => $Member['Name'].'-in', 'packet-mark' => $PacketMark, 'queue' => 'wireless-default');
    137130    $PacketMark++;
    138 
    139     // Posílej po menších částech
    140     if($Index > 50)
    141     {
    142       //print_r($Commands);
    143       $Commands = addslashes(implode(';', $Commands));
    144       $Command = '/usr/bin/ssh -l '.$Config['MainRouter']['UserName'].' -i id_dsa '.$Config['MainRouter']['HostName'].' "'.$Commands.'"';
    145       //echo($Command."\n");
    146       $Output = array();
    147       exec($Command, $Output);
    148       print_r($Output);
    149           $Commands = array();
    150       $Index = 0;
    151     }
    152     $Index++;
    153131  }
    154132  echo("\n");
    155133}
    156 //print_r($Commands);
    157 $Commands = addslashes(implode(';', $Commands));
    158 $Command = '/usr/bin/ssh -l '.$Config['MainRouter']['UserName'].' -i id_dsa '.$Config['MainRouter']['HostName'].' "'.$Commands.'"';
    159 //echo($Command."\n");
    160 $Output = array();
    161 exec($Command, $Output);
    162 //array_pop($Output);
    163 print_r($Output);
     134
     135//$Routerboard->ListUpdate($PathQueue, array('name', 'limit-at', 'max-limit', 'parent', 'packet-mark', 'queue'), $ItemsQueue, array(), true);
     136$Routerboard->ListUpdate($PathFirewall, array('chain', 'dst-address', 'in-interface', 'action', 'new-packet-mark', 'passthrough', 'comment', 'out-interface', 'src-address', 'jump-target'), $ItemsFirewall, array(), true);
    164137
    165138?>
  • trunk/system/queues_import.php

    r239 r241  
    88$Routerboard = new Routerboard($Config['MainRouter']['HostName'], $Config['MainRouter']['UserName'], $Config['MainRouter']['Password']);
    99$Routerboard->PrivateKey = 'generators/id_dsa';
    10 print_r($Routerboard->ListUpdate(array('tool', 'netwatch'), array('host', 'comment'), array()));
     10//print_r($Routerboard->ListGetPrint(array('queue', 'tree'), array('name', 'limit-at', 'max-limit', 'parent', 'packet-mark', 'queue')));
    1111//print_r($Routerboard->Execute('ip address {:foreach i in=[find] do={:put "address=".[get $i address]." network=".[get $i network])}}'));
    1212//print_r($Routerboard->GetItem('system ntp client print'));
    1313
     14print_r(ksort(array(1 => 'aaa', 0 => 'bbb')));
     15
    1416?>
  • trunk/system/routerboard.php

    r240 r241  
    109109  }
    110110
     111  function ListGetPrint($Path, $Properties, $Conditions = array())
     112  {
     113    $ConditionList = '';
     114    foreach($Conditions as $Index => $Item)
     115    {
     116      $ConditionList .= $Index.'="'.$Item.'" ';
     117    }
     118    $ConditionList = substr($ConditionList, 0, -1);
     119    if(trim($ConditionList) != '')
     120      $ConditionList = ' where '.$ConditionList;   
     121
     122    $Result = $this->Execute(implode(' ', $Path).' print terse'.$ConditionList);
     123    $List = array();
     124    foreach($Result as $ResultLine)
     125    {
     126      $ResultLineParts = explode(' ', $ResultLine);
     127      $ListItem = array();
     128      foreach($ResultLineParts as $ResultLinePart)
     129      {
     130        $Value = explode('=', $ResultLinePart);
     131        if(in_array($Value[0], $Properties))
     132        {
     133          if(count($Value) > 1)
     134          {
     135            if($Value[1]{0} == '"') $Value[1] = substr($Value[1], 1, -1);           
     136            //if(strlen($Value[1]) > 0)
     137            $ListItem[$Value[0]] = $Value[1];
     138          } else $ListItem[$Value[0]] = '';
     139        }
     140      }
     141      if(count($ListItem) > 0) $List[] = $ListItem;
     142    }
     143    return($List);
     144  }
     145
    111146  function ListEraseAll($Path)
    112147  {
     
    114149  }
    115150 
    116   function ListUpdate($Path, $Properties, $Values, $Condition = array())
    117   {
    118     $List = $this->ListGet($Path, $Properties, $Condition);
     151  function ListUpdate($Path, $Properties, $Values, $Condition = array(), $UsePrint = false)
     152  {
     153    if($UsePrint == 0) $List = $this->ListGet($Path, $Properties, $Condition);
     154      else $List = $this->ListGetPrint($Path, $Properties, $Condition);
    119155    $Commands = array();
    120156   
     
    129165      $Values[$Index] = $Item;     
    130166    }
    131    
     167    foreach($List as $Index => $Item)
     168    {
     169      foreach($Properties as $Property)
     170      {
     171        if(!array_key_exists($Property, $Item))
     172           $Item[$Property] = '';
     173      }
     174      $List[$Index] = $Item;     
     175    }
     176   
     177    // Sort properties
     178    foreach($Values as $Index => $Item)
     179    {
     180      ksort($Values[$Index]);
     181    }
     182    foreach($List as $Index => $Item)
     183    {
     184      ksort($List[$Index]);
     185    }
    132186    print_r($List);
    133187    print_r($Values);
     
    144198        }
    145199        $Prop = substr($Prop, 0, -1);
    146         $Commands[] = implode(' ', $Path).' remove [find '.$Prop.']';
     200        if(trim($Prop) != '')
     201          $Commands[] = implode(' ', $Path).' remove [find '.$Prop.']';
    147202      }
    148203    }
Note: See TracChangeset for help on using the changeset viewer.