Linux Training

Linux training for private, public & voluntary sector.

0793 572 8612

City LinUX sample scripts - chkdf


#$Id: chkdf,v 1.68 2018/01/12 14:13:35 fulford Exp fulford $
#$Source: /src/admin/usr/local/etc/RCS/chkdf,v $
#$Author: fulford $
#$Revision: 1.68 $
#
# Copyright (c) 2012-2015 Clifford W Fulford.
# Licensed for public use under the LGPL.
# For assistance  call 0709 229 5385
#		e-mail fulford@fulford.net
########################################################################
cmd=`basename $0`
syntax="$0 [-c <config file> [-e <email_rcpt>] [-l] [-m #(max % used)] [-u <login] [-v] [host]"
while [ $# -gt 0 ];do
	case $1 in
		-c) config=$2;shift 2;;
		-d) debug=:;adebug="-v debug=1";set -x;shift;;
		-e) rcpt=$2;shift 2;;
		-u) login=$2;shift 2;;
		-l) log=:;shift;;
		-m) max=$2;shift 2 ;;
		-v) verbose=:;shift ;;
		-[^cvmru]) echo $syntax >&2 ; exit;;
		 *) host=$1;shift;;
	esac
done
config=${config:="/usr/local/etc/chkdf.cf"}
default=60
host=${host:=localhost}
login=${login:=`id -nu`}
logger="/usr/bin/logger"
max=${max:-0}
msg=/tmp/${cmd}msg$$
tmp=/tmp/$cmd${host}$$
hdf=/tmp/$cmd${host}hdf$$
verbose=${verbose:-""}

[ $log ] && { $logger -t $cmd "$host run" ;}
if [ -f $config ];then
	if ! grep $host $config >/dev/null;then
		echo "$cmd: no configuration for $host, using defaults." >&2
	fi
	sed -ne '/^'$host':/,/^$/{
		/^[ \t].*:.*/{
			s/\t//
			s/:/ /
			p
		}
	}' $config >$tmp
	[ $max -gt 0  ] && echo "max $max" >>$tmp
	grep default $tmp >/dev/null||{
		echo "default $default">>$tmp
	}
else
	echo "$cmd: config $config not found" >&2
	exit 1
fi
rcpt=${rcpt:=`id -nu`}
[ $verbose ] && echo "`basename $0`: contacting $host" >&2
ssh -l $login $host "df -lPk" |sed -e '/\/dev\/loop/d;/\/dev\/sr/d'>$hdf 
awk $adebug -v host=$host -v cmd=$cmd '{
	if(NR == FNR) { a[$1]=$2 
	}
	if(NR > FNR){
		if($1 !~ /Filesystem/){
			sub("%","",$5);
			if("max" in a){
				max=a["max"]
			}else{
				if($6 in a) max=a[$6]
				else max=a["default"]
			}
			# $5 not converting to numeric properly force
			# by adding zero.
			if ($5 + 0  > max){
				printf "Warning! %6s %3d%% on %s\n",$6,$5,host
			}
		}
	}
}' $tmp $hdf |
while read line;do
	echo "$line" >>$msg
	[ $log ] && logger -t $cmd "$line"
	[ $verbose ] && echo "$cmd: $line"
done
[ $verbose ] &&{
	if [ ! -f $msg ];then
		echo "$cmd: $host ok"
	fi
}
	
if  [ -f $msg ];then 
	 mailx -s "$cmd $host warning! `date`" ${cc:+"-c $cc"} $rcpt <$msg
	rm $msg
fi
[ "$debug" ] && { cat $tmp $hdf ;}
rm $tmp $hdf 
######################################################################
# This program is free software: you can redistribute it and or      #
# modify it under the terms of the Lesser GNU General Public License #
# as published by the Free Software Foundation, either version 3 of  #
# the License, or (at your option) any later version.                #
#                                                                    #
# This program is distributed in the hope that it will be useful,    #
# but WITHOUT ANY WARRANTY; without even the implied warranty of     #    
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the      #
# GNU General Public License for more details.                       #
#                                                                    # 
# A copy of the Lesser GNU General Public License and the GNU        #
# General Public License is available at                             #
# <http://www.gnu.org/licenses/>.                                    #
######################################################################

The layout and associated style sheets for this page are taken from the World Wide Web Consortium and used here under the W3C software licence.