Code:
#!/bin/ksh
# fs_trend.sh Version 1 - Mike Hayes - 2008-Sep-09
# fs_trend.sh Version 1.1 - Mike Hayes - 2008-Sep-10 - Added error checking
# fs_trend.sh Version 2 - Mike Hayes - 2008-Sep-12 -
# Added colour coding
# Added configuration file
#
# This script produces a trend figure for disk usage report web page.
#
# It is freely redistributable for anyone who may find it useful
#
# For each file system, shows data from today, 1, 2 and 3 days ago, 1 and
# 2 weeks ago and 1, 3 and 6 months ago
# Where any historical data is not available, "-" will appear.
# New file systems are automatically catered for.
# The script will create a file fs_trend_<system name>.html which you can load
# to your web site.
# It also creates a file fs_history which will build over time to contain
# historical data for the html page.
# You should run the script daily from your crontab (root is not required)
# The script uses a configuration file which is created with defaults if it does not exist
# It also writes a copy of the web page to /tmp (in case you use something
# like an ssh keychain as another user to grab the files for your web server, to avaoid any permissions problems)
# **************************************************** #
# ****************** Initialisation ****************** #
# **************************************************** #
# set home location to same directory as fs_trend.sh
FST_HOME=${0%/*}
#
# If the configuration file does not exist, then create it
# Output_dir (The output directory) is set to the same directory where the script resides
# Colour=YES - The option to have coulour coded cells
# Redpct=90 - Turns the cell red if the FS % is 90% or more
# Amberpct=80 - Turns the cell Amber if the FS% is between 80% and 90%
if [ ! -f $FST_HOME/fs_trend.conf ]
then
if [ "$FST_HOME" = "." ]
then
echo "Output_dir=`pwd`" > $FST_HOME/fs_trend.conf
else
echo "Output_dir=$FST_HOME" > $FST_HOME/fs_trend.conf
fi
echo "Colour=YES" >> $FST_HOME/fs_trend.conf
echo "Redpct=90" >> $FST_HOME/fs_trend.conf
echo "Amberpct=80" >> $FST_HOME/fs_trend.conf
fi
#
# Source the configuration parameters
. $FST_HOME/fs_trend.conf
if [ -z $Output_dir ]
then
Output_dir=$FST_HOME
fi
if [ "$Colour" = "YES" ]
then
Red="FF0000"
Amber="EFE813"
else
Red="CCFFFF"
Amber="CCFFFF"
fi
# Set the Server name, today's date and add the heading line for the web page table
Server=`uname -n`
DAT=`date '+%Y-%b-%d'`
echo "File System,Today,CCFFFF,Yesterday,CCFFFF,2 Days ago,CCFFFF,3 Days ago,CCFFFF,1 Week ago,CCFFFF,2 Weeks ago,CCFFFF,1 Month ago,CCFFFF,3 Months ago,CCFFFF,6 Months ago,CCFFFF,Size" > $FST_HOME/fs_history_tmp2
# **************************************************** #
# **************** Get todays figures **************** #
# **************************************************** #
# If you want to exclude any additional file systems, then add them to the egrep below
df -k | egrep -v '^swap|^/devices|^proc|^/proc|^mnttab|^objfs|^fd|^/platform|^ctfs|^Filesystem' | awk '{print $6" "$5}' > $FST_HOME/fs_history_tmp1
# **************************************************** #
# ******** Add todays data to our history file ******* #
# **************************************************** #
# loop round the file systems we've found today
exec 4< $FST_HOME/fs_history_tmp1
while read -u4 fs fspct
do
# We check to see that the data has not been added today already
# but rather than miss out completely, check each fs incase it has just been added
grep "$DAT $fs " $FST_HOME/fs_history > /dev/null 2>&1
if [ $? != 0 ]
then
echo "$DAT $fs $fspct" >> $FST_HOME/fs_history
fi
size=`df -k $fs | tail -1 | awk '{print $2}'`
# **************************************************** #
# ************ Create data for the report ************ #
# **************************************************** #
# Now we get the data for the report for the 9 different dates
# loop round all the historical data
for xx in 0 1 2 3 7 14 30 90 180
do
# using perl localtime because you can't set TZ back more than 167 hours
# in Solaris 10
#/usr/bin/perl <<EOF > $FST_HOME/datem2
#print scalar localtime(time - (86400 * $xx) ), "\n";
#EOF
#typeset -Z2 dd=`cat $FST_HOME/datem2 | awk '{print $3}'`
#ym=`cat $FST_HOME/datem2 | awk '{print $5"-"$2"-"}'`
#DATE=$ym$dd
DATE=`perl -e '$xx = shift @ARGV;
($a,$mon,$d,$b,$y) = split " ",(scalar localtime(time - (86400 * $xx)));
printf "$y-$mon-$d"; ' $xx`
grep "$DATE $fs " $FST_HOME/fs_history > /dev/null
if [ $? = 0 ]
then
typeset pct_$xx=`grep "$DATE $fs " $FST_HOME/fs_history | awk '{print $3}'`
typeset pccol=`grep "$DATE $fs " $FST_HOME/fs_history | awk '{print $3}' | sed s/%//`
typeset bgcol_$xx=$Amber
if [ "$pccol" -ge "$Redpct" ]
then
typeset bgcol_$xx=$Red
elif [ "$pccol" -lt "$Amberpct" ]
then
typeset bgcol_$xx="CCFFFF"
fi
else
typeset pct_$xx="-"
typeset bgcol_$xx="E4DCDC"
fi
done
echo "$fs,$pct_0,$bgcol_0,$pct_1,$bgcol_1,$pct_2,$bgcol_2,$pct_3,$bgcol_3,$pct_7,$bgcol_7,$pct_14,$bgcol_14,$pct_30,$bgcol_30,$pct_90,$bgcol_90,$pct_180,$bgcol_180,$size" >> $FST_HOME/fs_history_tmp2
done
# fs_history has been updated and fs_history_tmp2 created with the data we
# want to format in our web page
# **************************************************** #
# ***************** Web page creation **************** #
# **************************************************** #
cat $FST_HOME/fs_history_tmp2 | awk '
BEGIN { FS="," }
NF == 20 {
fs=$1;
size=$20;
pct_0=$2;
pct_1=$4;
pct_2=$6;
pct_3=$8;
pct_7=$10;
pct_14=$12;
pct_30=$14;
pct_90=$16;
pct_180=$18;
bgcol_0=$3;
bgcol_1=$5;
bgcol_2=$7;
bgcol_3=$9;
bgcol_7=$11;
bgcol_14=$13;
bgcol_30=$15;
bgcol_90=$17;
bgcol_180=$19;
# Make the size figure more readable i.e. in Gb, Mb or Kb
gb = size / 1048576
if ( size == "Size" ) {
sizexp=Size;
sizex=" ";
} else {
sizep = gb;
sizex="G";
}
if ( size == "Size" ) {
printf("<tr><td><B>%s</B></td><td><B>Size</B></td><td align='center' bgcolor=%s><B>%s</B></td><td align='center' bgcolor=%s><B>%s</B></td>"\
"<td align='center' bgcolor=%s><B>%s</B></td><td align='center' bgcolor=%s><B>%s</B></td>"\
"<td align='center' bgcolor=%s><B>%s</B></td><td align='center' bgcolor=%s><B>%s</B></td><td align='center' bgcolor=%s><B>%s</B></td>"\
"<td align='center' bgcolor=%s><B>%s</B></td><td align='center' bgcolor=%s><B>%s</B></td></tr>\n",\
fs,bgcol_0,pct_0,bgcol_1,pct_1,bgcol_2,pct_2,bgcol_3,pct_3,bgcol_7,pct_7,bgcol_14,pct_14,bgcol_30,pct_30,bgcol_90,pct_90,bgcol_180,pct_180);
} else {
printf("<tr><td><B>%s</B></td><td align='right'><B>%1.1f%s</B></td><td align='center' bgcolor=%s><B>%s</B></td><td align='center' bgcolor=%s><B>%s</B></td>"\
"<td align='center' bgcolor=%s><B>%s</B></td><td align='center' bgcolor=%s><B>%s</B></td>"\
"<td align='center' bgcolor=%s><B>%s</B></td><td align='center' bgcolor=%s><B>%s</B></td><td align='center' bgcolor=%s><B>%s</B></td>"\
"<td align='center' bgcolor=%s><B>%s</B></td><td align='center' bgcolor=%s><B>%s</B></td></tr>\n",\
fs,sizep,sizex,bgcol_0,pct_0,bgcol_1,pct_1,bgcol_2,pct_2,bgcol_3,pct_3,bgcol_7,pct_7,bgcol_14,pct_14,bgcol_30,pct_30,bgcol_90,pct_90,bgcol_180,pct_180);
}
}' > $FST_HOME/fs_history_tmp3
OUTPUT_FILE=$Output_dir/fs_trend_$Server.html
print "<html><head><title>$Server File System Trend Report on $DATE</title>" > ${OUTPUT_FILE}
print "</head>" >> ${OUTPUT_FILE}
print "<body BGCOLOR=CCFFFF>" >> ${OUTPUT_FILE}
print "<B><CENTER><font size="5">$Server File System Trend Report on $DATE</CENTER></B><br>" >> ${OUTPUT_FILE}
print "<table border=2 width=\"100%\" >\n" >> ${OUTPUT_FILE}
cat $FST_HOME/fs_history_tmp3 >> ${OUTPUT_FILE}
print "</table>" >> ${OUTPUT_FILE}
print '<br><B><font size="2">Report shows the percentage full for each file system at various dates ( "-"=No data available)' >> ${OUTPUT_FILE}
print "</body></html>" >> ${OUTPUT_FILE}
# **************************************************** #
# ********************* Clean up ********************* #
# **************************************************** #
rm -f $FST_HOME/datem2 $FST_HOME/fs_history_tmp1 $FST_HOME/fs_history_tmp2 $FST_HOME/fs_history_tmp3
cp ${OUTPUT_FILE} /tmp/${OUTPUT_FILE##*/}
exit 0
##############################################################################
### This script is submitted to BigAdmin by a user of the BigAdmin community.
### Sun Microsystems, Inc. is not responsible for the
### contents or the code enclosed.
###
###
### Copyright Sun Microsystems, Inc. ALL RIGHTS RESERVED
### Use of this software is authorized pursuant to the
### terms of the license found at
### http://www.sun.com/bigadmin/common/berkeley_license.jsp
##############################################################################