Mini Shell

Direktori : /opt/sharedrads/
Upload File :
Current File : //opt/sharedrads/check_cpu

#!/bin/bash
# Shared RADS Tools, written by Erik Soroka (eriks@imhadmin.net, ext 834)
# This tool reports all pertinent CPU data on a particular user for RA review.

#lets set some important variables
CHECKUSER=$1
SESSIONID=$2
MONTH=`date '+%b'`
DAY=`date '+%d'`
HOUR=`date '+%H'`
NEWESTDATAAVG=`ls -ct1 /var/log/sa/rads/$MONTH/$DAY/*avg* | head -1 | cut -d. -f1`
NEWESTDATA=`ls -ct1 /var/log/sa/rads/$MONTH/$DAY/??.gz | head -1 | cut -d. -f1`

if [ -z $1 ]; then

#######################################################
## BEGIN GENERAL SERVER REPORT WITHOUT A SPECIFIC USER
#######################################################

USERS=`/bin/ls -1A /var/cpanel/users | wc -l`
TOTALCP=`/usr/sbin/sa -cm | head -1 | awk '{ print $5 }' | sed 's/cp//'`
AVERAGECP=`awk 'BEGIN{printf("%0.4f", '$USERS' / '$TOTALCP')}'`

echo
echo -e "    \033[40;37;2m  Displaying server's CPU usage as recorded by process accounting \033[0m"
echo -ne "    Total # of users: \033[37;1m $USERS"
echo -ne "\033[0m   "
echo -ne "Average CP: $AVERAGECP"
echo -ne "\033[0m   "
echo -ne "Total CP: $TOTALCP"
echo
echo
#top users from process accounting
/usr/sbin/sa -m | grep -v nobody | grep -v root | grep -v mailnull | sort -nk4 | tail -10 | head -9

echo

#are backups running?
CHKBKP=$(ps uawx | grep restic | wc -l)

        if [ $CHKBKP -gt 1 ]; then
                echo -e " \033[40;37;2m Shared Backup System Status \033[0m"  > /tmp/.status.$SESSIONID
                echo -e "            [\033[31;1m BACKUPS IN PROGRESS\033[0m ] "  >> /tmp/.status.$SESSIONID
	else
                echo -e " \033[40;37;2m Shared Backup System Status \033[0m"  > /tmp/.status.$SESSIONID
                echo -e "            [\033[32;1m BACKUPS NOT RUNNING\033[0m ] "  >> /tmp/.status.$SESSIONID
        fi

#to do: more status checks

CHKPKGACCT=$(ps uawx | grep pkgacct | grep -v grep | head -1 | awk '{print $13}')

	if [ -f /var/cpanel/users/$CHKPKGACCT ]; then
		echo >> /dev/null
	else
		CHKPKGACCT=$(ps uawx | grep pkgacct | grep -v grep | head -1 | awk '{print $14}')
	fi

		if [ "$CHKPKGACCT" != "" ]; then
			echo >> /tmp/.status.$SESSIONID
			echo -e "         \033[40;37;2m  cPanel Accounting Status  \033[0m"  >> /tmp/.status.$SESSIONID
                	echo -e "            [\033[31;1m PACKAGING: $CHKPKGACCT \033[0m] "  >> /tmp/.status.$SESSIONID
		fi

CHKRESTOREPKG=$(ps uawx | grep restorepkg | grep -v grep | head -1 | awk '{print $13}')

       	if [ -f /var/cpanel/users/$CHKRESTOREPKG ]; then
                echo >> /dev/null
       	else
                CHKRESTOREPKG=$(ps uawx | grep restorepkg | grep -v grep | head -1 | awk '{print $14}')
       	fi

        if [ "$CHKRESTOREPKG" != "" ]; then
                echo >> /tmp/.status.$SESSIONID
                echo -e "         \033[40;37;2m  cPanel Accounting Status  \033[0m"  >> /tmp/.status.$SESSIONID
               	echo -e "            [\033[31;1m RESTORING: $CHKPKGACCT \033[0m] "  >> /tmp/.status.$SESSIONID
        fi

CHKUPDATEDB=$(ps uawx | grep updatedb | grep -v grep)

        if [ "$CHKUPDATEDB" != "" ]; then
                echo >> /tmp/.status.$SESSIONID
               	echo -e "         \033[40;37;2m  Locate (updatedb) Status  \033[0m"  >> /tmp/.status.$SESSIONID
               	echo -e "            [\033[31;1m UPDATE IN PROGRESS \033[0m] "  >> /tmp/.status.$SESSIONID
        fi

