#!/bin/sh
#
#  Yum cron file
#	Designed to only send mail when something has been updated or installed
#
#  Written by Troy Dawson <<a href="/scripts/wa.exe?LOGON=A3%3Dind1311%26L%3DSCIENTIFIC-LINUX-USERS%26E%3D7bit%26P%3D507443%26B%3D--%253D_2dg8plg516nl%26T%3Dtext%252Fx-shellscript%3B%2520charset%3DISO-8859-1%3B%2520name%3D%2522yum.cron%2522%26N%3Dyum.cron%26attachment%3Dq" target="_parent" >[log in to unmask]</a>>
#	Changed - May 7, 2003  by Troy Dawson <<a href="/scripts/wa.exe?LOGON=A3%3Dind1311%26L%3DSCIENTIFIC-LINUX-USERS%26E%3D7bit%26P%3D507443%26B%3D--%253D_2dg8plg516nl%26T%3Dtext%252Fx-shellscript%3B%2520charset%3DISO-8859-1%3B%2520name%3D%2522yum.cron%2522%26N%3Dyum.cron%26attachment%3Dq" target="_parent" >[log in to unmask]</a>>
#	Rewrite - May 14, 2004  by Troy Dawson <<a href="/scripts/wa.exe?LOGON=A3%3Dind1311%26L%3DSCIENTIFIC-LINUX-USERS%26E%3D7bit%26P%3D507443%26B%3D--%253D_2dg8plg516nl%26T%3Dtext%252Fx-shellscript%3B%2520charset%3DISO-8859-1%3B%2520name%3D%2522yum.cron%2522%26N%3Dyum.cron%26attachment%3Dq" target="_parent" >[log in to unmask]</a>>
#	Rewrite - March 1, 2005  by Troy Dawson <<a href="/scripts/wa.exe?LOGON=A3%3Dind1311%26L%3DSCIENTIFIC-LINUX-USERS%26E%3D7bit%26P%3D507443%26B%3D--%253D_2dg8plg516nl%26T%3Dtext%252Fx-shellscript%3B%2520charset%3DISO-8859-1%3B%2520name%3D%2522yum.cron%2522%26N%3Dyum.cron%26attachment%3Dq" target="_parent" >[log in to unmask]</a>>
#
#Change DEBUG to 0 to turn on debugging
DEBUG=1
MAXWAITTIME=180
MAILLIST="root"
#MAILLIST="root,<a href="/scripts/wa.exe?LOGON=A3%3Dind1311%26L%3DSCIENTIFIC-LINUX-USERS%26E%3D7bit%26P%3D507443%26B%3D--%253D_2dg8plg516nl%26T%3Dtext%252Fx-shellscript%3B%2520charset%3DISO-8859-1%3B%2520name%3D%2522yum.cron%2522%26N%3Dyum.cron%26attachment%3Dq" target="_parent" >[log in to unmask]</a>,<a href="/scripts/wa.exe?LOGON=A3%3Dind1311%26L%3DSCIENTIFIC-LINUX-USERS%26E%3D7bit%26P%3D507443%26B%3D--%253D_2dg8plg516nl%26T%3Dtext%252Fx-shellscript%3B%2520charset%3DISO-8859-1%3B%2520name%3D%2522yum.cron%2522%26N%3Dyum.cron%26attachment%3Dq" target="_parent" >[log in to unmask]</a>"
CONFIGFILE="/etc/yum.conf"
CONFIGDIR="/etc/yum.d"
EXCLUDESFILE="${CONFIGDIR}/yum.cron.excludes"
EXECCONFIG="$CONFIGDIR/yum.cron.updateexec"

#*******************************************
#sleep for a random amount of minutes
#*******************************************	
random_sleep() {
	utime=`cat /proc/uptime | cut -d'.' -f1`
	if [ $utime -ge 72000 ] ; then
		RAN=$RANDOM
		if [ "$RAN" = "0" ] ; then
			RAN=1
		fi
		if [ $MAXWAITTIME -le 0 ] ; then
			MAXWAITTIME=1
		fi
		SLEEPTIME=$(($RAN % $MAXWAITTIME))
		if [ $DEBUG -eq 0 ] ; then
			echo "  Sleeping for $SLEEPTIME minutes"
		fi
		sleep ${SLEEPTIME}m
	fi
}

