LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-14-2014, 12:19 PM   #1
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,697

Rep: Reputation: 48
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?
 
Old 02-14-2014, 01:00 PM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,208

Rep: Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611
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.
 
1 members found this post helpful.
Old 02-14-2014, 01:05 PM   #3
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,697

Original Poster
Rep: Reputation: 48
Very cool, thanks. How do I tweak that to use for the other columns? change the $4 part?
 
Old 02-14-2014, 01:18 PM   #4
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,208

Rep: Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611Reputation: 1611
Correct. $4 is the column number.
 
1 members found this post helpful.
Old 02-14-2014, 02:12 PM   #5
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,697

Original Poster
Rep: Reputation: 48
Great, thanks.
 
Old 02-14-2014, 04:34 PM   #6
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
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
 
1 members found this post helpful.
Old 02-14-2014, 04:35 PM   #7
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,697

Original Poster
Rep: Reputation: 48
pretty cool stuff there, thanks
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bash suggestions to convert horizontal columns to vertical columns Guyverix Programming 14 01-24-2013 11:03 AM
SQL statements howto -- 3 columns input but 2 columns output fhleung Programming 3 11-29-2012 10:45 AM
Map 1 CSV's columns to matching columns in another CSV 2legit2quit Programming 7 10-27-2011 08:53 AM
[SOLVED] AWK: add columns while keep format for other columns cristalp Programming 3 10-13-2011 06:14 AM
How to check the total number of columns in a text file cliffyao Linux - Newbie 9 12-01-2009 04:49 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration