Skip to content

Commit 839596e

Browse files
committed
introduce --dry-run arg, closes jiehanzheng#2
1 parent cc61850 commit 839596e

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,18 @@ git clone git://github.com/jiehanzheng/squid2radius.git
2121
sudo pacman -S python2-pip
2222

2323
# install pyrad, command varies if you are on a different OS
24-
sudo pip2 install pyrad
24+
sudo pip2 install pyrad hurry.filesize
2525
```
2626

27+
Upgrading to v1.0
28+
-----------------
29+
30+
### New dependency `hurry.filesize`
31+
32+
Note that an dependency `hurry.filesize` is required since Version 1.0. Run
33+
`sudo pip2 install hurry.filesize` to install it.
34+
35+
2736
Usage
2837
-----
2938

@@ -48,6 +57,10 @@ You should also read [SquidFaq/SquidLogs](http://wiki.squid-cache.org/SquidFaq/S
4857

4958
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.
5059

60+
### --dry-run
61+
62+
If the script is called with this argument, no data will be sent to the server.
63+
5164
### --no-rotation
5265

5366
By default squid2radius calls `squid -k rotate` to make squid rotate your log files right after we are done counting usage data, in order to ensure usage data accuracy by not counting any log lines more than once next time you run it. If this is troublesome in your setup, you can add `--no-rotation` argument to disable this behavior.

squid2radius.py

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,17 @@
99
from pyrad.client import Client
1010
from pyrad.dictionary import Dictionary
1111

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+
1218

1319
parser = argparse.ArgumentParser(description='Analyze squid log by user ' \
1420
'and upload result to RADIUS ' \
1521
'server.')
22+
parser.add_argument('--version', action='version', version='%(prog)s 1.0')
1623
parser.add_argument('logfile_path', help='logfile to analyze')
1724
parser.add_argument('radius_server')
1825
parser.add_argument('radius_secret')
@@ -22,6 +29,9 @@
2229
parser.add_argument('--exclude-pattern', help='do not send to server if ' \
2330
'username contains this regexp',
2431
default='')
32+
parser.add_argument('--dry-run', help='run locally only and never contact the' \
33+
'server',
34+
action='store_true')
2535
parser.add_argument('--no-rotation', help='do not rotate squid log files',
2636
action='store_true')
2737
args = parser.parse_args()
@@ -50,7 +60,8 @@
5060
sum_bytes[rfc931] = int(num_bytes)
5161

5262

53-
print "\nSending..."
63+
print
64+
print "Sending..." if not args.dry_run else "Stats:"
5465
srv = Client(server=args.radius_server, secret=args.radius_secret,
5566
dict=Dictionary(sys.path[0] + "/dictionary"))
5667

@@ -60,18 +71,28 @@
6071

6172
failed_usernames = []
6273
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))
6680

81+
if args.dry_run:
82+
sys.stdout.write("\n")
83+
continue
84+
6785
if args.exclude_pattern and exclude_pattern.search(username):
68-
sys.stdout.write("..skipped!\n")
86+
sys.stdout.write("...skipped!\n")
6987
sys.stdout.flush()
7088
continue
7189

7290
session_id = str(time.time())
7391

7492
try:
93+
sys.stdout.write('.')
94+
sys.stdout.flush()
95+
7596
req = srv.CreateAcctPacket()
7697
req['User-Name'] = username
7798
req['NAS-Identifier'] = args.radius_nasid
@@ -95,15 +116,17 @@
95116
reply = srv.SendPacket(req)
96117
if not reply.code == pyrad.packet.AccountingResponse:
97118
raise Exception("Unexpected response from RADIUS server")
119+
120+
sys.stdout.write('.')
121+
sys.stdout.flush()
98122

99123
except Exception as e:
100124
failed_usernames.append((username, e))
101-
sys.stdout.write("..FAILED!\n")
125+
sys.stdout.write("FAILED!\n")
102126
sys.stdout.flush()
103127
continue
104128

105-
sys.stdout.write(".\n")
106-
sys.stdout.flush()
129+
sys.stdout.write("\n")
107130

108131

109132
if not args.no_rotation:

0 commit comments

Comments
 (0)