#*******************************************
# check to see if yum is running, and if it is, for how long
# If it has been running close to a day, it's time
# to kill it and try again.
#*******************************************	
check_yum() {
	if [ -f /var/run/yum.pid ] ; then
		YUMPID=`cat /var/run/yum.pid`
		if [ -d /proc/${YUMPID} ] ; then
			OLDTIME=`cat /proc/${YUMPID}/stat | cut -d' ' -f22`
			NOWTIME=`cat /proc/self/stat | cut -d' ' -f22`
			let DIFFTIME=${NOWTIME}-${OLDTIME}
			if [ ${DIFFTIME} -ge 5000000 ] ; then
				if [ $DEBUG -eq 0 ] ; then
					echo "  Yum PID ${YUMPID}, Running for ${DIFFTIME} jiffies"
					echo "    Started ${OLDTIME}, Now ${NOWTIME}"
					echo "    This is more than the limit of 5000000 jiffies"
					echo "    Killing process ${YUMPID}"
				fi	
				#one last sanity check
				grep -q yum /proc/${YUMPID}/stat
				if [ "$?" == "0" ] ; then
					kill -9 ${YUMPID}
				fi
			else
				if [ $DEBUG -eq 0 ] ; then
					echo "  Yum PID ${YUMPID}, Running for ${DIFFTIME} jiffies"
					echo "    This is less than the limit of 5000000 jiffies"
				fi	
			fi
		else
			if [ $DEBUG -eq 0 ] ; then
				echo "  Yum PID ${YUMPID}, but no process running"
			fi	
		fi
	else
		if [ $DEBUG -eq 0 ] ; then
			echo "  No other yum process running."
		fi	
	fi
}

