Download quotas for users


 
Thread Tools Search this Thread
Operating Systems Linux Ubuntu Download quotas for users
# 8  
Old 01-03-2009
Iptable is probably the final part of the puzzle, ie. how to block access, but other things are needed before getting that far.
# 9  
Old 01-03-2009
Ok then mate let's see what you can offer.

Happy New Year :-D
# 10  
Old 01-04-2009
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:
Code:
sudo cp userband_limit.sh /usr/bin
sudo cp userband_init.sh /etc/init.d
sudo cp bandlimit.conf /etc
sudo chown root:root /usr/bin/userband_limit.sh /etc/init.d/userband_init.sh /etc/bandlimit.conf
sudo chmod 755 /usr/bin/userband_limit.sh /etc/init.d/userband_init.sh
sudo chmod 644 /etc/bandlimit.conf
cd /etc/rc1.d
sudo ln -s ../init.d/userband_init.sh K99userband
cd /etc/rc3.d
sudo ln -s ../init.d/userband_init.sh S99userband
cd /etc/rc4.d
sudo ln -s ../init.d/userband_init.sh S99userband
cd /etc/rc5.d
sudo ln -s ../init.d/userband_init.sh S99userband

4. Edit the config file to add your usernames and bandwidth limits
Code:
sudo gedit /etc/bandlimit.conf

5. Start the bandwidth monitor
Code:
/etc/init.d/userband_init.sh start

NOTES
  1. Stopping the limiter using
    Code:
    /etc/init.d/userband_init.sh stop

    does not unblock the network. If you need to unblock the network you need run
    Code:
    sudo /sbin/iptables -D INPUT -j DROP

  2. It can take up to 1 minute after a new user who has no limit or who has not exceeded their limit logs in for the network to become available.
# 11  
Old 01-05-2009
Bug

WOW Reborg - if you did that in 20 mins you must be speed coder extrordinaire - I had only mangaged to get a glimpse of a perl solution.

greadey.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Disk Quotas

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)
Discussion started by: abhi7514
1 Replies

2. Solaris

quotas on two file system

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)
Discussion started by: cjashu
0 Replies

3. Solaris

Quotas on /export/home

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)
Discussion started by: grajp002
4 Replies

4. Linux

MySQL Quotas

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)
Discussion started by: Nilesh_lf
2 Replies

5. Filesystems, Disks and Memory

auto quotas?

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)
Discussion started by: mcady_02
3 Replies

6. UNIX for Dummies Questions & Answers

AIX 5.3 quotas

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)
Discussion started by: rondebbs
0 Replies

7. Solaris

Quotas and NIS

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)
Discussion started by: meyersp
4 Replies

8. Shell Programming and Scripting

Need help!! script for quotas

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)
Discussion started by: tomapam
2 Replies

9. Filesystems, Disks and Memory

setting quotas

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)
Discussion started by: cybler
1 Replies

10. UNIX for Advanced & Expert Users

disk quotas in UNIX?

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)
Discussion started by: zorro81
1 Replies
Login or Register to Ask a Question