my-scripts/dfc.sh

103 lines
3.5 KiB
Bash
Raw Normal View History

#!/bin/bash
# df (= DiskFree) with some options and colored if Alert levels are exceeded
#
# (c) GPL by Ulf Bartolomäus
VERSION="$( basename $0 ) Version 0.1.2 from 03.11.2021"
#
# Input: -v : Version
# -y YY : YY = alert level (yellow) in % (default = 80%)
# -r RR : RR = critical alert level (red) in % (default = 90%)
# Output: disk free information colored about mounted filesystems
# Returnvalue: 0
#
# Changes:
# 0.1.2
# Some format improfements and adding from options
# 0.1.1
# Initial based on whatch_new
## Definitions
#MyDfC='df -hT' ## df command: -h: human readeable, -T: show filesystem type
MyDfC='df -hTx tmpfs -x devtmpfs' ## df -hT command: excluding *tenpfs
#MyDfC='df -hTx tmpfs -x devtmpfs -x fuse -x nfs4' ## df -hT command: excluding *tenpfs, fusa and nfs4 filesystems
MyDfAlert1=80 ## "yellow" for alert level for out of space
MyDfAlert2=90 ## "red" for critical alert for out of space
## Escape secences for color
esc=$(echo -en "\033") ## define esc secence
dick="${esc}[1m" ## define bold secence
rot="${esc}[1;31m" ## define red secence
gruen="${esc}[1;32m" ## define green secence
gelb="${esc}[1;33m" ## define yelow secence
blau="${esc}[1;34m" ## define blue secence
lila="${esc}[1;35m" ## define purple secence
norm=$(echo -en "${esc}[m\017") ## define default secence
## Shows the Version iformation
do_version () {
echo "* ${VERSION} *"
}
## Show help
do_usage () {
echo "${dick}"
echo "Usage: $( basename $0 ) [-Vvh] [-y AlertLevel] [-r CriticalAlertLevel] [dfOptions]"
echo "This program provides the df (man df) information color by filling grade!"
echo "${dick}${blau}"
echo "This program is GPL (https://git.sp-codes.de/ub1x/my-scripts/src/branch/main/LICENSE)."
echo "${norm}${dick}"
echo "-V Version info - only"
echo "-v Version info - aditional"
echo "-h shows this help"
echo "-y N percent value of alert level (marked in yellow) (default 80%)"
echo "-r N percent value of critical alert level (marked in red) (default 90%)"
# echo "dfOptions additional comand options"
echo "${norm}"
echo
}
## Read options
while getopts Vvhy:r: o; do
case $o in
V) do_version
exit 0
;;
v) do_version
;;
h) do_usage
exit 0
;;
y) if [ -n "$(( ${OPTARG} ))" ] ; then
MyDfAlert1=$(( ${OPTARG} ));
# echo "gelb von 80% auf ${MyDfAlert1}% geändert"
fi
;;
r) if [ -n "$(( ${OPTARG} ))" ] ; then
MyDfAlert2=$(( ${OPTARG} ));
# echo "rot von 90% auf ${MyDfAlert2}% geändert"
fi
;;
*) do_usage
exit 10
;;
esac
done
#shift $(expr $OPTIND - 1)
#MyDfC="${MyDfC} $*"
## DiskFree (only if not the same device => unique check first 40 characters)
${MyDfC} | uniq -w40 | while read MyOutput; do ## Execute df and filter doublicates - for each row stored in MyOutput
MyDfUsage=$(echo ${MyOutput} | awk '{ print $6}' | cut -d'%' -f1 ) ## Search Usage in %
if [[ ! $( echo "${MyDfUsage}" | grep [[:digit:]] ) ]] ; then ## If first row
echo -e ${dick}"${MyOutput}"${norm} ## Echo in bold
elif [ $(( ${MyDfUsage} )) -ge ${MyDfAlert2} ]; then ## If critical level excided
echo -e ${rot}"${MyOutput}"${norm} ## Echo in red
elif [ $(( ${MyDfUsage} )) -ge ${MyDfAlert1} ]; then ## If alert level excided
echo -e ${gelb}"${MyOutput}"${norm} ## Echo in yelow
else ## default
echo -e ${gruen}"${MyOutput}"${norm} ## Echo in green
fi
done