CHILDACCTS=`/bin/grep -rl OWNER=$USER /var/cpanel/users|cut -d/ -f5`
# No reseller inputted, display top resellers sorted by cumulative CP #
touch /tmp/sumrescp.$SESSIONID
touch /tmp/rescp.$SESSIONID
sa -cm |awk '{print $1,$6}' > /tmp/sadata.$SESSIONID
cat /tmp/sadata.$SESSIONID| while read line; do
ruser=$(echo $line|awk '{print $1}')
rusercp=$(echo $line|sed 's/[A-Za-z]*//g'|awk '{print $NF}')
rparent=$(grep OWNER /var/cpanel/users/$ruser 2>/dev/null|cut -d= -f2)
echo "$rparent $rusercp" >> /tmp/rescp.$SESSIONID
awk '{arr[$1]+=$2;} END {for (i in arr) print i, arr[i]}' /tmp/rescp.$SESSIONID > /tmp/sumrescp.$SESSIONID
echo -e "Top 15 resellers sorted by CP used so far today:"
cat /tmp/sumrescp.$SESSIONID |egrep -v "root|nobody|mailnull|inmotion|cpanellogin|clamav|dovenull|nagios|cpanel" |sort -nk2 |tail -15 |tac|sed 's/$/cp/'|column -t
###############################################
# Showing cumulative CP for specific reseller #
###############################################
if [[ `grep OWNER /var/cpanel/users/$USER -s` = "OWNER=inmotion" ]]; then
ALLACCTS=$( echo "$CHILDACCTS"; echo "$USER")
echo -e " Cumulative CP usage of reseller: $USER"
echo -ne " Total # of child accounts: "
echo -ne " Total CP used today (`date --date="today"| awk '{print $2,$3}'`): "
sa -cm |egrep -w `echo $ALLACCTS|sed ':a;N;$!ba;s/\n/ /g'|sed 's# #|#g'`|awk '{s+=$6} END {printf "%.0f", s}'
echo "Historical cumulative CPU usage"
DAY1=$(echo "`date +"%b/%d" --date="-1 days"`")
zcat /var/log/sa/rads/$DAY1/21-avg.gz | egrep -w `echo $ALLACCTS|sed ':a;N;$!ba;s/\n/ /g'|sed 's# #|#g'`|awk '{s+=$6} END {printf "%.0f", s}'
DAY2=$(echo "`date +"%b/%d" --date="-2 days"`")
zcat /var/log/sa/rads/$DAY2/21-avg.gz | egrep -w `echo $ALLACCTS|sed ':a;N;$!ba;s/\n/ /g'|sed 's# #|#g'`|awk '{s+=$6} END {printf "%.0f", s}'
DAY3=$(echo "`date +"%b/%d" --date="-3 days"`")
zcat /var/log/sa/rads/$DAY3/21-avg.gz | egrep -w `echo $ALLACCTS|sed ':a;N;$!ba;s/\n/ /g'|sed 's# #|#g'`|awk '{s+=$6} END {printf "%.0f", s}'
DAY4=$(echo "`date +"%b/%d" --date="-4 days"`")
zcat /var/log/sa/rads/$DAY4/21-avg.gz | egrep -w `echo $ALLACCTS|sed ':a;N;$!ba;s/\n/ /g'|sed 's# #|#g'`|awk '{s+=$6} END {printf "%.0f", s}'
DAY5=$(echo "`date +"%b/%d" --date="-5 days"`")
zcat /var/log/sa/rads/$DAY5/21-avg.gz | egrep -w `echo $ALLACCTS|sed ':a;N;$!ba;s/\n/ /g'|sed 's# #|#g'`|awk '{s+=$6} END {printf "%.0f", s}'
DAY6=$(echo "`date +"%b/%d" --date="-6 days"`")
zcat /var/log/sa/rads/$DAY6/21-avg.gz | egrep -w `echo $ALLACCTS|sed ':a;N;$!ba;s/\n/ /g'|sed 's# #|#g'`|awk '{s+=$6} END {printf "%.0f", s}'
DAY7=$(echo "`date +"%b/%d" --date="-7 days"`")
zcat /var/log/sa/rads/$DAY7/21-avg.gz | egrep -w `echo $ALLACCTS|sed ':a;N;$!ba;s/\n/ /g'|sed 's# #|#g'`|awk '{s+=$6} END {printf "%.0f", s}'
echo "Displaying top 20 child accounts sorted by CP for reseller ($USER): "
sa -cm |egrep -w `echo $ALLACCTS|sed ':a;N;$!ba;s/\n/ /g'|sed 's# #|#g'`|awk '{print " "$1,$6}'|head -20|column -t
# Display error/help message
echo "rescp.sh - A script made to help identify reseller accounts that are consuming a large amount of CP"
echo " Option 1.) Run script with no arguments (calculates cumulative CP usage for reseller users across the server)"
echo " Option 2.) Run script with a reseller username (see children with the highest CP)"
rm -f /tmp/rescp.$SESSIONID
rm -f /tmp/sumrescp.$SESSIONID
rm -f /tmp/sadata.$SESSIONID