Skip to content

Instantly share code, notes, and snippets.

@wytten
Created April 26, 2019 14:04
Show Gist options
  • Save wytten/6265cd826f0114d880b33571505ebd94 to your computer and use it in GitHub Desktop.
Save wytten/6265cd826f0114d880b33571505ebd94 to your computer and use it in GitHub Desktop.
Build a spreadsheet of df output for a collection of servers
#!/bin/bash
# Read default value of $hosts from the file common, if it exists.
# If it exists it is also assumed to define the function die.
if [ -f common ]; then
. common
else
function die {
echo $1
exit 1
}
fi
if [ $# -gt 0 ]; then
hosts="$@"
fi;
out=df_all.csv
rm -f $out || die "Unable to remove $out"
# If not Linux most likely HP-UX, in which case use bdfmsg.sh if present, to emulate Linux df -Ph
cmd='if [ "$(uname)" = "Linux" ]; then df -Ph; elif [ -x bin/bdfmsg.sh ]; then bin/bdfmsg.sh; else df -kP; fi'
format="%-10s,%-46s,%-20s,%-20s,%-20s,%-5s,%s\n"
# Header line, print it exactly once regardless of number of hosts in the spreadsheet
awk -v f=$format 'BEGIN {printf f,"Server","File-System","Size","Used","%Used","Avail","Mounted"}' < /dev/null | tee -a $out
for host in $hosts; do
# Long story, let's just say these are a problem
if [ "$host" == "otus" ] || [ "$host" == "satyrs" ]; then
continue;
fi
# Only print line if not the header line (NR>1) and not blank (NF>0)
# Prepend each line with the name of the host
ssh $host "$cmd" | awk -v h=$host -v f=$format '{if (NR > 1 && NF > 0) printf f,h,$1,$2,$3,$4,$5,$6}' | tee -a $out
done
# Launch the spreadsheet if running on MS Windows
if [[ $OS == Windows* ]]; then
start $out
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment