|
3 | 3 | import sys
|
4 | 4 | import argparse
|
5 | 5 | import time
|
| 6 | +import re |
6 | 7 | from subprocess import call
|
7 | 8 | import pyrad.packet
|
8 | 9 | from pyrad.client import Client
|
|
17 | 18 | parser.add_argument('-p', '--radius-acct-port', default='1813')
|
18 | 19 | parser.add_argument('--radius-nasid', default='squid')
|
19 | 20 | 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') |
20 | 25 | args = parser.parse_args()
|
21 | 26 |
|
22 | 27 |
|
|
26 | 31 | sys.stdout.write("Analyzing")
|
27 | 32 | sum_bytes = {}
|
28 | 33 | 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() |
30 | 35 |
|
31 | 36 | # http://wiki.squid-cache.org/Features/LogFormat
|
32 | 37 | _, _, _, _, num_bytes, _, _, rfc931, _, _ = line.split()[:10]
|
|
44 | 49 | dict=Dictionary(sys.path[0] + "/dictionary"))
|
45 | 50 |
|
46 | 51 |
|
| 52 | +if args.exclude_pattern: |
| 53 | + print "Exclusion check has been enabled." |
| 54 | + exclude_pattern = re.compile(args.exclude_pattern) |
| 55 | + |
| 56 | + |
47 | 57 | print "Sending..."
|
48 | 58 | for username, total_bytes in sum_bytes.iteritems():
|
49 | 59 | sys.stdout.write(username + ' ' + str(total_bytes))
|
50 | 60 | 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 |
51 | 67 |
|
52 | 68 | session_id = str(time.time())
|
53 | 69 |
|
|
62 | 78 | raise Exception("mysterious RADIUS server response to Start packet")
|
63 | 79 |
|
64 | 80 | sys.stdout.write('.')
|
| 81 | + sys.stdout.flush() |
65 | 82 |
|
66 | 83 | req = srv.CreateAcctPacket()
|
67 | 84 | req['User-Name'] = username
|
|
75 | 92 | raise Exception("mysterious RADIUS server response to Stop packet")
|
76 | 93 |
|
77 | 94 | sys.stdout.write(".\n")
|
| 95 | + sys.stdout.flush() |
78 | 96 |
|
79 | 97 |
|
80 | 98 | print "\nRotating squid log..."
|
|
0 commit comments