Mini Shell

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

#!/bin/bash
# This script finds the top CPU users on the machine and fires in separate
# account move tickets to the queue.  Written by Erik S, ext 834 (e@inmotion.net)

#set some variables
SESSIONID=`date +%s`
OURSERVER=`hostname -s`
#RECIPIENT="eriks@inmotionhosting.com"
RECIPIENT="moves@imhadmin.net"

if [ -z "$1" ]; then
	echo "`echo $0 | cut -d/ -f4` - Written by Erik S. (x834/e@inmotion.net)"
	echo
	echo "This script will generate move tickets for specified number of users"
	echo "sorted by highest CP count first.  If a move ticket has already been"
	echo "generated, the user will be skipped and a higher number can be passed."
	echo "If the --force flag is passed, move tickets will be force generated."
	echo
	echo "Usage:"
	echo "       $0 <# of accts> [--force]"
	echo
	exit 0
fi

        if [ "$2" == "--force" ]; then
                FORCE=TRUE
        fi

function generate_ticket {
        MVUSER_HOME=`file /home*/$MVUSER | awk -F: '{print $1}'`
	echo $OURSERVER > $MVUSER_HOME/tmp/.move
        chown $MVUSER.$MVUSER $MVUSER_HOME/tmp/.move
        echo -e "  Username       Diskspace         Homedir  Pointed Local?      Dedicated IP?           Has SSL?          local MX?    Suspended? \n $line" | mail -s "Account move: $MVUSER @ $SERVER" $RECIPIENT
        echo "[*] generated move ticket for $MVUSER"
}

#INPUT/ERROR HANDLING
NUMBER=$1

	expr $NUMBER + 1 1>/dev/null 2>/dev/null
        if [ $? = 0 ]; then
                echo "$NUMBER is a number" >> /dev/null
        else
		echo "ERROR: You did not provide a number of accounts to schedule for move."
		exit 0
	fi

	if [ $NUMBER -lt 3 ]; then
		echo "ERROR: Number of accounts to schedule for move must be greather than 3."
		exit 0
	fi

#DUMP DATA FROM LISTACCOUNTS
sa -m | sort -nk4 | grep -v root | grep -v mysql | grep -v mail | grep -v cpanel | grep -v nobody | tail -$NUMBER > /tmp/.$SESSIONID


# User define Function (UDF)
processLine(){
  line="$@" # get all args
  MVUSER=`echo $line | awk '{print $1}'`
  SERVER=`hostname -s`

	if [ "$FORCE" == "TRUE" ]; then
		generate_ticket
		continue
	fi

	if [ -f /var/cpanel/suspended/$MVUSER ]; then #check for suspension
		echo "[*] $MVUSER account is suspended, skipping"
		continue
	fi

	if [ -f $MVUSER_HOME/tmp/.move ]; then  #if user has already been scheduled to move once, lets check if it was our machine

		MOVEBOX=`cat $MVUSER_HOME/tmp/.move`

		if [ "$MOVEBOX" == "$OURSERVER" ]; then
			echo "[*] already generated ticket for $MVUSER, skipping"
			continue
		fi

	else
		generate_ticket
fi

}

#Specify the file we outputted to
FILE="/tmp/.$SESSIONID"

#make sure file exist and readable
   if [ ! -f $FILE ]; then
  	echo "$FILE : does not exist, check to see if listacct tool is working, escalate to T3 if the problem persists."
  	exit 1
   elif [ ! -r $FILE ]; then
  	echo "$FILE: can not read, escalate to T3 if this problem persists"
  	exit 2
   fi

# Set loop separator to end of line
BAKIFS=$IFS
IFS=$(echo -en "\n\b")
exec 3<&0
exec 0<"$FILE"
while read -r line
do
	processLine $line
done
exec 0<&3

# restore $IFS which was used to determine what the field separators are
IFS=$BAKIFS

rm -f /tmp/.$SESSIONID
exit 0

Zerion Mini Shell 1.0