Community discussions

MikroTik App
 
rubence
just joined
Topic Author
Posts: 24
Joined: Mon Jan 24, 2011 10:06 am

notify the user about their consumption

Mon Jan 24, 2011 10:14 am

hi,

i want to write a script which will notifiy user by email about their total use. it'll take the total-bytes (if possible download bytes and upload bytes separtely) from simple queue from a specific queue name and send email to a specific email address.

plz help
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7055
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: notify the user about their consumption

Mon Jan 24, 2011 10:38 am

You can look for similar examples in our wiki
http://wiki.mikrotik.com/wiki/Scripts
 
rubence
just joined
Topic Author
Posts: 24
Joined: Mon Jan 24, 2011 10:06 am

Re: notify the user about their consumption

Tue Jan 25, 2011 6:04 pm

thanks mrz,

that page helped me a lot. now i can do email with the information of total-bytes for a queue. the problem is when my MT get reboot all information of total-bytes from simple queue get lost and it starts again from 0.
 
rubence
just joined
Topic Author
Posts: 24
Joined: Mon Jan 24, 2011 10:06 am

Re: notify the user about their consumption

Sun Jan 30, 2011 4:16 pm

reset-counter when reboot is a very discussed problem or limitation of MT. can we take the bandwidth consumption information from any other source ? MT is creating graph for bandwidth consumption for each queue which is stored and remain unchanged when router gets reboot. can we use this information from graph to resolve my problem ?
 
fewi
Forum Guru
Forum Guru
Posts: 7717
Joined: Tue Aug 11, 2009 3:19 am

Re: notify the user about their consumption

Sun Jan 30, 2011 5:56 pm

No.

Use an external SNMP solution for reliable graphing.
 
wcsnet
Frequent Visitor
Frequent Visitor
Posts: 64
Joined: Mon Apr 29, 2013 12:43 pm
Location: South Africa

Re: notify the user about their consumption

Thu Jul 31, 2014 12:08 pm

: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 ********************"
:log info "-"

:foreach i in=[/queue simple find] 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 "-"
}
}

:log info "******************** ending - queue usage update ********************"



And the report script


:global scriptemail

:local content
:local i
:local testvalue

:local hostdnsname
:local queuehostname
:local queuehostip

:local bytessaved
:local megssaved
:local gigssaved

:local logcontenttemp ""
:local logcontent ""

:log info "******************** starting - queue usage email report ********************"
:log info "-"

: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 "From $[/file get "stats reset" contents] to $[/system clock get date] \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 testvalue [/ip dns static find address=$queuehostname]

:if ([:len $testvalue] > 0) do={
:set hostdnsname [/ip dns static get value-name=name number=[find address=$queuehostname]]
} else={
:set hostdnsname "no dns record"
}

:set bytessaved ([:pick $content ($pos1+1) $pos2])
:set megssaved ($bytessaved / 1048576)
:set gigssaved ($megssaved / 1000)

:set logcontenttemp "$queuehostname"

:for x from=1 to=( 15 - [:len $logcontenttemp]) step=1 do={
:set logcontenttemp ("$logcontenttemp" . " ")
}

:set logcontenttemp ("$logcontenttemp" . "$hostdnsname")

:for x from=1 to=( 50 - [:len $logcontenttemp]) step=1 do={
:set logcontenttemp ("$logcontenttemp" . " ")
}

:if ($gigssaved >=1) do={
:set logcontenttemp ("$logcontenttemp" . " Used: " . "$gigssaved" . "g")
:set logcontent ("$logcontent
" ."$logcontenttemp")
} else={
: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 report" body="$logcontent" start-tls=yes

:log info "******************** ending - queue usage email report ********************"
You do not have the required permissions to view the files attached to this post.
 
wcsnet
Frequent Visitor
Frequent Visitor
Posts: 64
Joined: Mon Apr 29, 2013 12:43 pm
Location: South Africa

Re: notify the user about their consumption

Thu Jul 31, 2014 12:10 pm

Report looks like this
You do not have the required permissions to view the files attached to this post.

Who is online

Users browsing this forum: No registered users and 25 guests