Noob Script Writer - Experienced RooterOS user

Hi all

I am trying to write a script that will get the totals from all the simple queues and mail it to me at the close of business everyday.
I have achieved this the only problem i have is that i want all the results in one email not a email for each result.

This may be quite simple but i am new to scripting so any help would be appreciated.

I am running v3.10 and here is my current script:

:global texts " ";
:local traf;
:global megs "0";
:global resulter " "
/queue simple
    :for i from=60 to=198 do={
       :if ([/queue simple find name=("192.168.0.".$i)]!="") do={
            :set traf [get [find name=("192.168.0.".$i)] total-bytes]
            :set megs ($traf / 1000000)
            :if ($traf > 0) do={
            :set texts ("192.168.0.".$i. "--------------> ". $megs . " Mb")
                :log error ("192.168.0.$i" . "--------------" . "$megs" . " Mb")
                :set resulter ("192.168.0." .$i. "-------------->". $megs . "Mb")
                
                }
             }
        }

/tool e-mail send to="user@email.com" subject="testing it" body=("$resulter")

Try replacing :set resulter (“192.168.0.” .$i. “-------------->”. $megs . “Mb”)
with :set resulter ($resulter . “\n192.168.0.” .$i. “-------------->”. $megs . “Mb”)

This way, each time $resulter gets update with itself, plus the new entry. The \n is interpreted by the mail client as NEWLINE. You might also look at \r , not sure which one is which exactly for proper NEWLINE character.

Hope this helps.

Thanks for the reply.
It does not help, i still only get emailed the last record. Perhaps i need to update some sort of global variable in a WHILE loop? I am not too sure?

Are you sure you to got the resulter variable lowercase right? Here’s my test script:

:global resulter " ";
:for i from=60 to=198 do={
:set resulter ($resulter . " " . $i)
}

:log error $resulter


The variable $resulter was appended each iteration of the for loop. so the output was 60 61 62 .. 196 197 198. You might try replacing:
:set resulter ($resulter . “\n192.168.0.” .$i. “-------------->”. $megs . “Mb”)
with
:global resulter ($resulter . “\n192.168.0.” .$i. “-------------->”. $megs . “Mb”)

Let me know if that works for you. I did this test script in v2.9.46 with no probs. Maybe 3.x won’t update global variables unless specifically told to.

