modified on 8 May 2013 at 07:24 ••• 9,977 views

Automatically Create Simple Queues

From MikroTik Wiki

Jump to: navigation, search

Contents

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


I schedule both the update and reporting script using the scheduler.

The update script I execute every 15min and the reporting script once a day


Create Queues

This script is used to created simple queues for each address found in the ip firewall address list belonging to the list specified in the script

  

       :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 queues simpel looks like:

add burst-limit=0/0 burst-threshold=0/0 burst-time=0s/0s comment= 192.168.20.216*1691120 direction=both disabled=no interface=all limit-at= 0/0 max-limit=0/0 name=192.168.20.216 packet-marks="" parent=none priority=

8 queue=default/default target-addresses=192.168.20.216/32 total-queue=default


  

    :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.


       :global scriptemail 
       
       :local content
       :local i
       :local queuehostname
       :local queuehostip
       
       :local bytessaved
       :local megssaved       
       
       :local logcontenttemp ""       
       :local logcontent ""          

       :set logcontenttemp "Good Day \n\r"
       :set logcontent ("$logcontent
" ."$logcontenttemp")
       
       :set logcontenttemp "This is an automated notification, please do not reply to this email"
       :set logcontent ("$logcontent
" ."$logcontenttemp")

       :set logcontenttemp "Please see below the usage stats for your ip queue: \n\r"
       :set logcontent ("$logcontent
" ."$logcontenttemp")
       
       :set logcontenttemp "**************************************Simple Queue*************************************"
       :set logcontent ("$logcontent
" ."$logcontenttemp")

       :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 queuehostname [:pick $content 0 ($pos1)]
                     :set queuehostip [/queue simple get $i target]
       
                     :set bytessaved ([:pick $content ($pos1+1) $pos2])
                     :set megssaved ($bytessaved  / 1048576)      

                     :set logcontenttemp "$queuehostname"

                     :for x from=1 to=( 55 - [:len $logcontenttemp]) step=1 do={ 
                            :set logcontenttemp ("$logcontenttemp" . " ")
                     }
                            
                     :set logcontenttemp ("$logcontenttemp" . " Used: " . "$megssaved" . "mb") 
                     :set logcontent ("$logcontent
" ."$logcontenttemp")     

            
              }

        }       

       :set logcontenttemp "\n\r" 
       :set logcontent ("$logcontent
" ."$logcontenttemp")         

       :set logcontenttemp "***************************************************************************************"                                         
       :set logcontent ("$logcontent
" ."$logcontenttemp")     
       
       :set logcontenttemp "KEY:"
       :set logcontent ("$logcontent
" ."$logcontenttemp")

       :set logcontenttemp "1 Megabyte (Mb) = 1000000 bytes (b)"
       :set logcontent ("$logcontent
" ."$logcontenttemp")

       :set logcontenttemp "1 Gigabyte (Gb) = 1000 Megabytes (Mb) \n\r"
       :set logcontent ("$logcontent
" ."$logcontenttemp")

       :set logcontenttemp "Should you have any queries, please contact your account manager"
       :set logcontent ("$logcontent
" ."$logcontenttemp")

       :set logcontenttemp "\n\r" 
       :set logcontent ("$logcontent
" ."$logcontenttemp")         

       :set logcontenttemp "Kind Regrads"
       :set logcontent ("$logcontent
" ."$logcontenttemp")

        /tool e-mail send to=$scriptemail   subject="$[/system identity get name] queue usage report"  body="$logcontent" tls=yes
       
 


Please feel free to email me if you have any questions werner.venter.mail@gmail.com