|
30 | 30 | logfile = open(args.logfile_path)
|
31 | 31 | print logfile
|
32 | 32 |
|
33 |
| -sys.stdout.write("Analyzing") |
| 33 | +sys.stdout.write("Analyzing.") |
34 | 34 | sum_bytes = {}
|
35 | 35 | 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() |
37 | 37 |
|
38 | 38 | # http://wiki.squid-cache.org/Features/LogFormat
|
39 |
| - _, _, _, _, num_bytes, _, _, rfc931, _, _ = line.split()[:10] |
| 39 | + _, _, _, code_status, num_bytes, _, _, rfc931, _, _ = line.split()[:10] |
40 | 40 |
|
| 41 | + # unauthorized user |
41 | 42 | if rfc931 == '-': continue
|
| 43 | + |
| 44 | + # wrong username and/or password |
| 45 | + if code_status.split('/')[1] == '407': continue |
42 | 46 |
|
43 | 47 | try:
|
44 | 48 | sum_bytes[rfc931] = sum_bytes[rfc931] + int(num_bytes)
|
45 | 49 | except KeyError:
|
46 | 50 | sum_bytes[rfc931] = int(num_bytes)
|
47 | 51 |
|
48 | 52 |
|
49 |
| -print "\nSetting up RADIUS server..." |
| 53 | +print "\nSending..." |
50 | 54 | srv = Client(server=args.radius_server, secret=args.radius_secret,
|
51 | 55 | dict=Dictionary(sys.path[0] + "/dictionary"))
|
52 | 56 |
|
53 |
| - |
54 | 57 | if args.exclude_pattern:
|
55 | 58 | print "Exclusion check has been enabled."
|
56 | 59 | exclude_pattern = re.compile(args.exclude_pattern)
|
57 | 60 |
|
58 |
| - |
59 |
| -print "Sending..." |
| 61 | +failed_usernames = [] |
60 | 62 | for username, total_bytes in sum_bytes.iteritems():
|
61 | 63 | sys.stdout.write(username + ' ' + str(total_bytes))
|
62 | 64 | sys.stdout.write('.')
|
|
75 | 77 | req['Acct-Session-Id'] = session_id
|
76 | 78 | req['Acct-Status-Type'] = 1 # Start
|
77 | 79 |
|
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 |
81 | 89 |
|
82 | 90 | sys.stdout.write('.')
|
83 | 91 | sys.stdout.flush()
|
|
89 | 97 | req['Acct-Status-Type'] = 2 # Stop
|
90 | 98 | req['Acct-Output-Octets'] = total_bytes
|
91 | 99 |
|
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 |
95 | 109 |
|
96 | 110 | sys.stdout.write(".\n")
|
97 | 111 | sys.stdout.flush()
|
98 | 112 |
|
| 113 | + |
99 | 114 | if not args.no_rotation:
|
100 | 115 | print "\nRotating squid log..."
|
101 | 116 | call([args.squid_path, "-k", "rotate"])
|
102 | 117 |
|
| 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