I take script from this topic
http://forum.mikrotik.com/t/interface-data-quota/79514/1
and made some modifications to work with m ROS 7.15 ( RB951G-2HnD)
testing :-
I try to copy 2GB file from SAN Drive through Interface ether2 But no telegram message and ether2 is not Disable
( I try telegram message code alone and it works fine )
I check Interface ether2 traffic from winbox and notice that tx and rx are increased normally With copy size
One Notice: we I disable ether2 manually it enabled again by the script this means script is running and enable ether2 as quota is not reached
But it works forever even the quota exceeds 500MB.
Modified Code
:global persistVar
:global notifyCount
:do {
####################################################################################################
# User config
#Set limit in MB
:local limit 500
#Set day to reset limit
:local dayresetlimit "01"
#Set interface
:local interf "ether2"
#Set auto bring up interface
:local autointon true
#Delta startup value
:local startValue 0
####################################################################################################
:local notifyLimit 1
:local botId " "
:local chatId " "
:local site [/system identity get value-name=name]
:local day ([:pick [/system clock get date] 4 6])
:local mon ([:pick [/system clock get date] 0 3])
:local year ([:pick [/system clock get date] 7 11])
:local date "$day-$mon-$year"
:local time (:put [/system clock get time])
:local newdata
:local result
:local ref
:local status
:local tx [/interface get ether2 tx-byte]
:local rx [/interface get ether2 rx-byte]
:set $newdata (($rx + $tx) / 1024 / 1024)
:if ([:len [$persistVar wan1_counter]] < 1 ) do={
$persistVar wan1_counter 0
}
:if ([:len [$persistVar wan1_rebootblock]] < 1 ) do={
$persistVar wan1_rebootblock 1
}
:if ([:len [$persistVar wan1_offset]] < 1 ) do={
$persistVar wan1_offset 0
}
:if ([:len [$persistVar wan1_refdata]] < 1 ) do={
$persistVar wan1_refdata 0
}
:local rebootblocker ([$persistVar wan1_rebootblock])
:if ( $day = $dayresetlimit and $rebootblocker = 0) do={
$persistVar wan1_counter 0
$persistVar wan1_offset $newdata
$persistVar wan1_refdata 0
/interface set ether2 disabled=no
$persistVar wan1_rebootblock 1
:set notifyCount 0
:log warning "Resetting limits, int ether2 bring up"
}
:if ( $day != $dayresetlimit ) do={
$persistVar wan1_rebootblock 0
}
:local before [$persistVar wan1_counter]
:local offset [$persistVar wan1_offset]
:if ( $newdata < $before ) do={
$persistVar wan1_offset 0
:set $ref [$persistVar wan1_refdata]
:if ( $newdata < $ref ) do={
:set $ref (0)
}
:set $result ($before + $newdata - $ref + $startValue)
$persistVar wan1_refdata $newdata
$persistVar wan1_counter $result
} else={
:set $result ($newdata - $offset + $startValue)
$persistVar wan1_counter $result
}
:local traffic ([$persistVar wan1_counter])
:local percent ($traffic*100 / $limit)
:if ($percent >= 100) do={
:if ($notifyCount <= $notifyLimit) do={
/tool fetch url="https://api.telegram.org/bot$botId/sendMessage?chat_id=$chatId&text=MikroTik $site info:%0A[$date $time]%0AWAN1 disabled for exceeding bandwidth limit $limit MB" keep-result=no
:log warning "ether2 disabled for exceeding bandwidth limit $limit MB"
:set $notifyCount ($notifyCount + 1)
:delay 3
}
/interface set ether2 disabled=yes
}
:if ($percent < 100 and $autointon = true) do={
:set $status [/interface get ether2 value-name=disabled]
:if ($status = true) do={
/interface set ether2 disabled=no
}
}
} on-error={ :log error "Error WAN1_Limiter "}
I need some help