Mini Shell
#!/bin/bash
# Shared RADS Tools, written by Erik Soroka (eriks@imhadmin.net, ext 834)
CHECKUSER=$1
SESSIONID=$2
if [ -z $1 ]; then
echo
echo "Usage: check_bandwidth [username]"
echo
exit 0
fi
if [ -z $2 ]; then
SESSIONID=`date +%s`
DETAILED=YES
fi
if [ "$2" == "--plaintext" ]; then
RADSCOLORS=off
fi
MONTH=`date +%m | cut -d0 -f2`
YEAR=`date +%Y`
echo
#lets make sure we have bandwidth data for this user
if [ -f /var/cpanel/bandwidth/$CHECKUSER ]; then
# COLLECT HTTP BANDWIDTH DATA
cat /var/cpanel/bandwidth/$CHECKUSER |grep http |grep "$MONTH...$YEAR" > /tmp/.$SESSIONID
echo -e "\033[40;37;2m HTTPD \033[0m" > /tmp/.$SESSIONID.http
for bw in `cat /tmp/.$SESSIONID`; do
HTTPDATE=`echo $bw | cut -d'-' -f1`
HTTPBYTES=`echo $bw | awk -F "=" '{print $2}'`
HTTPMB=`echo $HTTPBYTES/1024/1024 | bc`
if [ "$RADSCOLORS" == "off" ]; then
echo "HTTPDATE : $HTTPMB MB"
else
echo -e "\033[29;30;1m$HTTPDATE \033[31;34m:\033[0m\033[29;33m $HTTPMB MB \033[0m"
fi
done >> /tmp/.$SESSIONID.http
# COLLECT IMAP BANDWIDTH DATA
cat /var/cpanel/bandwidth/$CHECKUSER |grep imap |grep "$MONTH...$YEAR" > /tmp/.$SESSIONID
echo -e "\033[40;37;2m IMAP \033[0m" > /tmp/.$SESSIONID.imap
for bw in `cat /tmp/.$SESSIONID`; do
IMAPDATE=`echo $bw | cut -d'-' -f1`
IMAPBYTES=`echo $bw | awk -F "=" '{print $2}'`
IMAPMB=`echo $IMAPBYTES/1024/1024 | bc`
if [ "$RADSCOLORS" == "off" ]; then
echo "$IMAPDATE : $IMAPMB MB"
else
echo -e "\033[29;30;1m$IMAPDATE \033[31;34m:\033[0m\033[29;33m $IMAPMB MB \033[0m"
fi
done >> /tmp/.$SESSIONID.imap
# COLLECT POP3 BANDWIDTH DATA
cat /var/cpanel/bandwidth/$CHECKUSER |grep pop3 |grep "$MONTH...$YEAR" > /tmp/.$SESSIONID
echo -e "\033[40;37;2m POP3 \033[0m" > /tmp/.$SESSIONID.pop3
for bw in `cat /tmp/.$SESSIONID`; do
POP3DATE=`echo $bw | cut -d'-' -f1`
POP3BYTES=`echo $bw | awk -F "=" '{print $2}'`
POP3MB=`echo $POP3BYTES/1024/1024 | bc`
if [ "$RADSCOLORS" == "off" ]; then
echo "$POP3DATE : $POP3MB MB"
else
echo -e "\033[29;30;1m$POP3DATE \033[31;34m:\033[0m\033[29;33m $POP3MB MB \033[0m"
fi
done >> /tmp/.$SESSIONID.pop3
# COLLECT FTP BANDWIDTH DATA
cat /var/cpanel/bandwidth/$CHECKUSER |grep ftp |grep "$MONTH...$YEAR" > /tmp/.$SESSIONID
echo -e "\033[40;37;2m FTP \033[0m" > /tmp/.$SESSIONID.ftp
for bw in `cat /tmp/.$SESSIONID`; do
FTPDATE=`echo $bw | cut -d'-' -f1`
FTPBYTES=`echo $bw | awk -F "=" '{print $2}'`
FTPMB=`echo $FTPBYTES/1024/1024 | bc`
if [ "$RADSCOLORS" == "off" ]; then
echo "$FTPDATE : $FTPMB MB"
else
echo -e "\033[29;30;1m$FTPDATE \033[31;34m:\033[0m\033[29;33m $FTPMB MB \033[0m"
fi
done >> /tmp/.$SESSIONID.ftp
# we're just giving the summary since this was invoked from the check script
if [ "$RADSCOLORS" == "off" ]; then
echo " Displaying summary of bandwidth transfer for this user this month: "
else
echo -e " \033[40;37;2m Displaying summary of bandwidth transfer for this user this month: \033[0m"
fi
HTTPTOTALMB=`cat /tmp/.$SESSIONID.http | awk '{print $3}' | awk '{total = total + $1}END{print total}'`
IMAPTOTALMB=`cat /tmp/.$SESSIONID.imap | awk '{print $3}' | awk '{total = total + $1}END{print total}'`
FTPTOTALMB=`cat /tmp/.$SESSIONID.ftp | awk '{print $3}' | awk '{total = total + $1}END{print total}'`
POP3TOTALMB=`cat /tmp/.$SESSIONID.pop3 | awk '{print $3}' | awk '{total = total + $1}END{print total}'`
if [ "$RADSCOLORS" == "off" ]; then
echo " HTTP: $HTTPTOTALMB MB IMAP: $IMAPTOTALMB MB POP3: $POP3TOTALMB MB FTP: $FTPTOTALMB MB"
else
echo -e " HTTP: \033[33;32m $HTTPTOTALMB MB\033[0m IMAP: \033[33;32m $IMAPTOTALMB MB\033[0m POP3: \033[33;32m$POP3TOTALMB MB \033[0m FTP: \033[33;32m$FTPTOTALMB MB \033[0m"
fi
echo
#no session ID was received earlier, we were invoked directly, lets display add'l history:
if [ "$DETAILED" == "YES" ]; then
echo
echo -e "\033[40;37;2m Displaying details of user's bandwidth transfer for various services over the past month \033[0m"
echo
paste /tmp/.$SESSIONID.http /tmp/.$SESSIONID.imap /tmp/.$SESSIONID.pop3 /tmp/.$SESSIONID.ftp
echo
echo -ne "\033[0m"
echo
fi
rm -f /tmp/.$SESSIONID
rm -f /tmp/.$SESSIONID.http
rm -f /tmp/.$SESSIONID.imap
rm -f /tmp/.$SESSIONID.pop3
rm -f /tmp/.$SESSIONID.ftp
rm -f /tmp/.$SESSIONID.smtp
else
#we do not have bandwidth data for this user
echo -e " ERROR: Could not locate any bandwidth data for $CHECKUSER in /var/cpanel/bandwidth/ \033[0m "
echo
exit 0
fi
Zerion Mini Shell 1.0