#!/bin/sh ver="1.3" vdate="2nd July 2003" # # 1.3 02/Jul/2003 Cleaned up Email # 1.2 10/Mar/2002 Tidy up in preperation for public release # 1.1 23/Jan/2001 Added Log rotation section # 1.0 18/Nov/2000 Completed # date=$(/bin/date "+%Y/%m/%d %H:%M:%S") today=$(/bin/date +%Y/%m/%d) yest=$(/bin/date --date="1 day ago" +%Y/%m/%d) rotate_date=$(/bin/date --date="1 month ago" +%Y/%m/%d) script=$(/bin/basename $0 2>/dev/null) hist_2day=" * No Logging enabled" hist_yest=" * No Logging enabled" panic=/tmp/.$script.panic email_contents=/tmp/.$script.email email="" showparam="" alert="" help="" while getopts ":w:p:e:l:s" option; do case $option in w ) warn=$(/bin/echo $OPTARG | /bin/sed s~[^0-9\.]~~g 2>/dev/null);; p ) period=$(/bin/echo $OPTARG | /bin/sed s~[^0-9]~~g 2>/dev/null);; e ) email=$OPTARG;; l ) logfile=$OPTARG;; s ) showparam=1;; * ) help=1;; esac done if [ ! -z "$showparam" ] || [ ! -z "$help" ] || [ -z "$period" ] || [ -z "$warn" ]; then if [ -z "$warn" ]; then err_1="*";else err_1=" ";fi if [ -z "$period" ]; then err_2="*";else err_2=" ";fi /bin/echo "$script: v$ver on $vdate, http://scripts.akakul.co.uk/cpu_temp" /bin/echo "Usage: ./$script -w temperature -p period [-e email] [-l logfile] [-s]" /bin/echo "* denotes missing 'required' argument('s)" /bin/echo "$err_1 -w: Temperature to trigger Email, +ve real/integer i.e. 39.5" /bin/echo "$err_2 -p: One email per 'period'(minutes) is sent, +ve integer i.e. 60" /bin/echo " -e: [optional] Email Address i.e. nobody@akakul.co.uk" /bin/echo " -l: [optional] Full path to log history i.e. /var/log/cputemp.log" /bin/echo " -s: [optional] Show Parameters (test correct order), and EXIT" /bin/echo "e.g. ./$script -w 45 -p 360 -e nobody@akakul.co.uk -l /var/log/cputemp.log" /bin/echo /bin/echo "Parameters Input:" /bin/echo " -w (Warning Temperature): $warn" /bin/echo " -p (Email Period): $period" /bin/echo " -e (Email Address): $email" /bin/echo " -l (History Log File): $logfile" exit 1 fi curr_temp=$(/bin/grep temperature /proc/cpuinfo | /bin/awk {'print $3'} 2>/dev/null) warning=$(/bin/echo $curr_temp $warn | /bin/awk {'if ($1 >= $2) print "PANIC"'} 2>/dev/null) dont_email=$(/usr/bin/find $panic -mmin -$period -print 2>/dev/null) if [ ! -z "$logfile" ]; then if [ ! -z $warning ]; then alert="\tALERT ($warn $period $email)" fi /bin/echo -e "$date\t$curr_temp$alert" >> $logfile fi # START Log rotate section (keeps 1 months of logs max) rotate=$(/bin/grep -n "^$rotate_date" $logfile | /usr/bin/tail -1 | \ /bin/awk -F: {'if ($1 > 1) print $1'} 2>/dev/null) if [ ! -z "$rotate" ]; then tail_num=$(($(/usr/bin/wc -l $logfile | /bin/awk {'print $1'} 2>/dev/null) - $rotate)) /bin/mv -f $logfile $logfile.tmp /usr/bin/tail -$tail_num $logfile.tmp > $logfile /bin/rm -rf $logfile.tmp /bin/echo -e "$date\tLOG ROTATED" >> $logfile fi # END Log rotation section if [ ! -z $dont_email ]; then exit 0 fi /bin/rm -rf $panic /bin/echo "CPU Temperature Monitor has been triggered.\nhttp://scripts.akakul.co.uk/cpu_temp\n\ You will NOT be emailed about this again for another $period minutes\n\n\ Current CPU Temperature: $curr_temp (>= $warn)\n\nTodays History:\n" > $email_contents if [ ! -z "$warning" ]; then /bin/touch $panic 2>/dev/null if [ -f "$logfile" ]; then /bin/echo -e "$date\t$curr_temp$alert EMAILED" >> $logfile hist_2day=$(/bin/grep "^$today" $logfile | /bin/sed s~^$today~~ | \ /bin/sort -r >> $email_contents 2>/dev/null) /bin/echo "\n\nYesterdays History:\n" >> $email_contents hist_yest=$(/bin/grep "^$yest" $logfile | /bin/sed s~^$yest~~ | \ /bin/sort -r >> $email_contents 2>/dev/null) fi if [ ! -z "$email" ]; then /bin/cat $email_contents | /bin/mail -s "Warning CPU Temp=$curr_temp @ $date" $email else /bin/cat $email_contents fi fi /bin/rm -f $email_contents # End of Script