CHKRSYNC=$(ps uawx | grep rsync | grep -v grep)

        if [ "$CHKRSYNC" != "" ]; then
                echo >> /tmp/.status.$SESSIONID
                echo -e "         \033[40;37;2m Rsync File Transfer Status \033[0m"  >> /tmp/.status.$SESSIONID
                echo -e "            [\033[31;1m RSYNCS IN PROGRESS \033[0m]"  >> /tmp/.status.$SESSIONID
        fi

CHKCPLOGD=$(ps uawx | grep cpanellogd | grep -v sleeping | wc -l)

        if [ $CHKCPLOGD -gt 1 ]; then
		echo >> /tmp/.status.$SESSIONID
                echo -e "          \033[40;37;2m cPanel Log Daemon Status \033[0m"  >> /tmp/.status.$SESSIONID
                echo -e "            [\033[31;1m cpanellogd running\033[0m ]"  >> /tmp/.status.$SESSIONID
        fi


if [ -f /var/log/sa/rads/lastcomm.cache ]; then
	#lets check to see how old the lastcomm cache is, and if >180 secs update it.
        CURRENT_DATE=$(date +%s)
        LASTDATA_DATE=$(stat --format=%X /var/log/sa/rads/lastcomm.cache)
	SECS_OLD=`echo $LASTDATA_DATE $CURRENT_DATE | awk '{ print ($2 - $1) }'`
        SECS_OLD_INT=`echo $SECS_OLD | cut -d. -f1`

	if [ $SECS_OLD_INT -gt 180 ]; then
		LCRUNNING=$(ps uawx | grep lastcommcache | wc -l)
		if [ $LCRUNNING -le 1 ]; then
			/opt/sharedrads/lastcommcache.sh
		fi
	fi
else
	touch /var/log/sa/rads/lastcomm.cache
fi

	#paste together lastcomm output and status column
	paste /var/log/sa/rads/lastcomm.cache /dev/null /tmp/.status.$SESSIONID

	#cleanup
	rm -f /tmp/.status.$SESSIONID

	echo
	exit 0

fi

###################################################
## BEGIN DETAILED REPORT FOR A SPECIFIC USER ($2)
###################################################

	#didn't receive a session ID
	if [ -z $2 ]; then
		SESSIONID=`date +%s`
		DETAILED=YES
	fi

	if [ ! -f /var/cpanel/users/$CHECKUSER ]; then
		echo
                echo -e "          \E[0;41m \E[5m     USER NOT FOUND:  THIS IS NOT A VALID CPANEL USER     \033[0m"
		echo
		exit 0
	fi

	echo -e "\033[30;1m                       `date`  \033[0m"

	#get most current data
	/usr/sbin/sa -cmi | grep "$CHECKUSER " > /tmp/.$SESSIONID

	echo
	echo -e "  \033[40;37;2m  Displaying today's most recent CPU usage data as recorded by process accounting \033[0m"

	#lets pull some info from our last poll archive
	SADATATIME=`zgrep "$CHECKUSER " $NEWESTDATAAVG.gz | awk '{print $4}'`
	SADATATIMEPCT=`zgrep "$CHECKUSER " $NEWESTDATAAVG.gz | awk '{print $5}'`
	SADATAMIN=`zgrep "$CHECKUSER " $NEWESTDATAAVG.gz  | awk '{print $6}'`
	SADATAMINPCT=`zgrep "$CHECKUSER " $NEWESTDATAAVG.gz  | awk '{print $7}'`
	SADATAMININT=`echo "$SADATAMIN" | cut -d'.' -f1`
	#SADATATOTAL=`zgrep "$CHECKUSER" $NEWESTDATA.gz | awk 'BEGIN{TOTAL=0}{TOTAL=TOTAL+$2}END{print TOTAL}'`


	#lets pull some info from the live/current sa data
	SADATATIMENOW=`cat /tmp/.$SESSIONID | awk '{print $4}'`
	SADATATIMEPCTNOW=`cat /tmp/.$SESSIONID  | awk '{print $5}'`
	SADATAMINNOW=`cat /tmp/.$SESSIONID   | awk '{print $6}'`
	SADATAMINPCTNOW=`cat /tmp/.$SESSIONID   | awk '{print $7}'`
	SADATAMININTNOW=`echo "$SADATAMINNOW" | cut -d'.' -f1`

