I didn't get much time to look at this today, but I put something together in about 20 minutes to get started. I have done some basic testing and it does work but might not be the most robust yet.
The main script:
Code:
#!/bin/bash
#
#
# posted by reborg on The UNIX and Linux Forums
# (c) 2009 - The UNIX and Linux Forums
#
CONFIGFILE=/etc/bandlimit.conf
RUNDIR=/var/run/bandcap
STATSFILE=/proc/net/dev
AWK=/usr/bin/awk
DATE=/bin/date
IPTABLES=/sbin/iptables
TOUCH=/usr/bin/touch
WHO=/usr/bin/who
NETWORK_BLOCKED=0
block_network() {
if [[ $NETWORK_BLOCKED -ne 1 ]] ; then
$IPTABLES -A INPUT -j DROP
NETWORK_BLOCKED=1
fi
}
unblock_network() {
$IPTABLES -D INPUT -j DROP > /dev/null 2>&1
NETWORK_BLOCKED=0
}
init() {
if [[ -f ${RUNDIR}/pidfile ]] ; then
exit
else
echo $$ > ${RUNDIR}/pidfile
fi
INTERFACE=$($AWK '$1 == "INTERFACE" {print $2}' $CONFIGFILE)
USER_BANDWIDTH=$($AWK -v user="$username" '$1 == user { printf "%i\n", $2 * 1024 * 1024 ; exit}' $CONFIGFILE)
if [[ -z "$USER_BANDWIDTH" ]] ; then
unblock_network
CAP_USER=0
return
fi
CAP_USER=1
$TOUCH ${RUNDIR}/${username}.$($DATE +%m)
[[ -d $RUNDIR ]] || mkdir -p $RUNDIR
if [[ ! -f "${RUNDIR}/${username}" ]] ; then
echo $USER_BANDWIDTH > "${RUNDIR}/$username"
fi
REMAINING=$(< "${RUNDIR}/${username}" )
# note IFS on the next line is '<space><tab>:'
while IFS=' :' read net bandwidth junk; do
if [[ "$net" = "$INTERFACE" ]] ; then
STARTUP_BANDWIDTH=$bandwidth
fi
done < $STATSFILE
if (( REMAINING > 0 )) ; then
unblock_network
fi
}
run() {
while : ; do
set -- $($WHO)
if [[ $# -lt 1 ]] ; then
sleep 60
next
fi
if [[ "$1" != "$username" ]] ; then
username="$1"
init
fi
if [[ $CAP_USER -eq 1 ]] ; then
while IFS=' :' read net bandwidth junk; do
if [[ "$net" = "$INTERFACE" ]] ; then
LAST_MONTH=$($DATE -d "last month" +%m)
if [[ -f ${RUNDIR}/${username}.${LAST_MONTH} ]] ; then
rm ${RUNDIR}/${username}.${LAST_MONTH}
$TOUCH ${RUNDIR}/${username}.$($DATE +%m)
REMAINING=$USER_BANDWIDTH
STARTUP_BANDWIDTH=$bandwidth
unblock_network
fi
(( SESSION = bandwidth - STARTUP_BANDWIDTH ))
(( REMAINING = REMAINING - SESSION ))
if (( REMAINING <= 0 )) ; then
block_network
echo 0 > ${RUNDIR}/$username
else
echo $REMAINING > ${RUNDIR}/$username
fi
fi
done < $STATSFILE
fi
sleep 60
done
}
if [[ ! -f $CONFIGFILE ]] ; then
echo "No bandwidth limits set" >& 2
exit 2
fi
run
Config file
The format of this file is:
Code:
INTERFACE <iterface name>
<username> <monthly bandwidth limit in megabytes>
In this version you can have only one INTERFACE line and as many user lines as you want. Don't forget to update the INTERFACE line to eth0 if you use the attached sample config file.
Code:
INTERFACE wlan0
testuser 100
Init script
Code:
#!/bin/bash
PIDFILE=/var/run/bandcap/pidfile
case $1 in
start)
if [[ -f "$PIDFILE" ]] ; then
PID=$(< "$PIDFILE")
/bin/ps -fp $PID | grep -q userband_limit.sh
if [[ $? -eq 0 ]] ; then
echo "User bandwidth limiter already running" >&2
exit 1
else
rm "$PIDFILE"
$START_COMMAND
fi
else
( /usr/bin/userband_limit.sh & )
fi
;;
stop)
if [[ -f "$PIDFILE" ]] ; then
PID=$(< "$PIDFILE")
/bin/ps -fp $PID | grep -q userband_limit.sh
if [[ $? -ne 0 ]] ; then
rm "$PIDFILE"
echo "User bandwidth limiter already stopped" >&2
exit 1
else
rm "$PIDFILE"
/bin/kill $PID
fi
else
echo "User bandwidth limiter already stopped" >&2
exit 1
fi
;;
esac
Instructions:
1. Download the three files attached
2. Open a terminal and cd to the directory where you downloaded the files
3. Run the following commands:
RedHat 7.0
Caldera 2.4
From Windows 2000 server, you can allow ceratin users to use only so much disk space when they login.
I'm sure you can do that in Linux/UNIX, but I just don't know how or what you call them here (disk quotas maybe?). Anyway, any kind help is always greatly appreciated.... (1 Reply)
Hi, I'm trying to setup user quotas on my Linux system.
It is Suse 7.3 running the standard kernel from that release.
When I try to run quotaon, I get the following:
# quotaon -v /dev/hdc1
quotaon: using /home/aquota.user on /dev/hdc1: Invalid argument
I can't figure out what it means by... (1 Reply)
here is the file i must use to write my script (from a repquota -g) :
Group used soft grace ...
group1 -- 270000 0 0 ...
group2 -- 1500005 0 0 ...
group3 -- 55 0 0
... ... ... ... ...
the script has to... (2 Replies)
Has anyone ever used disk quotas with NIS? I tried to implement it the normal way but since the users only exist in NIS and not in the local passwd file when i try the edquota command it cannot find the user.
Thanks (4 Replies)
Hello, I will be creating several users on a new AIX box. I would like to limit the amount of data they can keep. How can I do that in smit or with a command.
Thanks - Brad (0 Replies)
Heres a stupid question to all you Linux gods/goddesses.
Is there any way to have the system automatically set user quotas when a user account is created or does it have to be done by hand or a script. Every article, thread, anything Ive read so far only shows how to set quotas manually. Any... (3 Replies)
How do I limit the maximum usage space for a specific MySQL user ??
Databases can be unlimited but the total sum of data on all tables and databases shouldn't exceed a limit I want. How to do this ?? (2 Replies)
Hello all,
I am trying to set quotas on /export/home filesystem for some of our users on a Solaris Zone I know that you would be redirecting me to some of the documentation pages, but I have already done that.
The /export/home on the Zone is a Veritas FS and I cannot see an entry for... (4 Replies)
Hi Guys,
I have quota support turned on, on two file systems. However, when I do a repquota -va I get report only for one. What might be the problem?
I will really appreciate your help.
Thanks Gurus. (0 Replies)
Hi,
I'm practicing new things with Linux/Unix and I need someone to point me at the right direction.
Lets say I create a user named user1. After creating it, how would I enable quotas for it. I have already added "userquota" under "/etc/fstab" and rebooted my system, but after I run the command... (1 Reply)