Community discussions

MikroTik App
 
rootER
just joined
Topic Author
Posts: 14
Joined: Mon Jun 02, 2008 4:24 pm

Noob Script Writer - Experienced RooterOS user

Mon Jun 02, 2008 4:40 pm

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")
 
dssmiktik
Forum Veteran
Forum Veteran
Posts: 732
Joined: Fri Aug 17, 2007 8:42 am

Re: Noob Script Writer - Experienced RooterOS user

Wed Jun 04, 2008 12:59 am

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.
 
rootER
just joined
Topic Author
Posts: 14
Joined: Mon Jun 02, 2008 4:24 pm

Re: Noob Script Writer - Experienced RooterOS user

Wed Jun 04, 2008 8:20 am

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?
 
dssmiktik
Forum Veteran
Forum Veteran
Posts: 732
Joined: Fri Aug 17, 2007 8:42 am

Re: Noob Script Writer - Experienced RooterOS user

Wed Jun 04, 2008 9:42 am

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.
 
dssmiktik
Forum Veteran
Forum Veteran
Posts: 732
Joined: Fri Aug 17, 2007 8:42 am

Re: Noob Script Writer - Experienced RooterOS user

Wed Jun 04, 2008 9:48 am

: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.
 
rootER
just joined
Topic Author
Posts: 14
Joined: Mon Jun 02, 2008 4:24 pm

Re: Noob Script Writer - Experienced RooterOS user

Wed Jun 04, 2008 10:06 am

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 :)
/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 :)
 
dssmiktik
Forum Veteran
Forum Veteran
Posts: 732
Joined: Fri Aug 17, 2007 8:42 am

Re: Noob Script Writer - Experienced RooterOS user

Wed Jun 04, 2008 11:31 am

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
 
rootER
just joined
Topic Author
Posts: 14
Joined: Mon Jun 02, 2008 4:24 pm

Re: Noob Script Writer - Experienced RooterOS user

Wed Jun 04, 2008 11:44 am

Thanks bud

it works now - :D :D 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 :D

Again i appreciate the help 8)
 
User avatar
Giepie
Member
Member
Posts: 433
Joined: Mon Sep 13, 2004 12:33 pm
Location: Western Cape, South Africa
Contact:

Re: Noob Script Writer - Experienced RooterOS user

Sun Jun 22, 2008 5:47 am

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
 
User avatar
Giepie
Member
Member
Posts: 433
Joined: Mon Sep 13, 2004 12:33 pm
Location: Western Cape, South Africa
Contact:

Re: Noob Script Writer - Experienced RooterOS user

Sun Jun 22, 2008 9:25 am

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
 
pjotr
just joined
Posts: 3
Joined: Mon Feb 23, 2009 10:53 pm

Re: Noob Script Writer - Experienced RooterOS user

Mon Feb 23, 2009 11:42 pm

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"
 
User avatar
kameelperdza
Member
Member
Posts: 468
Joined: Thu Nov 27, 2008 11:45 am
Location: Oudtshoorn, South Africa

Re: Noob Script Writer - Experienced RooterOS user

Thu Apr 23, 2009 10:55 am

Me 2..... :( Nothing is working? Not even http://wiki.mikrotik.com/wiki/Automated ... sermanager.

Can anyone help.
 
User avatar
Giepie
Member
Member
Posts: 433
Joined: Mon Sep 13, 2004 12:33 pm
Location: Western Cape, South Africa
Contact:

Re: Noob Script Writer - Experienced RooterOS user

Thu Apr 23, 2009 11:04 am

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
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7209
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Noob Script Writer - Experienced RooterOS user

Thu Apr 23, 2009 11:06 am

Nothing is working?
There is no such thing as 'Nothing is working'.
Run the script form the terminal and look for error messages.
 
User avatar
kameelperdza
Member
Member
Posts: 468
Joined: Thu Nov 27, 2008 11:45 am
Location: Oudtshoorn, South Africa

Re: Noob Script Writer - Experienced RooterOS user

Thu Apr 23, 2009 1:09 pm

Hi good news the Manual Usuage Report works. But im only getting a black email. wonder why? The monthend script is not working
 
User avatar
Giepie
Member
Member
Posts: 433
Joined: Mon Sep 13, 2004 12:33 pm
Location: Western Cape, South Africa
Contact:

Re: Noob Script Writer - Experienced RooterOS user

Thu Apr 23, 2009 1:16 pm

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.
 
User avatar
kameelperdza
Member
Member
Posts: 468
Joined: Thu Nov 27, 2008 11:45 am
Location: Oudtshoorn, South Africa

Re: Noob Script Writer - Experienced RooterOS user

Thu Apr 23, 2009 1:28 pm

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.
 
User avatar
kameelperdza
Member
Member
Posts: 468
Joined: Thu Nov 27, 2008 11:45 am
Location: Oudtshoorn, South Africa

Re: Noob Script Writer - Experienced RooterOS user

Thu Apr 23, 2009 1:55 pm

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.

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