Skip to content

Commit 8d6957e

Browse files
committed
allow username exclusion
1 parent 9749d03 commit 8d6957e

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Usage
3030
```
3131
usage: squid2radius.py [-h] [-p RADIUS_ACCT_PORT]
3232
[--radius-nasid RADIUS_NASID] [--squid-path SQUID_PATH]
33+
[--exclude-pattern EXCLUDE_PATTERN]
3334
logfile_path radius_server radius_secret
3435
```
3536

@@ -43,8 +44,13 @@ It is certainly a good idea to make a cron job for this.
4344

4445
You should also read [SquidFaq/SquidLogs](http://wiki.squid-cache.org/SquidFaq/SquidLogs#access.log) to make sure your log files are in reasonable sizes.
4546

47+
--exclude-pattern
48+
-----------------
49+
50+
If for some reason you need to prevent usage information of certain user from being sent to the RADIUS server, there is an argument for that! Use `--exclude-pattern="(girl|boy)friend"` and squid2radius won't send usage of either your `girlfriend` or `boyfriend` to the RADIUS server.
51+
4652
Note
47-
----
53+
====
4854

4955
The script assumes that you are using the default [Squid native access.log format](http://wiki.squid-cache.org/Features/LogFormat#squid) on first ten columns of your log file. If you need custom columns, add them after the default ones.
5056

squid2radius.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import sys
44
import argparse
55
import time
6+
import re
67
from subprocess import call
78
import pyrad.packet
89
from pyrad.client import Client
@@ -17,6 +18,10 @@
1718
parser.add_argument('-p', '--radius-acct-port', default='1813')
1819
parser.add_argument('--radius-nasid', default='squid')
1920
parser.add_argument('--squid-path', default='/usr/sbin/squid')
21+
parser.add_argument('--exclude-pattern', default='', help='do not send to ' \
22+
'server if ' \
23+
'username contains '\
24+
'this regexp')
2025
args = parser.parse_args()
2126

2227

@@ -26,7 +31,7 @@
2631
sys.stdout.write("Analyzing")
2732
sum_bytes = {}
2833
for i, line in enumerate(logfile):
29-
if i % 1000 == 0: sys.stdout.write('.')
34+
if i % 1000 == 0: sys.stdout.write('.'); sys.stdout.flush()
3035

3136
# http://wiki.squid-cache.org/Features/LogFormat
3237
_, _, _, _, num_bytes, _, _, rfc931, _, _ = line.split()[:10]
@@ -44,10 +49,21 @@
4449
dict=Dictionary(sys.path[0] + "/dictionary"))
4550

4651

52+
if args.exclude_pattern:
53+
print "Exclusion check has been enabled."
54+
exclude_pattern = re.compile(args.exclude_pattern)
55+
56+
4757
print "Sending..."
4858
for username, total_bytes in sum_bytes.iteritems():
4959
sys.stdout.write(username + ' ' + str(total_bytes))
5060
sys.stdout.write('.')
61+
sys.stdout.flush()
62+
63+
if args.exclude_pattern and exclude_pattern.match(username):
64+
sys.stdout.write("..skipped!\n")
65+
sys.stdout.flush()
66+
continue
5167

5268
session_id = str(time.time())
5369

@@ -62,6 +78,7 @@
6278
raise Exception("mysterious RADIUS server response to Start packet")
6379

6480
sys.stdout.write('.')
81+
sys.stdout.flush()
6582

6683
req = srv.CreateAcctPacket()
6784
req['User-Name'] = username
@@ -75,6 +92,7 @@
7592
raise Exception("mysterious RADIUS server response to Stop packet")
7693

7794
sys.stdout.write(".\n")
95+
sys.stdout.flush()
7896

7997

8098
print "\nRotating squid log..."

0 commit comments

Comments
 (0)