Automatically Create Simple Queues

From MikroTik Wiki
Revision as of 08:54, 8 May 2013 by Wcsnet (talk | contribs)
Jump to: navigation, search

Summery

I had the need to be able to create simples queues based on addresses added to the ip firewall address list. A requirement was also to be able to report on the bandwidth used per queue .

I this example I have a firewall rule to log the source address of traffic going out of the router wan interface.

add action=add-src-to-address-list address-list="i was on the internet" address-list-timeout=0s chain=forward comment="log-src address of forward wan traffic" disabled=no \
    out-interface=wan 

The script set consist of 4 scripts. • Create Queues • Update Bandwith Usage • Report Bandwith Usage • Reset Batwith Usage

  

       :local hostip
       :local list
       :local testvalue
      
       :log info "******************** starting - address list queue create ********************"
 
       :foreach i in=[/ip firewall address-list find] do={

              :set list ([/ip firewall address-list get $i list] )
              :set hostip ([/ip firewall address-list get $i address] )

              :set testvalue [/queue simple find name=$hostip]

              :if ([:len $testvalue] <= 0) do={
                     :if ($list = "i was on the internet") do={

                             /queue simple add name=$hostip comment="$hostip*0"  target=$hostip queue=default/default total-queue=default

                      } else={
           
                                 }
                      } else={
                         
                        }
       }
       
       :log info "******************** ending - address lsit queue create ********************"
       


Update Statistics

The update script makes use of the comment fields to store items descriptions and statistics (Bytes Used) A example comment filed for IP Firewall Filter looks like:

  

    :local content
       :local i
       
       :local megstotal
       :local bytestotal
       :local bytescurrent
       
       :local bytessaved
       :local megssaved
       
       :local bytesdowncurrent
       :local megsdowncurrent
       
       :local megsupcurrent
       :local bytesupcurrent
       
       :log info "******************** starting - queue usage update ********************"
       
       :foreach i in=[/queue simple find comment !=""] do={
       
              :set content [/queue simple get $i comment]
       
              :if ([:find $content "*"] != "") do={
                     :local pos1 [:find $content "*"]
                     :local pos2 [:len $content]
       
                     :set bytescurrent [/queue simple get $i bytes]
       
                    :local pos3 [:find $bytescurrent "/"]
                    :local pos4 [:len $bytescurrent]
       
                    :set bytessaved ([:pick $content ($pos1+1) $pos2])
                    :set megssaved ($bytessaved  / 1048576)
              
                    :set bytesupcurrent ([:pick $bytescurrent 0 ($pos3)])
                    :set megsupcurrent ($bytesupcurrent / 1048576)
       
                    :set bytesdowncurrent ([:pick $bytescurrent ($pos3+1) $pos4])
                    :set megsdowncurrent ($bytesdowncurrent  / 1048576)
       
                    :set megstotal ($megsupcurrent + $megsdowncurrent + $megssaved)
                    :set bytestotal ($bytesupcurrent + $bytesdowncurrent + $bytessaved)     
       
                     /queue simple set $i comment="$[:pick $content 0 ($pos1)]*$bytestotal"
                     /queue simple reset-counters $i
                 }
       }
       :log info "******************** ending - queue usage update ********************"

  

== '''The Reset Script''' ==


The reset script will reset all counters back to a *0

       :local content
       :local i
       
       :log info "******************** starting - queue usage update reset********************"
       
       :foreach i in=[/queue simple find comment !=""] do={
       
              :set content [/queue simple get $i comment]
       
              :if ([:find $content "*"] != "") do={
                     :local pos1 [:find $content "*"]
                     :local pos2 [:len $content]
       
                     /queue simple set $i comment="$[:pick $content 0 ($pos1)]*0"
                     /queue simple reset-counters $i
              }
       }
       :log info "******************** ending - queue usage update reset ********************"


The Report Script.

The report script will look for items in the ip queues simpel which has a * in the comment field. The reported unit would be in MB.