Community discussions

MikroTik App
 
eldoncito2019
Member
Member
Topic Author
Posts: 333
Joined: Fri Jun 14, 2019 1:07 pm

Get free HDD space percent

Wed Dec 07, 2022 10:11 pm

I have the following script
{
/system resource
:local totalhdd [get total-hdd-space]
:local freehdd [get free-hdd-space]
:local percfree ($totalhdd-$freehdd/$totalhdd*100)
:put "Free Space: $freehdd"
:put "Total Space: $totalhdd"
:put ($percfree."% freehdd space")
}
but I don't get the desired result, please help me.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3253
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: Get free HDD space percent

Wed Dec 07, 2022 11:26 pm

Ah, divide... This one is dozy. I'm not sure this is readily possible since Mikrotik doesn't support "floating point" numbers (e.g. with decimal point in US). They do integers, which are whole/counting numbers. When you divide numbers, it reports the nearest whole number. So 5/10 is 0 too. They do support "getting the remainder", modulo "%", but this too isn't much use since the modulo here would be the same as dividend (e.g. the disk freespace it got).
:put (5 / (10))
0
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Get free HDD space percent  [SOLVED]

Thu Dec 08, 2022 9:37 am

First of all, if for example free-hdd-space=113070080 and total-hdd-space=134217728
this operation
($totalhdd - $freehdd / $totalhdd * 100)
is = to
(134217728 - 113070080 / 134217728 * 100)
and the result inside routeros is (134217728 - 0 * 100) = (134217728 - 0) = 134217728 the total hdd space
Because / and * take precedence over -
The correct operation to have "% freehdd space" from the proportion
$totalhdd : 100 = $freehdd : x
x = (100 * $freehdd) / $totalhdd
The division must be last operation, because RouterOS do not support floating values.
If you want decimals, multiply the numbers by 10, 100, etc. before the division, and use [:pick] for add the comma/dot (language specific)

{
/system resource
:local totalhdd [get total-hdd-space]
:local freehdd [get free-hdd-space]
:local percfree ((100 * $freehdd) / $totalhdd)
:put "Space: Total $totalhdd, Free $freehdd"
:put "$percfree% free disk space, $(100 - $percfree)% busy"
# 2 decimals = 2 significant zeros (for prevent results under 1,00% like  _,_0%), number * 100 and :pick the last 2 digit
:local perdec "00$((100 * $freehdd * 100) / $totalhdd)"
:set perdec "$[:tonum [:pick $perdec 0 ([:len $perdec]-2)]],$[:pick $perdec ([:len $perdec]-2) [:len $perdec]]"
:put "$perdec% free disk space"
}

If you want "nice" formatted space(s) value on MiB (RouterOS style is MiB with 1 decimal):
{
/system resource
:local totalhdd [get total-hdd-space]
:local freehdd  [get free-hdd-space]

:set totalhdd "0$(($totalhdd * 0xA) / 0x100000)"
:set totalhdd "$[:tonum [:pick $totalhdd 0 ([:len $totalhdd]-1)]],$[:pick $totalhdd ([:len $totalhdd]-1) [:len $totalhdd]]"
:set freehdd  "0$(($freehdd * 0xA) / 0x100000)"
:set freehdd  "$[:tonum [:pick $freehdd 0 ([:len $freehdd]-1)]],$[:pick $freehdd ([:len $freehdd]-1) [:len $freehdd]]"

:put "Space: Total $totalhdd MiB, Free $freehdd MiB"
}

Who is online

Users browsing this forum: No registered users and 19 guests