#/bin/bash

#Please note that this script is tuned for my syslog-ng configuration.
#Maybe you must align the cut values. 

#Kernelversions you have used
Kernelversions="2.6.21-1-k7 2.6.18-5-486 2.6.18-4-486 2.6.18-4-k7 2.6.16.52 2.6.8-3-k7"

#Path tho your Logfiles with kernel Messages.
Logfilepath="/var/log/CENTRAL/<hostname>/<hostname>-kern-*"

#Path to a temporary file
TempFile="/tmp/kernel-ext3-stat.$$"

#Collect relevant log entries
zegrep -h "Linux version|kernel.*EXT3.*hda1" $Logfilepath > $TempFile


for RoughVersion in $Kernelversions
do
    #Count how much different Debian package versions we found in the logfile.
    DetailVersions=`cat $TempFile | grep $RoughVersion | cut -d "(" -f2 | cut -d" " -f2 | cut -d")" -f1 | sort -n | uniq`
    
    CompleteVersions=""
    if [ $DetailVersions ]; then
       for dv in $DetailVersions
       do
         CompleteVersions=$CompleteVersions" "$RoughVersion".*"$dv
       done
    else
       CompleteVersions=$RoughVersion
    fi

    for Version in $CompleteVersions
    do
   
       #Count Reboots in Total.
       ari=`cat $TempFile | grep -c $Version`
       
       #Count Reboots with EXT errors.
       ark=`cat $TempFile | grep -A2 $Version | cut -d" " -f-3,5- | sort -k1M -k2n | cut -c-7,16-100 | uniq  | grep -c error`
       
       #Count days this Kernel was booted.
       atg=`cat $TempFile | grep $Version | sort -k1M -k2n | cut -c-6 | uniq | grep -c .`
       
       #Oldest and youngest day this kernel was booted.
       od=`cat $TempFile | grep $Version | sort -k1M -k2n | head -1 | cut -c-7`
       yd=`cat $TempFile | grep $Version | sort -k1M -k2n | tail -1 | cut -c-7`
       
       pdays=$[$[$atg-$ark]*100/$atg]
       preboots=$[$[$ari-$ark]*100/$ari]
       
       echo
       echo "----------------------------------------------"
       echo " Kernel : $Version"
       echo "----------------------------------------------"
       echo "Days with this kernel booted      (counted): "$atg" ($od until $yd)"
       echo "Reboots with this kernel in total (counted): "$ari
       echo "Reboots with EXT3 corruption      (counted): "$ark
       #echo
       echo "% Errorfree days (roughly) / reboots       : "$pdays"% / "$preboots"%"
       #echo
       
    done
done

rm $TempFile
