/home/phn/`date..."> /home/phn/`date...">

Bash - HTML report generator with sending mail

Posted by Pavlo Khmel on Fri 01 January 2010

Script below will generate HTML report.

Report will be sent as HTML email.

#!/bin/sh
echo "To: phn@khmel.org" > /home/phn/`date +"%Y-%m-%d"`.html
echo "Subject: Daily Backup Check Report `date +"%Y-%m-%d"`" >> /home/phn/`date +"%Y-%m-%d"`.html
echo "Content-Type: text/html" >> /home/phn/`date +"%Y-%m-%d"`.html
echo "<html>" >> /home/phn/`date +"%Y-%m-%d"`.html
echo "<head>" >> /home/phn/`date +"%Y-%m-%d"`.html
echo "<title>Daily backup check report</title>" >> /home/phn/`date +"%Y-%m-%d"`.html
echo "</head>" >> /home/phn/`date +"%Y-%m-%d"`.html
echo "<body>" >> /home/phn/`date +"%Y-%m-%d"`.html
echo "<h1>Daily backup check report. Date: `date +"%Y-%m-%d"`" >> /home/phn/`date +"%Y-%m-%d"`.html
echo "<table border>" >> /home/phn/`date +"%Y-%m-%d"`.html
echo '<tr>' >> /home/phn/`date +"%Y-%m-%d"`.html
echo "<td>**NUM**</td>" >> /home/phn/`date +"%Y-%m-%d"`.html
echo "<td>**HOSTNAME**</td>" >> /home/phn/`date +"%Y-%m-%d"`.html
echo "<td>**REPORT DATE**</td>" >> /home/phn/`date +"%Y-%m-%d"`.html
echo "<td>**UPTIME**</td>" >> /home/phn/`date +"%Y-%m-%d"`.html
echo "<td>**BACKUP LAST LOG**</td>" >> /home/phn/`date +"%Y-%m-%d"`.html
echo '</tr>' >> /home/phn/`date +"%Y-%m-%d"`.html
DATETODAY=`date +"%Y-%m-%d"`
COUNTER=1
while read ServerName
do
    filePath="/home/ftp_report/reports/`echo $ServerName`.txt"
    if [ -f $filePath ];
        then serverNameTemp=`echo $ServerName.txt`;
    else serverNameTemp="NO";
    fi
    if [ $serverNameTemp != "NO" ]; then
        reportDateZ=`head -n2 /home/ftp_report/reports/$serverNameTemp | tail -n1 | awk -F " " '{ print $1 }'`
        uptimeStatusZ=`head -n3 /home/ftp_report/reports/$serverNameTemp | tail -n1 | awk -F "," '{ print $1 }' | awk -F " " '{ print $3, $4 }'`
        backupStatusZ=`head -n4 /home/ftp_report/reports/$serverNameTemp | tail -n1 | awk -F "backupcheck:" '{ print $2 }'`
        # Verification for ERROR in log
        if echo $backupStatusZ | grep -q ERROR; then
            backupStatusZ="<font color=red> $backupStatusZ </font>";
        fi
        # Verification for OLD report
        if [ "$reportDateZ" != "$DATETODAY" ]; then
            ServerName="<font color=red>** $ServerName **</font>";
            reportDateZ="<font color=red>** $reportDateZ **</font>";
            uptimeStatusZ="<font color=red>** $uptimeStatusZ **</font>";
            backupStatusZ="<font color=red>** $backupStatusZ **</font>";
        fi
        echo '<tr>' >> ./`date +"%Y-%m-%d"`.html
        echo "<td> $COUNTER </td>" >> /home/phn/`date +"%Y-%m-%d"`.html
        echo "<td> $ServerName </td>" >> /home/phn/`date +"%Y-%m-%d"`.html
        echo "<td> $reportDateZ </td>" >> /home/phn/`date +"%Y-%m-%d"`.html
        echo "<td> $uptimeStatusZ </td>" >> /home/phn/`date +"%Y-%m-%d"`.html
        echo "<td> $backupStatusZ </td>" >> /home/phn/`date +"%Y-%m-%d"`.html
        echo '</tr>' >> /home/phn/`date +"%Y-%m-%d"`.html
    else
        echo '<tr>' >> /home/phn/`date +"%Y-%m-%d"`.html
        echo "<td> $COUNTER > /home/phn/`date +"%Y-%m-%d"`.html
        echo "<td> $ServerName > /home/phn/`date +"%Y-%m-%d"`.html
        echo "<td><font color=red> $serverNameTemp </font></td>" >> /home/phn/`date +"%Y-%m-%d"`.html
        echo "<td><font color=red> $serverNameTemp </font></td>" >> /home/phn/`date +"%Y-%m-%d"`.html
        echo "<td><font color=red> There is $serverNameTemp any report </font></td>" >> /home/phn/`date +"%Y-%m-%d"`.html
        echo '</tr>' >> /home/phn/`date +"%Y-%m-%d"`.html
    fi
    let COUNTER=COUNTER+1
done < /home/phn/linux_server_list.txt
echo "</table>" >> /home/phn/`date +"%Y-%m-%d"`.html
echo "</body>" >> /home/phn/`date +"%Y-%m-%d"`.html
echo "</html>" >> /home/phn/`date +"%Y-%m-%d"`.html
echo "<a href="`date +"%Y-%m-%d"`.html">Report date: `date +"%Y-%m-%d"`</a><br />" >> /home/phn/index.html
/usr/sbin/sendmail phn@khmel.org < /home/phn/`date +"%Y-%m-%d"`.html