#******************************
#Start the program
#But only if we're supposed to
#******************************
if [ -f /var/lock/subsys/yum ]; then
	if [ $DEBUG -eq 0 ] ; then
		echo "Yum crontab starting"
	fi
	#****************************
	#VARIABLES
	#****************************
	CHANGE=0
	TEMPFILE="`mktemp /tmp/yum.temp.XXXXXXXXXX`"
	TEMPCONFIGFILE="`mktemp /tmp/yum.temp.config.XXXXXXXXXX`"
	TEMPMAILFILE="`mktemp /tmp/yum.temp.mail.XXXXXXXXXX`"
	TEMPRANDFILE="`mktemp /tmp/yum.temp.rand.XXXXXXXXXX`"
        TEMPPRERUNFILE="`mktemp /tmp/yum.temp.prerun.XXXXXXXXXX`"
        TEMPPOSTRUNFILE="`mktemp /tmp/yum.temp.postrun.XXXXXXXXXX`"
	TODAY="$(date +%Y-%m-%d)"

	if [ -f $EXECCONFIG ]; then
		. $EXECCONFIG
	fi

        #****************************
        #Setup Enviroment
        #****************************
        # make sure these are blank and owned by us
        touch $TEMPMAILFILE $TEMPCONFIGFILE $TEMPFILE
        chown root:root $TEMPMAILFILE $TEMPCONFIGFILE $TEMPFILE
        chmod 644 $TEMPMAILFILE $TEMPCONFIGFILE $TEMPFILE
        echo > $TEMPMAILFILE 
        echo > $TEMPCONFIGFILE
        echo > $TEMPFILE

	#****************************
	#We are doing our own sleep here
	#instead of in yum so that each
	#of the modules can be added
	#without worrying which one
	#has the randome time in it
	#****************************
	random_sleep
	#****************************
	#We are are checking the yum process
	#here because we were finding that 
	#yum would get stuck with the right
	#ftp problems
	#****************************
	check_yum

    #****************************
    #PRERUN
    #****************************
        # Allow certian customizations before running
        #****************************
        if [ "$PRERUN" != "" ]; then
                if [ "$DEBUG" = "true" ] ; then
                        echo "Executing PRERUN: $PRERUN"
                fi

                eval $PRERUN &> $TEMPPRERUNFILE
                if [ "$DEBUG" = "true" ] ; then
                        echo "PRERUN output:"
                        cat $TEMPPRERUNFILE
                fi

                if [[ -s $TEMPPRERUNFILE ]]; then
                        echo " --------------------" >> $TEMPMAILFILE
                        echo " YUM - PRE" >> $TEMPMAILFILE
                        echo " --------------------" >> $TEMPMAILFILE
                        cat $TEMPPRERUNFILE >> $TEMPMAILFILE
                        echo "" >> $TEMPMAILFILE
                fi
        fi

	

  #****************************
  #START Errata MODULE
  #****************************
	#The errata module is to install
	#security updates to the packages
	#already on a machine.
	#****************************
	MODULE="security"
	REPOFILE="security"
	HEAD="baseurl="
	TAIL="/errata/SL/RPMS/"
	
	if [ $DEBUG -eq 0 ] ; then
		echo "********** Starting ***** $MODULE ***** STARTING ********"
		echo "   Creating Config File"
	fi
	#****************************
	# Create yum.conf for this module
	# We will do that by simply taking the original yum.conf file
	# and adding the yum.cron.excludes to it
	#****************************
	grep -q "exclude=" $CONFIGFILE
	if [ $? -eq 0 ] ; then
		excludelist="`cat $EXCLUDESFILE | cut -d'=' -f2`"
		cat $CONFIGFILE | sed -e "s:exclude=.*:& $excludelist:" > $TEMPCONFIGFILE
	else
		excludelist="`cat $EXCLUDESFILE`"
		cat $CONFIGFILE | sed -e "s:\[main\].*:&\n$excludelist:" > $TEMPCONFIGFILE	
	fi
	if [ $DEBUG -eq 0 ] ; then
		echo "*********START** $MODULE YUM.CONF FILE **START**********"
		cat $TEMPCONFIGFILE
		echo "**********END*** $MODULE YUM.CONF FILE ***END***********"
	fi
	#****************************
	#Run yum and record if anything happens
	#****************************
	if [ $DEBUG -eq 0 ] ; then
		echo "    Starting Yum with command"
		echo "     /usr/bin/yum -c $TEMPCONFIGFILE -e 0 -d 1 -y update"		
	fi
	/usr/bin/yum -c $TEMPCONFIGFILE -e 0 -d 1 -y update > $TEMPFILE 2>&1
	if [ -s $TEMPFILE ] ; then
		if [ $DEBUG -eq 0 ] ; then
			echo "    We had changes"
			echo "*********START** ${MODULE} CHANGES FILE **START**********"
			cat $TEMPFILE
			echo "**********END*** ${MODULE} CHANGES FILE ***END***********"
		fi
		CHANGE=1
		echo " --------------------" >> $TEMPMAILFILE
		echo " YUM - ${MODULE}" >> $TEMPMAILFILE
		echo " --------------------" >> $TEMPMAILFILE
		cat $TEMPFILE >> $TEMPMAILFILE
	fi
	#****************************
	#Cleanup ${MODULE}
	#****************************
	if [ $DEBUG -eq 0 ] ; then
		echo " Cleaning Up ${MODULE}"
	fi
	yum -e 0 -d 0 -c $TEMPCONFIGFILE clean packages
	mv -f $TEMPCONFIGFILE /tmp/yum.conf.${MODULE}
  #****************************
  #END Errata MODULE
  #****************************

        #****************************
        #POSTRUN
        #****************************
        if [ "$POSTRUN" != "" ]; then
                if [ "$DEBUG" = "true" ] ; then
                        echo "Executing POSTRUN: $POSTRUN"
                fi

                eval $POSTRUN &> $TEMPPOSTRUNFILE
                if [ "$DEBUG" = "true" ] ; then
                        echo "POSTRUN output:"
                        cat $TEMPPOSTRUNFILE
                fi

                if [[ -s $TEMPPOSTRUNFILE ]]; then
                        echo " --------------------" >> $TEMPMAILFILE
                        echo " YUM - POST" >> $TEMPMAILFILE
                        echo " --------------------" >> $TEMPMAILFILE
                        cat $TEMPPOSTRUNFILE >> $TEMPMAILFILE
                        echo "" >> $TEMPMAILFILE
                fi
        fi
	
	#****************************
	#Send Mail If Needed
	#****************************
	if [ $CHANGE -eq 1 ] ; then
		if [ $DEBUG -eq 0 ] ; then
			echo " Mail Needs To Be Sent"
			echo "  /bin/mail -s \"YUM:$HOSTNAME:$TODAY\" $MAILLIST "
		fi
		/bin/mail -s "YUM:$HOSTNAME:$TODAY" $MAILLIST < $TEMPMAILFILE
	fi

	#****************************
	#Cleanup
	#****************************
	if [ $DEBUG -eq 0 ] ; then
		echo " Cleaning Up Interactive Yum"
	fi
	yum -e 0 -d 0 clean packages
	yum -e 0 -d 0 provides xyz > /dev/null 2>&1
	rm -f $TEMPMAILFILE
	rm -f $TEMPCONFIGFILE
	rm -f $TEMPRANDFILE
	rm -f $TEMPPRERUNFILE
	rm -f $TEMPPOSTRUNFILE
	rm -f $TEMPFILE
	if [ $DEBUG -eq 0 ] ; then
		echo "Yum Cron Exiting succesfully"
	fi
else
	if [ $DEBUG -eq 0 ] ; then
		echo "Yum cron not turned on.  Exiting"
	fi
	exit 1
fi
exit 0