if [ ! -z "$SADATAMININTNOW" ]; then
	if [ "$SADATAMININTNOW" -gt "0" ] && [ "$SADATAMININT" -gt "0" ]; then
		CPDIFF="`expr $SADATAMININTNOW - $SADATAMININT`"
		CPSINCE=`/usr/bin/stat -c%y $NEWESTDATAAVG.gz | awk '{print $2}' | cut -d: -f1-2`
	else
		CPDIFF="0"
		CPSINCE="N/A"
	fi
fi

if [ "$SADATAMININT" -gt "8" ]; then
#average CP for this user is over 8, lets make the figures RED

#echo -e " CPU minutes: \033[31;1m $SADATAMIN ($SADATAMINPCT)\033[0m  Actual time: \033[31;1m$SADATATIME ($SADATATIMEPCT)  \033[0m  CPU seconds: \033[31;1m$SADATATOTAL\cpu \033[0m"
echo -e "       CPU minutes: \033[31;1m $SADATAMINNOW ($SADATAMINPCTNOW)\033[0m          Actual time:  \033[31;1m$SADATATIMENOW ($SADATATIMEPCTNOW) "

else

#echo -e " CPU minutes: \033[33;32m $SADATAMIN ($SADATAMINPCT)\033[0m  Actual time: \033[33;32m$SADATATIME ($SADATATIMEPCT)\033[0m  CPU seconds: \033[33;32m$SADATATOTAL\cpu \033[0m"
echo -e "       CPU minutes: \033[33;32m $SADATAMINNOW ($SADATAMINPCTNOW)\033[0m          Actual time:  \033[33;32m$SADATATIMENOW ($SADATATIMEPCTNOW)  \033[0m"

fi

#report the amount of cp burned since the last poll until now
echo -e "\033[30;1m        (since my last data poll @ $CPSINCE `date +%Z` $CHECKUSER burned another ~$CPDIFF cp) \033[0m"
echo


        #determine home directory
        USERHOME=$(awk -F: -v v="$CHECKUSER" '{if ($1==v) print $6}' /etc/passwd)

       	#lets see if we have autoprockills data for this user
        if [ -f $USERHOME/.imh/autoprockills ]; then
                HOWMANY=$(grep KILLED $USERHOME/.imh/autoprockills | wc -l)
                echo -e "     \033[31;1m Auto Process Killer has logged $HOWMANY strike(s) against this account recently!\033[0m"
		echo -e "               \033[31;30m Hint: use check_autoprockills $CHECKUSER to view them\033[0m"
		echo
	fi


	if [ -f /var/cpanel/suspended/$CHECKUSER ]; then
		#user is suspended!
		echo -e "     \E[0;41m \E[5mWARNING: THIS USER IS CURRENTLY SUSPENDED, CHECK ACCOUNT NOTES FOR DETAILS \033[0m"
		echo
	fi

echo -e "\033[40;37;2m  # of executions for CPU intensive processes that have been spawned by this user today \033[0m"

zgrep $CHECKUSER $NEWESTDATA.gz > /tmp/.$SESSIONID

NUMPHP=`grep php /tmp/.$SESSIONID | wc -l`
echo -ne "php: \033[34;1m $NUMPHP \033[0m "

NUMPERL=`grep perl /tmp/.$SESSIONID | wc -l`
echo -ne "perl: \033[34;1m $NUMPERL \033[0m "

NUMIMAP=`grep imap /tmp/.$SESSIONID | wc -l`
echo -ne "imap: \033[34;1m $NUMIMAP \033[0m "

NUMPOP3=`grep pop3d /tmp/.$SESSIONID | wc -l`
echo -ne "pop3: \033[34;1m $NUMPOP3 \033[0m "

NUMEXIM=`grep exim /tmp/.$SESSIONID | wc -l`
echo -ne "exim: \033[34;1m $NUMEXIM \033[0m "

NUMBOX=`grep boxtrap /tmp/.$SESSIONID | wc -l`
echo -ne "boxtrap: \033[34;1m $NUMBOX \033[0m "

NUMFTP=`grep ftp /tmp/.$SESSIONID | wc -l`
echo -ne "ftp: \033[34;1m $NUMFTP \033[0m "

