LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Way to total the columns in a df -h (https://www.linuxquestions.org/questions/linux-newbie-8/way-to-total-the-columns-in-a-df-h-4175494929/)

anon091 02-14-2014 12:19 PM

Way to total the columns in a df -h
 
Hi everybody. I need to get total space available, total space used, and total space available for all my servers.

What I was planning to do was log into each of them, do a df -h, then add up the size, used, and avail columns manually.

Is there a better/faster/easier way to do this?

szboardstretcher 02-14-2014 01:00 PM

On a single machine to get "all" freespace into one number:

Code:

df -lP | awk '{total+=$4} END {printf "%d", total/2^20 + 0.5}'
You can build a script around running that on each machine and adding the numbers together.

anon091 02-14-2014 01:05 PM

Very cool, thanks. How do I tweak that to use for the other columns? change the $4 part?

szboardstretcher 02-14-2014 01:18 PM

Correct. $4 is the column number.

anon091 02-14-2014 02:12 PM

Great, thanks.

Firerat 02-14-2014 04:34 PM

Instead of using | to pipe the output of df to awk I would use this

Code:

< <(df -lP) awk '..awk stuff here ..'
Probably makes little difference here, but using that instead of | means you don't have awk in a subshell ( instead df is in the subshell, and its output is passed to awk )

A better example is a while read loop


Code:

unset Array Array2
while read a;do Array+=($a);done < <(df -l --output=avail);echo ${Array[@]}
df -l --output=avail|while read a;do Array2+=($a);done ;echo ${Array2[@]}

In the first, the bash array 'survives', where as the second it is lost


Anyway
You will almost certainly want to apply either in conjunction with a passwordless ssh login, using public keys

Example
Code:

for Server in Server1 Server2 Server3;do
    echo $Server free = $(ssh User@${server} "awk '/dev/{total+=$4} END {printf "%d", total/2^20 + 0.5}' < <(df -lP)")
done


anon091 02-14-2014 04:35 PM

pretty cool stuff there, thanks


All times are GMT -5. The time now is 10:56 AM.