Calculate used precentage of multi device btrfs /

  • 15 September 2022
  • 4 replies

Badge +2


I have some checks for btrfs usage that are failing. The problem lies in the / filesystem configuration which on those machines have 2 devices.


For example, the btrfs output for one is:

myhost $ btrfs filesystem sh --raw /
Label: none  uuid: 0cd6d969-fddf-4eba-92fd-8826a9a73376
        Total devices 2 FS bytes used 13946572800
        devid    1 size 15025045504 used 11143217152 path /dev/sdu2
        devid    2 size 11811160064 used 5939134464 path /dev/sdu3


The check uses this line for the calculation:

ocupatpercent=`sudo /usr/sbin/btrfs filesystem sh --raw $volum | awk '$1=="devid" {print int( 100-((($4-$6)/$4)*100) )}'`

which calculates de used percentage for each device so instead of one value:

myhost $ echo $ocupatpercent
74 50


My quick solution, replacing that one line in the script is this:

# saves total space of both devices ($4) in an array
total=( `sudo /usr/sbin/btrfs filesystem sh --raw $volum | awk '$1=="devid" {print $4}'` )
# saves used space of both devices  ($6) in an array
usage=( `sudo /usr/sbin/btrfs filesystem sh --raw $volum | awk '$1=="devid" {print $6}'` )
# sums each device total space
total_space=$(( ${total[0]}+${total[1]} ))
# sums each device used space
total_usage=$(( ${usage[0]}+${usage[1]} ))      
# calculates used percentage from the sum of both total and used space
ocupatpercent=$(( $total_usage*100/$total_space ))      


So basically, I sum the total space and the total used space for both devices and use that to calculated the used percentage of /.

So my first question is, is this the correct way to calculate used space for this setup of btrfs?

And, how can I improve the calculation (making it simpler and clearer)?





Best answer by emeck 16 September 2022, 16:27

View original

4 replies

Badge +2

Ok, I have reduce it to one line again:


ocupatpercent=`sudo /usr/sbin/btrfs filesystem sh --raw $volum | awk '$1=="devid"{size+=$4;used+=$6;}END{print (used*100/size)}'`


Works fine with the test I've done, for one or more devices for the btrfs filesystem. My concern still is if that is the way for calculating total size and used space for a filesystem setup like that.




Badge +2

Correction, forgot “int” for the “print” part:


ocupatpercent=`sudo /usr/sbin/btrfs filesystem sh --raw $volum | awk '$1=="devid" {size+=$4;used+=$6;}END{print int(used*100/size)}'`

Userlevel 6
Badge +17


I’m not aware of any centreon-plugins providing btrfs check, are you refering to a custom/home made script?


If so, we could not help a lot. 

It you would like to have a snmp-based check, we can help you with this. We will need a snmpwalk from the following snmp branches: 

  • .

  • .

  • .


Badge +2



Yes, it is a custom check for nrpe. It seems to work for now with the above change.

We still have a lot of hosts with nrpe and some with snmp for basic checks. When this multi device btrfs host are changed to snmp, I’ll post again if there is any trouble.