#!/bin/bash
# *************************************************************
# file: volume
# version: 1.01
# date: 2005-05-19
# author: Marko Schulz - <info@tuxnet24.de>
# description: dispaly the disk space of a lokal or remote host
# *************************************************************
# width of bars...
FORMAT="30"
# *************************************************************
# create bars...
function getbar() {
local i=0
local count=$1
local space=${2:-' '}
while [ $i -le $count ]; do
str="${str}${space}"
i=$(( $i + 1))
done
echo "$str"
}
# *************************************************************
# calculate relationship between used and free disk space...
function calculate() {
local used=$1
local free=$2
local width=$3
used=$( perl -e "printf \"%.0f\", ( $free*$width/100 );" )
free=$[$width - $used]
echo -n "$free:$used"
}
# *************************************************************
# show help...
function help() {
header
echo
echo -e "Usage: $( basename $0 ) [-r <user>@<host> -F integer -m {text|color} -h -s ]\n"
echo -e "\t-r display the disk space from remote host"
echo -e "\t-F width of the dispay"
echo -e "\t-m output modus {text|color}"
echo -e "\t-s to suppress header"
echo -e "\t-h show this Help\n\a"
exit 0
}
# *************************************************************
# show header...
function header() {
echo "*********************************************************"
echo -e "$( basename $0) v1.01 - display disk space of lokal or remote host"
echo -e "Copyright (c) 2005 by Marko Schulz - <mschulz@jamba.net>"
echo "*********************************************************"
}
# *************************************************************
# P R O G R A M A C T I O N
while getopts r:F:m:hs Optionen 2>/dev/null; do
case $Optionen in
r) REMOTE=$OPTARG ;;
F) FORMAT=$OPTARG ;;
m) MODUS=$OPTARG ;;
s) SUPPRESS="on" ;;
h) help ;;
esac
done
# Temporary file...
temp="/tmp/$( basename $0 | sed 's/\.[a-z]\{2,\}$//g' )-$$.log"
# delete temporary file after finish...
trap "[ -f $temp ] && rm $temp" EXIT
# get df-output from remote or lokal host...
if [ -n "$REMOTE" ]; then
ssh $REMOTE 'df -h' > $temp
else
df -h > $temp
fi
# suppress header
[ "$SUPPRESS" != "on" ] && header
echo
# loop data from temporary file...
for result in $( cat $temp | grep '^/dev/' | awk '{ print $3":"$4":"$5":"$6 }' ); do
used=$( echo $result | cut -d: -f3 | sed 's/%//g' )
sused=$( echo $result | cut -d: -f1 )
sfree=$( echo $result | cut -d: -f2 )
xused=$used
mount=$( echo $result | cut -d: -f4 )
free=$[100 - $used]
xfree=$free
used=$( calculate $used $free $FORMAT | cut -d: -f1 )
free=$( calculate $used $free $FORMAT | cut -d: -f2 )
if [ "$MODUS" = 'color' ]; then
used="$( getbar "$used" )"
free="$( getbar "$free" )"
# Output...
erg=$( printf "%5s %4s %29s %3s %4s %-35s\n" "$sused" "${xused}%" \
"\033[41m${used}\033[0m\033[42m${free}\033[0m" "${xfree}%" "$sfree" "$mount" )
echo -e "$erg"
else
used="$( getbar "$used" ':' )"
free="$( getbar "$free" '.' )"
# Output...
printf "%5s %4s %29s %3s %4s %-35s\n" "$sused" "${xused}%" \
"${used}${free}" "${xfree}%" "$sfree" "$mount"
fi
echo
done
# *************************************************************
# EOF