NUMCRON=`grep cron /tmp/.$SESSIONID | wc -l`
echo -ne "cron: \033[34;1m $NUMCRON \033[0m "

###############################################
### lets display the stats available for today
### 0 3, 6, 9, 12, 15, 18, 21
###############################################

DATAAVG00=`zgrep "$CHECKUSER" /var/log/sa/rads/$MONTH/$DAY/00.gz 2>/dev/null | awk 'BEGIN{TOTAL=0}{TOTAL=TOTAL+$2}END{print TOTAL / 60 }' | cut -c-4`
DATAAVG03=`zgrep "$CHECKUSER" /var/log/sa/rads/$MONTH/$DAY/03.gz 2>/dev/null | awk 'BEGIN{TOTAL=0}{TOTAL=TOTAL+$2}END{print TOTAL / 60 }' | cut -c-4`
DATAAVG06=`zgrep "$CHECKUSER" /var/log/sa/rads/$MONTH/$DAY/06.gz 2>/dev/null | awk 'BEGIN{TOTAL=0}{TOTAL=TOTAL+$2}END{print TOTAL / 60 }' | cut -c-4`
DATAAVG09=`zgrep "$CHECKUSER" /var/log/sa/rads/$MONTH/$DAY/09.gz 2>/dev/null | awk 'BEGIN{TOTAL=0}{TOTAL=TOTAL+$2}END{print TOTAL / 60 }' | cut -c-4`
DATAAVG12=`zgrep "$CHECKUSER" /var/log/sa/rads/$MONTH/$DAY/12.gz 2>/dev/null | awk 'BEGIN{TOTAL=0}{TOTAL=TOTAL+$2}END{print TOTAL / 60 }' | cut -c-4`
DATAAVG15=`zgrep "$CHECKUSER" /var/log/sa/rads/$MONTH/$DAY/15.gz 2>/dev/null | awk 'BEGIN{TOTAL=0}{TOTAL=TOTAL+$2}END{print TOTAL / 60 }' | cut -c-4`
DATAAVG18=`zgrep "$CHECKUSER" /var/log/sa/rads/$MONTH/$DAY/18.gz 2>/dev/null | awk 'BEGIN{TOTAL=0}{TOTAL=TOTAL+$2}END{print TOTAL / 60 }' | cut -c-4`
DATAAVG21=`zgrep "$CHECKUSER" /var/log/sa/rads/$MONTH/$DAY/21.gz 2>/dev/null | awk 'BEGIN{TOTAL=0}{TOTAL=TOTAL+$2}END{print TOTAL / 60 }' | cut -c-4`

#lets spit the data out into a file, with 8 rows even if the data isn't available yet.
if [ "$DATAAVG00" != "0" ]; then
	echo "12:00AM `date +%Z` :: "$DATAAVG00"cp" > /tmp/.cpumin.$SESSIONID
               	else
        echo -e "\033[31;30m (no data available)\033[0m" >> /tmp/.cpumin.$SESSIONID
fi

if [ "$DATAAVG03" != "0" ]; then
	echo "03:00AM `date +%Z` :: "$DATAAVG03"cp" >> /tmp/.cpumin.$SESSIONID
               	else
        echo -e "\033[31;30m (no data available)\033[0m" >> /tmp/.cpumin.$SESSIONID
fi

if [ "$DATAAVG06" != "0" ]; then
	echo "06:00AM `date +%Z` :: "$DATAAVG06"cp" >> /tmp/.cpumin.$SESSIONID
               	else
        echo -e "\033[31;30m (no data available)\033[0m" >> /tmp/.cpumin.$SESSIONID
fi

if [ "$DATAAVG09" != "0" ]; then
	echo "09:00AM `date +%Z` :: "$DATAAVG09"cp" >> /tmp/.cpumin.$SESSIONID
               	else
        echo -e "\033[31;30m (no data available)\033[0m" >> /tmp/.cpumin.$SESSIONID
fi

if [ "$DATAAVG12" != "0" ]; then
	echo "12:00PM `date +%Z` :: "$DATAAVG12"cp" >> /tmp/.cpumin.$SESSIONID
               	else
        echo -e "\033[31;30m (no data available)\033[0m" >> /tmp/.cpumin.$SESSIONID
fi

