Mini Shell
#!/bin/bash
# This script finds the top 10 large disk 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="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 largest account 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 {
mkdir -p /$MVUSER_HOME/.imh/
echo $OURSERVER > /$MVUSER_HOME/.imh/.move
chown $MVUSER.$MVUSER /$MVUSER_HOME/.imh/.move
echo "A new server disk move is required for $MVUSER @ $SERVER" > /tmp/.move.$SESSIONID
echo >> /tmp/.move.$SESSIONID
echo "Move Username: $MVUSER" >> /tmp/.move.$SESSIONID
echo "Account Size: $SIZE" >> /tmp/.move.$SESSIONID
echo "Points to us: $POINTED" >> /tmp/.move.$SESSIONID
echo "Dedicated IP: $DEDICATED" >> /tmp/.move.$SESSIONID
echo "SSL Certificate: $CERTIFICATE" >> /tmp/.move.$SESSIONID
echo >> /tmp/.move.$SESSIONID
echo "After successfully completing the move please close this ticket and schedule a reclaim." >> /tmp/.move.$SESSIONID
echo >> /tmp/.move.$SESSIONID
cat /tmp/.move.$SESSIONID | /opt/sharedrads/mailers/diskmovemailer.py $MVUSER $SERVER
rm -f /tmp/.move.$SESSIONID
echo "[*] generated move ticket for $MVUSER (size: $SIZE MB)"
}
#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 1 ]; then
echo "ERROR: Number of accounts to schedule for move must be greather than 1."
exit 0
fi
#DUMP DATA FROM LISTACCOUNTS
/opt/sharedrads/listacct --large_disk | egrep -v 'Username|Fetching|/root' | tail -$NUMBER > /tmp/.$SESSIONID
# User define Function (UDF)
processLine(){
line="$@" # get all args
MVUSER=`echo $line | awk '{print $1}'`
MVUSER_HOME=`file /home*/$MVUSER | awk -F: '{print $1}'`
SIZE=`echo $line | awk '{print $4}'`
POINTED=`echo $line | awk '{print $6}'`
IP=`echo $line | awk '{print $7}'`
if [ "$IP" == "DEDICATED" ]; then
DEDICATED="YES"
else
DEDICATED="NO"
fi
CERT=`echo $line | awk -F' ' '{print $3}' | awk -F' ' '{print $1}'`
if [ "$CERT" == "NO" ]; then
CERTIFICATE="NO"
else
CERTIFICATE="YES"
fi
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 (size: $SIZE)"
continue
fi
if [ -f /opt/sharedrads/autosuspend/$MVUSER ]; then #check for sched suspension
echo "[*] $MVUSER account is scheduled for suspension, skipping (size: $SIZE)"
continue
fi
if [ -f /$MVUSER_HOME/.imh/.move ]; then #if user has already been scheduled to move once, lets run some checks
CURRENT_DATE=$(date +%s)
MOVE_DATE=$(stat --format=%X /$MVUSER_HOME/.imh/.move)
DAYSOLD=`echo $MOVE_DATE $CURRENT_DATE | awk '{ print ($2 - $1)/86400 }'`
DAYSOLDINT=`echo $DAYSOLD | cut -d. -f1`
if [ $DAYSOLDINT -gt 4 ]; then
rm -f /$MVUSER_HOME/.imh/.move
echo "[*] a previous ticket for $MVUSER was sent >4 days ago, forcing new ticket..."
generate_ticket
continue
fi
MOVEBOX=`cat /$MVUSER_HOME/.imh/.move`
if [ "$MOVEBOX" == "$OURSERVER" ]; then
echo "[*] already generated ticket for $MVUSER, skipping (size: $SIZE)"
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