Skip to content

Commit 256d7a6

Browse files
committed
better error handling, ignores 407 lines
1 parent 5a18a6c commit 256d7a6

File tree

1 file changed

+35
-13
lines changed

1 file changed

+35
-13
lines changed

squid2radius.py

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,33 +30,35 @@
3030
logfile = open(args.logfile_path)
3131
print logfile
3232

33-
sys.stdout.write("Analyzing")
33+
sys.stdout.write("Analyzing.")
3434
sum_bytes = {}
3535
for i, line in enumerate(logfile):
36-
if i % 1000 == 0: sys.stdout.write('.'); sys.stdout.flush()
36+
if i % 10000 == 0: sys.stdout.write('.'); sys.stdout.flush()
3737

3838
# http://wiki.squid-cache.org/Features/LogFormat
39-
_, _, _, _, num_bytes, _, _, rfc931, _, _ = line.split()[:10]
39+
_, _, _, code_status, num_bytes, _, _, rfc931, _, _ = line.split()[:10]
4040

41+
# unauthorized user
4142
if rfc931 == '-': continue
43+
44+
# wrong username and/or password
45+
if code_status.split('/')[1] == '407': continue
4246

4347
try:
4448
sum_bytes[rfc931] = sum_bytes[rfc931] + int(num_bytes)
4549
except KeyError:
4650
sum_bytes[rfc931] = int(num_bytes)
4751

4852

49-
print "\nSetting up RADIUS server..."
53+
print "\nSending..."
5054
srv = Client(server=args.radius_server, secret=args.radius_secret,
5155
dict=Dictionary(sys.path[0] + "/dictionary"))
5256

53-
5457
if args.exclude_pattern:
5558
print "Exclusion check has been enabled."
5659
exclude_pattern = re.compile(args.exclude_pattern)
5760

58-
59-
print "Sending..."
61+
failed_usernames = []
6062
for username, total_bytes in sum_bytes.iteritems():
6163
sys.stdout.write(username + ' ' + str(total_bytes))
6264
sys.stdout.write('.')
@@ -75,9 +77,15 @@
7577
req['Acct-Session-Id'] = session_id
7678
req['Acct-Status-Type'] = 1 # Start
7779

78-
reply = srv.SendPacket(req)
79-
if not reply.code == pyrad.packet.AccountingResponse:
80-
raise Exception("mysterious RADIUS server response to Start packet")
80+
try:
81+
reply = srv.SendPacket(req)
82+
if not reply.code == pyrad.packet.AccountingResponse:
83+
raise Exception("Unexpected response from RADIUS server")
84+
except Exception as e:
85+
failed_usernames.append((username, e))
86+
sys.stdout.write("..FAILED!\n")
87+
sys.stdout.flush()
88+
continue
8189

8290
sys.stdout.write('.')
8391
sys.stdout.flush()
@@ -89,14 +97,28 @@
8997
req['Acct-Status-Type'] = 2 # Stop
9098
req['Acct-Output-Octets'] = total_bytes
9199

92-
reply = srv.SendPacket(req)
93-
if not reply.code == pyrad.packet.AccountingResponse:
94-
raise Exception("mysterious RADIUS server response to Stop packet")
100+
try:
101+
reply = srv.SendPacket(req)
102+
if not reply.code == pyrad.packet.AccountingResponse:
103+
raise Exception("Unexpected response from RADIUS server")
104+
except Exception as e:
105+
failed_usernames.append((username, e))
106+
sys.stdout.write("..FAILED!\n")
107+
sys.stdout.flush()
108+
continue
95109

96110
sys.stdout.write(".\n")
97111
sys.stdout.flush()
98112

113+
99114
if not args.no_rotation:
100115
print "\nRotating squid log..."
101116
call([args.squid_path, "-k", "rotate"])
102117

118+
119+
if failed_usernames:
120+
raise Exception("Unable to send stats for the following user(s):\n "
121+
+ "\n ".join(fu[0]
122+
+ ' (' + fu[1].__class__.__name__ + ': '
123+
+ str(fu[1]) + ')'
124+
for fu in failed_usernames))

0 commit comments

Comments
 (0)