if [ "$DATAAVG15" != "0" ]; then
	echo "03:00PM `date +%Z` :: "$DATAAVG15"cp" >> /tmp/.cpumin.$SESSIONID
               	else
        echo -e "\033[31;30m (no data available)\033[0m" >> /tmp/.cpumin.$SESSIONID
fi

if [ "$DATAAVG18" != "0" ]; then
	echo "06:00PM `date +%Z` :: "$DATAAVG18"cp" >> /tmp/.cpumin.$SESSIONID
               	else
        echo -e "\033[31;30m (no data available)\033[0m" >> /tmp/.cpumin.$SESSIONID
fi

if [ "$DATAAVG21" != "0" ]; then
	echo "09:00PM `date +%Z` :: "$DATAAVG21"cp" >> /tmp/.cpumin.$SESSIONID
		else
	echo -e "\033[31;30m (no data available)\033[0m" >> /tmp/.cpumin.$SESSIONID
fi

		#lets do some math and get daily history for past 8 days
                BACK1MON=$(date +%b --date='1 day ago')
                BACK1DAY="$(date +%e --date='1 day ago')"
                BACK2MON=$(date +%b --date='2 days ago')
                BACK2DAY="$(date +%e --date='2 days ago')"
                BACK3MON=$(date +%b --date='3 days ago')
                BACK3DAY="$(date +%e --date='3 days ago')"
                BACK4MON=$(date +%b --date='4 days ago')
                BACK4DAY="$(date +%e --date='4 days ago')"
                BACK5MON=$(date +%b --date='5 days ago')
                BACK5DAY="$(date +%e --date='5 days ago')"
                BACK6MON=$(date +%b --date='6 days ago')
                BACK6DAY="$(date +%e --date='6 days ago')"
                BACK7MON=$(date +%b --date='7 days ago')
                BACK7DAY="$(date +%e --date='7 days ago')"
                BACK8MON=$(date +%b --date='8 days ago')
                BACK8DAY="$(date +%e --date='8 days ago')"

function getdaydata() {

		#lets build the variable based on the desired variable
		eval HISTMONTH=\$BACK${TIMETRAVEL}MON
		eval HISTDAYTMP=\$BACK${TIMETRAVEL}DAY

		#if we're in the beginning of the month, we need to add a "0" prefix for dir structure compatibility
		if [ "$HISTDAYTMP" -lt 10 ]; then
			HISTDAY="0$(echo $HISTDAYTMP)"
		else
			HISTDAY=$(echo $HISTDAYTMP)
		fi

		        HISTDATA=`ls -ct1 /var/log/sa/rads/$HISTMONTH/$HISTDAY/*avg* | head -1 | cut -d. -f1`
		        SADATAMIN=`zgrep "$CHECKUSER " $HISTDATA.gz  | awk '{print $6}'`
		        SADATAMINPCT=`zgrep "$CHECKUSER " $HISTDATA.gz  | awk '{print $7}'`

			if [ -z $SADATAMIN ]; then
                        	NULLDATA=true
			else
	                       	echo "$HISTMONTH $HISTDAY :: $SADATAMIN ($SADATAMINPCT)" >> /tmp/.historical.$SESSIONID

			fi

		if [ "$NULLDATA" == "true" ]; then
		        echo -e "\033[31;30m  (no data available)\033[0m   " >> /tmp/.historical.$SESSIONID
		fi
}


	#now lets call the function to actually collect and output the historical data
	for TIMETRAVEL in {1..8}
		do
				getdaydata $TIMETRAVEL
		done


	#lets collect the 8 most CPU intensive processes now
	/usr/bin/lastcomm $CHECKUSER | awk '{print $1 " :: " $(NF-5) " secs"}' | sort -r -nk3 | head -8 > /tmp/.cpuexpensive.$SESSIONID

echo
echo

#print header for this section
echo -e "\033[40;37;2mCPU minutes used today \033[0m        \033[40;37;2mHistorical CPU usage data \033[0m    \033[40;37;2m Most expensive processes \033[0m"

#paste the mini-sections together
paste /tmp/.cpumin.$SESSIONID /dev/null /tmp/.historical.$SESSIONID  /tmp/.cpuexpensive.$SESSIONID

echo

#remove temporary session files
rm -f /tmp/.$SESSIONID
rm -f /tmp/.cpumin.$SESSIONID
rm -f /tmp/.cpuexpensive.$SESSIONID
rm -f /tmp/.historical.$SESSIONID

#EOF

Zerion Mini Shell 1.0