#!/bin/bash

logfiles="/var/log/cron.log /var/log/freshclam.log /var/log/kern.log /var/log/sys.log /var/log/auth.log /var/log/daemon.log /var/log/kdc.log /var/log/krb.log /var/log/mail.log /var/log/user.log /var/log/xferlog" 


function report_header() {
	echo -e "       System Report for `hostname` on `date`"
	uptime
}


function last_log() {
	echo -e "\n============ Last Log ============="
	last -a|grep "`date +"%b %e"`"
}


function bandwidth() {
	echo -e "\n============ Bandwidth Usage =============="
	lastrxbytes=`grep "RX bytes" /home/jesse/tmp/reportscript|cut -d' ' -f12,17 --output-delimiter=":"|cut -d':' -f2`
	lasttxbytes=`grep "RX bytes" /home/jesse/tmp/reportscript|cut -d' ' -f12,17 --output-delimiter=":"|cut -d':' -f4`

	rxbytes=`/sbin/ifconfig eth0|grep "RX bytes"|cut -d' ' -f12,17 --output-delimiter=":"|cut -d':' -f2` 
	txbytes=`/sbin/ifconfig eth0|grep "RX bytes"|cut -d' ' -f12,17 --output-delimiter=":"|cut -d':' -f4`

	echo "Today's Bytes Received: $((rxbytes - lastrxbytes)) ($(((rxbytes - lastrxbytes)/1024/1024)) Mb)"
	echo "Today's Bytes Sent:     $((txbytes - lasttxbytes)) ($(((txbytes - lasttxbytes)/1024/1024)) Mb)"
	echo "Today's Bytes Total:    $(( (rxbytes + txbytes) - (lastrxbytes + lasttxbytes) )) ($((( (rxbytes + txbytes) - (lastrxbytes + lasttxbytes)) /1024/1024)) Mb)" 

	echo 

	up=`/usr/bin/uptime|cut -d' ' -f4`
	echo "Average Daily Bytes Received: $((rxbytes / up)) ($(((rxbytes / up) / 1024 / 1024)) Mb)"
	echo "Average Daily Bytes Sent:     $((txbytes / up)) ($(((txbytes / up) / 1024 / 1024 )) Mb)"
	echo "Average Daily Bytes Total:    $(((rxbytes + txbytes) / up)) ($((((rxbytes + txbytes) / up) / 1024 / 1024)) Mb)"


	if [ "23" == `date +%H` ]; then
		/sbin/ifconfig eth0|grep "RX bytes" > /home/jesse/tmp/reportscript
	fi
}


function disk_usage() {
	echo -e "\n============= Disk Usage =============="
	df -h
	echo
	sudo /usr/bin/du -sh /home/*|sort -nr|gawk '{print $2,"\t\t", $1}'
}


function apache_stats() {
	echo -e "\n============= Apache Stats ================"
	rcount=0
	botcount=0
	apachedate=`date +%d/%b/%G`
	for i in /var/log/apache/*-access_log; do
		requests=`grep $apachedate $i|wc -l` 
		bots=`grep $apachedate $i|grep 'GET /robots.txt'|wc -l`
	
		rcount=$((rcount + requests))
		botcount=$((botcount + bots))

		lnamelen=`basename $i|wc -c`
		lname=`basename $i|cut -b-$((lnamelen - 12))`

		echo "$lname: $requests Requests"
		echo "$lname: $bots ROBOTS Requests"
		echo
	done

	echo "Total Hits:   $rcount"
	echo "Total ROBOTS: $botcount"
}


function logfiles() {
	for i in $logfiles; do
		echo -e "\n========= `basename $i` ========"
		tail -4 $i
	done
}

if [ "$*" == "" ]; then
	report_header
	last_log
        bandwidth
        disk_usage
        apache_stats
        logfiles
else
	for i in $*; do
		case "$i" in
			header)
				report_header;;
			lastlog)
				last_log;;
			bandwidth)
				bandwidth;;
			disk)
				disk_usage;;
			apache)
				apache_stats;;
			logs)
				logfiles;;
			*)
				echo "Usage: $0 header lastlog bandwidth disk apache logs help";;
		esac
	done
fi
