Community discussions

MikroTik App
 
iqbaldalban
just joined
Topic Author
Posts: 4
Joined: Sat Aug 24, 2019 11:55 am
Contact:

Change bytes to mb or gb in simple queue for telegram bots

Sun May 02, 2021 12:25 pm

hello, I have a script for monitoring simple queues using a telegram bot, but for data usage it only displays bytes, is there a way to change it to mb or gb to make it easier to read?
:local download;
:local upload;
:local bot "BOT ID";
:local chat "CHAT ID";
:local tanggal "Tanggal: $[/system clock get date] %0A";
:foreach queue in=[/queue simple find parent="CLIENT HOTSPOT"] do={
:local name ("Name".[/queue simple get value-name="name" $queue]."%0A")
:local byte ("Usage".[/queue simple get value-name="byte" $queue]." Bytes %0A")
:local trafik ("Trafik ".[/queue simple get value-name="rate" $queue]." %0A")
:local limit ("Limit ".[/queue simple get value-name="max-limit" $queue]." %0A")
:set download ($name.$byte.$trafik.$limit)
/tool fetch url="https://api.telegram.org/bot$bot/sendmessage\?chat_id=$chat&text=Pemakaian internet: %0A$tanggal$download" keep-result=no;
}
 
joegoldman
Forum Veteran
Forum Veteran
Posts: 766
Joined: Mon May 27, 2013 2:05 am

Re: Change bytes to mb or gb in simple queue for telegram bots

Sun May 02, 2021 2:06 pm

You can use simple arithmetic operators on returned values i.e. bytes -> kilobytes is divide bytes by 1024 (( [bytes] / 1024) for instance), where 5120 bytes becomes 5kilobytes, continue up the chain by dividing 1024 t o get to desired value

bytes / 1024 = kilobytes
kb / 1024 = megabytes
mb / 1024 = gigabytes
gb / 1024 = terrabytes

so on...
 
iqbaldalban
just joined
Topic Author
Posts: 4
Joined: Sat Aug 24, 2019 11:55 am
Contact:

Re: Change bytes to mb or gb in simple queue for telegram bots

Mon May 03, 2021 7:57 am

You can use simple arithmetic operators on returned values i.e. bytes -> kilobytes is divide bytes by 1024 (( [bytes] / 1024) for instance), where 5120 bytes becomes 5kilobytes, continue up the chain by dividing 1024 t o get to desired value

bytes / 1024 = kilobytes
kb / 1024 = megabytes
mb / 1024 = gigabytes
gb / 1024 = terrabytes

so on...
i tried to apply it like this but it still doesn't work

:local download;
:local upload;
:local bot "BOT ID";
:local chat "CHAT ID";
:local tanggal "Tanggal: $[/system clock get date] %0A";
:foreach queue in=[/queue simple find parent="CLIENT"] do={
:local name ("Name".[/queue simple get value-name="name" $queue]."%0A")
:local byte ("Usage".[/queue simple get value-name="byte" $queue]." Bytes %0A")
:local byteMB ("Total Usage".($byte / 1024 / 1024)." Mb%0A")
:local trafik ("Trafik ".[/queue simple get value-name="rate" $queue]." %0A")
:local limit ("Limit ".[/queue simple get value-name="max-limit" $queue]." %0A")
:set download ($name.$byteMB.$trafik.$limit)
/tool fetch url="https://api.telegram.org/bot$bot/sendmessage\?chat_id=$chat&text=Internet usage: %0A$tanggal$download" keep-result=no;
}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Change bytes to mb or gb in simple queue for telegram bots

Mon May 03, 2021 2:45 pm

:global bot "BOT ID"
:global chat "CHAT ID"
:global download ""
# unused :global upload ""
:global tanggal "Tanggal:+$[/system clock get date]%0A"
/queue simple
:foreach queue in=[find parent="CLIENT"] do={
 :local name ("Name+".[get $queue "name"]."%0A")
 :local ubyte ("Usage+".[get $queue "bytes"]."+Bytes%0A")
 :local totalbyte ("Total+Usage+".[get $queue "total-bytes"]."+Bytes%0A")
 :local trafik ("Trafik+".[get $queue "rate"]."%0A")
 :local limit ("Limit+".[get $queue "max-limit"]."%0A")
 :set download ($name.$ubyte.$totalbyte.$trafik.$limit)
 /tool fetch keep-result=no url="https://api.telegram.org/bot$bot/sendmessage\?chat_id=$chat&text=Internet+usage:%0A$tanggal$download"
}
On terminal, for test keep \ before ?, but on script remember to remove it...

"total-bytes" and "bytes" returning only pair of values like "3454401/2081207"
It's useless multiply It...
must be splitted first...
Last edited by rextended on Tue May 04, 2021 12:58 am, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Change bytes to mb or gb in simple queue for telegram bots

Tue May 04, 2021 12:31 am

You can use simple arithmetic operators on returned values i.e. bytes -> kilobytes is divide bytes by 1024 (( [bytes] / 1024) for instance), where 5120 bytes becomes 5kilobytes, continue up the chain by dividing 1024 t o get to desired value

bytes / 1024 = kilobytes
kb / 1024 = megabytes
mb / 1024 = gigabytes
gb / 1024 = terrabytes

so on...

Please update your knowledge.

1000 bit = 1 kilobit (kb)
1000 kb = 1 Megabit (Mb)
1000 Mb = 1 Gigabit (Gb)
1000 Gb = 1 Terabit (Tb)

8 bit = 1 Byte
1000 Byte = 1 kiloByte (kB)
1000 kB = 1 MegaByte (MB)
1000 MB = 1 GigaByte (GB)
1000 GB = 1 TeraByte (TB)

8 bit = 1 Byte
1024 Byte = 1 KibiByte (KiB)
1024 KiB = 1 MebiByte (MiB)
1024 MiB = 1 GibiByte (GiB)
1024 GiB = 1 TebiByte (TiB)

MikroTik follow the standard from 1999
https://en.wikipedia.org/wiki/Binary_pr ... ST_and_ISO
 
iqbaldalban
just joined
Topic Author
Posts: 4
Joined: Sat Aug 24, 2019 11:55 am
Contact:

Re: Change bytes to mb or gb in simple queue for telegram bots

Tue May 04, 2021 4:55 am

:global bot "BOT ID"
:global chat "CHAT ID"
:global download ""
# unused :global upload ""
:global tanggal "Tanggal:+$[/system clock get date]%0A"
/queue simple
:foreach queue in=[find parent="CLIENT"] do={
 :local name ("Name+".[get $queue "name"]."%0A")
 :local ubyte ("Usage+".[get $queue "bytes"]."+Bytes%0A")
 :local totalbyte ("Total+Usage+".[get $queue "total-bytes"]."+Bytes%0A")
 :local trafik ("Trafik+".[get $queue "rate"]."%0A")
 :local limit ("Limit+".[get $queue "max-limit"]."%0A")
 :set download ($name.$ubyte.$totalbyte.$trafik.$limit)
 /tool fetch keep-result=no url="https://api.telegram.org/bot$bot/sendmessage\?chat_id=$chat&text=Internet+usage:%0A$tanggal$download"
}
On terminal, for test keep \ before ?, but on script remember to remove it...

"total-bytes" and "bytes" returning only pair of values like "3454401/2081207"
It's useless multiply It...
must be splitted first...
and how to do it, do you have an example for me to make as a reference?

Who is online

Users browsing this forum: rogerioqueiroz and 17 guests