rderoldan1 / usagewatch_ext

Extended version of usagewatch
MIT License
21 stars 9 forks source link

On Mac, uw_load is wrong when there are multiple volumes mounted #7

Open aurelienshz opened 4 years ago

aurelienshz commented 4 years ago

On a Mac, with an external disk or a dmg mounted, the columns in the output of the iostat command get shifted to the right (each disk adds 3 columns). This causes the result of uw_load to be very wrong.

Reproduction steps:

Mount a dmg to add a new volume.

Usagewatch.uw_load
# => 11.0

Eject the dmg:

Usagewatch.uw_load
# => 2.78

Why is this happening?

The command used to get the current CPU load is iostat -w1 -c 2 | awk '{print $7}'.

That's happening because, with a mounted volume, the output of iostat -w1 -c 2 contains 3 more columns to the left of the load average columns, which messes up the result of the awk command.

# With an external volume:
$ iostat -w1 -c 2
              disk0               disk2       cpu    load average
    KB/t  tps  MB/s     KB/t  tps  MB/s  us sy id   1m   5m   15m
   18.83   29  0.53   352.03    0  0.00  15 13 72  1.97 2.44 2.99
    4.00   14  0.05     0.00    0  0.00   6  9 85  1.97 2.44 2.99

# Without an external volume:
$ iostat -w1 -c 2
              disk0       cpu    load average
    KB/t  tps  MB/s  us sy id   1m   5m   15m
   18.83   29  0.53  15 13 72  2.54 2.53 3.01
    6.13   30  0.18  32 24 44  2.90 2.60 3.03

Cc @Lyriaaw

aurelienshz commented 4 years ago

Potential fix: use echo $x | awk '{print $(NF-2)}'. This would take the 3rd to last column (ie 3rd starting from the rightmost column) instead of the 7th (starting from the left). Let me know if you can see any good reason not to do this, otherwise I'm happy to submit a PR!