:set megs ($traf / 1000000)
:if ($traf > 0) do={

Another thought… are you sure that when you run the script that $traf is greater than zero after dividing ? It may be that traffic is below 1 Mb/s which wouldn’t get reported. If so, you could just divide by 1000 to get Kb/s instead.

Hey dssmiktik

Thanks again for your reply.
yes it does iterate in the log file but i want the final step to be that it emails me in one email all the IP’s that are above 1Mb :slight_smile:

/tool e-mail send to="user@email.com" subject="testing it" body=("$resulter")

So the email should look like this:

192.168.0.1 ---------> 10Mb
192.168.0.2 ---------> 9Mb
192.168.0.3 ----------> 7Mb
ect
ect

Instead it just mails me the last result ie: 192.168.0.198 ----------> 25Mb

I appreciate the help :slight_smile:

Here’s my test straight from working script. I found a few things.. first put a semicolon after the :global resulter line near top. Make sure that there’s a space between the dot on both sides in any string to variable concatenation. The last log statement worked perfect and interpreted the \n as NEWLINE as expected.

Replace the 101 with 0 and the from=134 to=139 to your start and end IPs. Hope this fixes it for ya. If you’re still having troubles, feel free to email directly at dscomputer_2000 [at] hotmail dot com.


:global texts " ";
:local traf;
:global megs “0”;
:global resulter " ";

/queue simple
:for i from=134 to=139 do={
:if ([/queue simple find name=(“192.168.101.” . $i)] != “” ) do={
:set traf [get [find name=(“192.168.101.” . $i)] total-bytes]
:set megs ($traf / 1000000)

:if ($traf > 0) do={
:set texts (“192.168.101.” . $i . “--------------> " . $megs . " Mb”)
:log error (“192.168.101.” . $i . “-------------->” . $megs . " Mb")
:set resulter ($resulter . “\n 192.168.101.” . $i . “-------------->” . $megs . “Mb”)
}

}
}

\

here’s where your email line was, this was just for verification

:log error $resulter

Thanks bud

it works now - :smiley: :smiley: this is where i was going wrong i had not added the variable here:

:set resulter (> $resulter . > “\n 192.168.101.” . $i . “-------------->” . $megs . “Mb”)

Happy days :smiley:

Again i appreciate the help :sunglasses:

Hi all

Congrats on the script!

I attempted my own version using a foreach loop, but this way seems to work better for the particular task. I would like to change the script so it not only sends me “total-bytes” but instead “total-tx-byte” and “total-rx-bytes”. I noted that MT only has two options, either “total-bytes” or “bytes”.

Where “total-bytes” is, displayed as: “12345”, “bytes” on the other hand is displayed as this: “12345/12345”.

Does anyone know how to seperate the TX from the RX bytes?

All help would be greatly appreciated! G

I managed to get it right!

It took me a few hours, but it was worth it! Feel free to use, modify and abuse!




:log info “Starting QUEUE Statistics Checker”

#####DEFINE VARIABLES#####
:local slashchecker
:local slashcheckertotal
:local bytecountedup
:local bytecounteddown
:local megcountedup
:local megcounteddown
:local slashcounter
:local precalcbytes
:local pickbegin
:local pickend
:local slashcounted
:local slashcounted1
:local numberofqueues
:local test
:local interfacename
:local emailvariable
:local highsitename
:local fromemailaddress
:local toemailaddress
#####VARIABLES DEFINED#####

########## CHANGE INDIVIDUAL DETAILS HERE: ##########
:set fromemailaddress “highsite@domain.co.za
:set toemailaddress “admin@domain.co.za
########## END OF INDIVIDUAL DETAILS ##########

#####COUNT NUMBER OF QUEUES#####
:set numberofqueues [/queue simple print count-only]
:set highsitename [/system identity get name]
##ADD DETAILS TO E-MAIL VARIABLE##
:set emailvariable "QUEUE STATISTICS FOR:
$highsitename

TOTAL NUMBER OF QUEUES: $numberofqueues"
##ADD DETAILS TO E-MAIL VARIABLE DONE##

#####BEGIN MAIN LOOP FOR QUEUE PROCESSING#####
/queue simple
:foreach n in=[/queue simple find priority=8] do={
:local interfacename [/queue simple get [$n] name]

#####SEPERATE UP/DOWN BYTES FROM QUEUE#####
##PRESET VARIABLES##
:local precalcbytes [:pick [/queue simple get [$n] bytes] 0 30]
:set slashcheckertotal $precalcbytes
:set pickbegin 1
:set pickend 2
:set slashcounter 0

#####START SLASH CHECKER#####
/queue simple
:for i from=1 to=20 do={

:set pickbegin (pickbegin + 1)
:set pickend (pickend + 1)
:set slashchecker [:pick $slashcheckertotal $pickbegin $pickend]

:if ($slashchecker = “/”) do={
:set slashcounted $pickbegin } else={ :set slashcounter ($slashcounter +1)}
}
#:log info “SLASHCOUNTED: $slashcounted”
#####SLASHCHECKER LOOP END#####

#####GOT SLASH PICK COUNT, STARTING BYTES UP AND DOWN SPLIT#####
:set slashcounted1 ($slashcounted + 1)
:set bytecountedup [:pick $precalcbytes 0 $slashcounted]
:set bytecounteddown [:pick $precalcbytes $slashcounted1 30]
:set megcountedup ($bytecountedup / 1000000)
:set megcounteddown ($bytecounteddown / 1000000)
#:log info “INTERFACE NAME: $interfacename”
#:log info “PRECALCBYTES: $precalcbytes”
#:log info “BYTECOUNTEDUP $bytecountedup BYTES”
#:log info “BYTECOUNTEDDOWN $bytecounteddown BYTES”
#:log info “MEGCOUNTEDUP $megcountedup MB”
#:log info “MEGCOUNTEDDOWN $megcounteddown MB”
#:log info " "
#:log info " "
##ADD ABOVE INFO TO EMAIL VARIABLE##
:set emailvariable "$emailvariable

QUEUE NAME:\t\t\t$interfacename
TOTAL BYTES:\t\t$precalcbytes
UPLOADED BYTES:\t\t$megcountedup MB
DOWNLOADED BYTES:\t\t$megcounteddown MB

"
##UPDATE OF EMAIL VARIABLE DONE##

}
#####END OF QUEUE PROCCESSOR LOOP#####


#####SENDING DETAIL TO EMAIL#####
:set emailvariable “$emailvariable
END OF QUEUE STATISTICS FOR: $highsitename”

/tool e-mail send from=$fromemailaddress to=$toemailaddress server=196.25.240.94 subject=“$highsitename QUEUE Statistics” body=$emailvariable
#####END OF SENDING EMAIL#####

:log info “Total number of queues processed: $numberofqueues”
:log info “QUEUE Statistics Check DONE and E-Mailed to $toemailaddress”



Re, G

In the version 3.20 does not work
Please advice
Thanks


\

[quote=“Giepie”]I managed to get it right!

It took me a few hours, but it was worth it! Feel free to use, modify and abuse!




:log info “Starting QUEUE Statistics Checker”

Me 2… :frowning: Nothing is working? Not even http://wiki.mikrotik.com/wiki/Automated_Usage_Script_without_usermanager.

Can anyone help.

Hi KameelperdZA! (vanaf 'n donderstormende weskaap!)

I would like to help you with your problems.

I am still using the same script (perhaps a few additions and modifications) and it works with all versions except MTROS4B. Could you perhaps give some more info on what exactly isn’t working, and perhaps some log entries?

Since you’re from SA, I could help you telephonically if you want, but it would be nice if we could keep others in the loop too.

G

There is no such thing as ‘Nothing is working’.
Run the script form the terminal and look for error messages.

Hi good news the Manual Usuage Report works. But im only getting a black email. wonder why? The monthend script is not working

Are you getting a BLACK email or BLANK email? If the email is BLACK, I don’t believe the problem is with the script. The mail shouldn’t be blank either. Perhaps put some text in the body field of the /tool e-mail command and see if it makes any difference.

Sorry yes it is a blank email. In the script it says subject=“Manual usage report” body="Site Usage report - Runtime: $time
$text

But there is nothing. I have 2 qeue lists.

Hi Giepie your script is working great thanx. I tried yours now and it is working. Do you have a script that i can use for monthend? Please let me know