Gentoo Archives: gentoo-server

From: Jens Gutzeit <gentoo-server@×××××××.at>
To: gentoo-server@l.g.o
Subject: Re: [gentoo-server] Per user monthly transfer quota
Date: Tue, 06 Apr 2004 16:06:16
Message-Id: 200404061813.43833.gentoo-server@gutzeit.at
In Reply to: [gentoo-server] Per user monthly transfer quota by Jernej Kos
1 On Monday 05 April 2004 17:49, Jernej Kos wrote:
2 > How could i see how many megabytes did a specified user transfer in the
3
4 I assume you mean a specific domain, rather then a specific system user,
5 right?
6
7 > last month and/or limit it ? Especialy i would need this for apache.
8
9 see: parse your log.
10 limiting: some scripts that disables a website after checking the traffic
11 report.
12
13 For parsing the logs there are 2 options:
14 1. parse the access.log
15 or
16 2. create a very simple logformat, which logs "$domain $bytessend", s.th. like
17 this:
18 LogFormat "%v %B" traffic
19 CustomLog /var/log/apache/traffic.log traffic
20
21 See http://httpd.apache.org/docs-2.0/mod/mod_log_config.html.en#formats for
22 description about the %v and %B. This gives you a log like this:
23
24 phpbb.de 0
25 phpbb.de 4583
26 phpbb.de 7444
27 phpbb.de 75
28 phpbb.de 693
29
30 You only need to read that log with a script, split the lines on <whitespace>
31 and sum the numbers up. I would use a simple python script, for example:
32 #!/usr/bin/python
33 import sys, string, re
34
35 dom = sys.argv[1]
36 send = 0
37
38 while 1:
39 line = sys.stdin.readline()
40 if line == "":
41 break
42
43 (domain, bytes) = re.split(" ", string.strip(line))
44 if domain == dom:
45 send += int(bytes)
46
47 print send
48
49 Usage:
50 ./traffic.py phpbb.de < /var/log/apache/traffic.log
51
52 Gives you:
53 14202493
54
55 Now you know how many traffic a domain makes, you only need to disable a
56 virtualhost if the limit has been reached.
57
58 I would suggest to put every virtualhost in a seperate file, then you only
59 need to move a configfile away and SIGHUP apache to enforce the limit.
60
61 > Regards,
62 > Kostko.
63
64 Jens