#! /bin/bash

# write the output of df to logfiles, for each device/partition in one file
# in function of time (you can call the script from time to time or use cron)
# generates plots and a html-file
# requires "gnuplot"
# 20080610 (c) by LG  http://nofalab.ch GPL
# ToDo: output a summary of the logfiles as text in place of the images
#       for blind people (with <img alt="...">), line 91

#################################################
# START CONFIGURATION
#################################################
LOG_DIR=$HOME/log/
IMAGES_DIR="$LOG_DIR"images/
HTML_DIR="$LOG_DIR"html/
HTML_FILE="$HTML_DIR"dflog.html
BROWSER=${BROWSER:-"/usr/bin/epiphany-gecko"}
#################################################
# END CONFIGURATION
#################################################

# commands usded by dflog.sh
DF_CMD=$(whereis -b df | cut -d " " -f2)
GNUPLOT_CMD=$(whereis -b gnuplot | cut -d " " -f2)
MKDIR_CMD=$(whereis -b mkdir | cut -d " " -f2)
AWK_CMD=$(whereis -b awk | cut -d " " -f2)
TR_CMD=$(whereis -b tr | cut -d " " -f2)
DATE_CMD=$(whereis -b date | cut -d " " -f2)
CUT_CMD=$(whereis -b cut | cut -d " " -f2)
ECHO_CMD=$(whereis -b echo | cut -d " " -f2)

# is "gnuplot" installed?
$GNUPLOT_CMD --version > /dev/null
if [ $? -gt 0 ]; then
    $ECHO_CMD "$(basename $0): can not create the plots"
    $ECHO_CMD "please install \"gnuplot\""
    exit
fi

# help message
$ECHO_CMD "This is $(basename $0).
You will find the disk-statistics as a html-file under
"$HTML_FILE"."

# check for directories
$MKDIR_CMD -p $LOG_DIR
$MKDIR_CMD -p $IMAGES_DIR
$MKDIR_CMD -p $HTML_DIR

# html, header
$ECHO_CMD '<html><body><center><h1>discfree !</h1>' > "$HTML_FILE"

# main, use of df
n=1
for i in $($DF_CMD -TP -x tmpfs | $TR_CMD -s ' ' '@'); do

    # skip the first line from the output of df
    [ $n = 1 ] && n=$(( $n + 1 )) && continue

    # for each device a logfile-name
    LOG_FILE="$($ECHO_CMD "$i" | \
    $AWK_CMD 'BEGIN { FS = "@" }{ print $7 }' | \
    $TR_CMD '/' '_').log" # a slash is not so good in filenames

    # write data to $LOG_FILE
    $ECHO_CMD "$($DATE_CMD '+%Y%m%d-%H%M') $($ECHO_CMD $i | $TR_CMD '@' ' ')" \
    >> "$LOG_DIR"/"$LOG_FILE"
    
    # gnuplot-script
    $GNUPLOT_CMD<<EOF
    set terminal png size 900,500
    set xlabel 'date'
    set ylabel 'blocks of 1024 bytes'
        #set key inside left center box
        set key tmargin right box
    set xdata time
        #set logscale y
    set timefmt "%Y%m%d-%H%M"
    set format x "%d %b\n%Y"
    set yrange [1: ]
    #set xrange ["20080800-0000":"20090701-0000"]
    set xrange []
    set grid xtics mxtics ytics mytics
    set output '$IMAGES_DIR$LOG_FILE.png'
    plot '$LOG_DIR$LOG_FILE' using 1:4 title "Disk space (partition)" with lines, \
         '$LOG_DIR$LOG_FILE' using 1:5 title "Occupé" with lines, \
         '$LOG_DIR$LOG_FILE' using 1:6 title "Disponible" with lines
EOF

    # html, content
    $ECHO_CMD "<hr><h2>$($ECHO_CMD "$i" | cut -d "@" -f1) <i>mounted on</i> \"$($ECHO_CMD $i | \
    $CUT_CMD -d "@" -f7)\"</h2>" >> "$HTML_FILE"
    $ECHO_CMD "<h3>Filesystem: $($ECHO_CMD "$i" | cut -d "@" -f2)</h3>" >> "$HTML_FILE"
    $ECHO_CMD "<img src=\""$IMAGES_DIR"/"$LOG_FILE".png\" alt=\"\">" >> "$HTML_FILE"
    
    # if you want to display the plots, uncomment the next line:
    display $IMAGES_DIR$LOG_FILE.png
    # hint: use the key "q" in the 'display'-programm to switch to the next plot.
done

# html, footer
{ $ECHO_CMD '<hr>created by dflog - <a href="http://nofalab.ch">nofalab</a>,'
$ECHO_CMD 'using <a href="http://www.gnu.org/software/coreutils/manual/html_node/df-invocation.html#df-invocation">'
$ECHO_CMD 'df (coreutils)</a> and <a href="http://www.gnuplot.info">gnuplot</a>'
$ECHO_CMD '</center></body></html>'; } >> "$HTML_FILE"

# if you want to display the statistics in a web-browser, uncomment the next line
#$BROWSER file://"$HTML_FILE" &

exit