|
9 | 9 | from pyrad.client import Client
|
10 | 10 | from pyrad.dictionary import Dictionary
|
11 | 11 |
|
| 12 | +try: |
| 13 | + from hurry.filesize import size |
| 14 | +except ImportError: |
| 15 | + print "WARNING: Unable to import hurry.filesize. Data transfer will be " \ |
| 16 | + "displayed in bytes. To fix this, run `pip2 install hurry.filesize`." |
| 17 | + |
12 | 18 |
|
13 | 19 | parser = argparse.ArgumentParser(description='Analyze squid log by user ' \
|
14 | 20 | 'and upload result to RADIUS ' \
|
15 | 21 | 'server.')
|
| 22 | +parser.add_argument('--version', action='version', version='%(prog)s 1.0') |
16 | 23 | parser.add_argument('logfile_path', help='logfile to analyze')
|
17 | 24 | parser.add_argument('radius_server')
|
18 | 25 | parser.add_argument('radius_secret')
|
|
22 | 29 | parser.add_argument('--exclude-pattern', help='do not send to server if ' \
|
23 | 30 | 'username contains this regexp',
|
24 | 31 | default='')
|
| 32 | +parser.add_argument('--dry-run', help='run locally only and never contact the' \ |
| 33 | + 'server', |
| 34 | + action='store_true') |
25 | 35 | parser.add_argument('--no-rotation', help='do not rotate squid log files',
|
26 | 36 | action='store_true')
|
27 | 37 | args = parser.parse_args()
|
|
50 | 60 | sum_bytes[rfc931] = int(num_bytes)
|
51 | 61 |
|
52 | 62 |
|
53 |
| -print "\nSending..." |
| 63 | +print |
| 64 | +print "Sending..." if not args.dry_run else "Stats:" |
54 | 65 | srv = Client(server=args.radius_server, secret=args.radius_secret,
|
55 | 66 | dict=Dictionary(sys.path[0] + "/dictionary"))
|
56 | 67 |
|
|
60 | 71 |
|
61 | 72 | failed_usernames = []
|
62 | 73 | for username, total_bytes in sum_bytes.iteritems():
|
63 |
| - sys.stdout.write(username + ' ' + str(total_bytes)) |
64 |
| - sys.stdout.write('.') |
65 |
| - sys.stdout.flush() |
| 74 | + sys.stdout.write(' ' + username + ' ') |
| 75 | + |
| 76 | + try: |
| 77 | + sys.stdout.write(size(total_bytes)) |
| 78 | + except NameError: |
| 79 | + sys.stdout.write(str(total_bytes)) |
66 | 80 |
|
| 81 | + if args.dry_run: |
| 82 | + sys.stdout.write("\n") |
| 83 | + continue |
| 84 | + |
67 | 85 | if args.exclude_pattern and exclude_pattern.search(username):
|
68 |
| - sys.stdout.write("..skipped!\n") |
| 86 | + sys.stdout.write("...skipped!\n") |
69 | 87 | sys.stdout.flush()
|
70 | 88 | continue
|
71 | 89 |
|
72 | 90 | session_id = str(time.time())
|
73 | 91 |
|
74 | 92 | try:
|
| 93 | + sys.stdout.write('.') |
| 94 | + sys.stdout.flush() |
| 95 | + |
75 | 96 | req = srv.CreateAcctPacket()
|
76 | 97 | req['User-Name'] = username
|
77 | 98 | req['NAS-Identifier'] = args.radius_nasid
|
|
95 | 116 | reply = srv.SendPacket(req)
|
96 | 117 | if not reply.code == pyrad.packet.AccountingResponse:
|
97 | 118 | raise Exception("Unexpected response from RADIUS server")
|
| 119 | + |
| 120 | + sys.stdout.write('.') |
| 121 | + sys.stdout.flush() |
98 | 122 |
|
99 | 123 | except Exception as e:
|
100 | 124 | failed_usernames.append((username, e))
|
101 |
| - sys.stdout.write("..FAILED!\n") |
| 125 | + sys.stdout.write("FAILED!\n") |
102 | 126 | sys.stdout.flush()
|
103 | 127 | continue
|
104 | 128 |
|
105 |
| - sys.stdout.write(".\n") |
106 |
| - sys.stdout.flush() |
| 129 | + sys.stdout.write("\n") |
107 | 130 |
|
108 | 131 |
|
109 | 132 | if not args.no_rotation:
|
|
0 commit comments