ralijani has asked for the wisdom of the Perl Monks concerning the following question:

is there any perl script or software for squid billing i want to restrict my user by Traffic or time for example : they can use internet 10 hours per week or they can use internet 100 MB in week

Replies are listed 'Best First'.
Re: squid-billing
by tachyon (Chancellor) on Oct 27, 2004 at 12:11 UTC

    These are different issues. The time online is usually monitored by your login (radius) server. The bandwidth is available from you squid logs, but need to be mapped back from IP to username (once again your radius server, complicated by dynamic IPs).

    perl -ne '($ip,$bytes)=(split)[0,-1];$h{$ip}+=$bytes;END{print "$_\t$h +{$_}\n" for keys %h}' access_log*
    will give you the total bandwidth used by each IP for your entire log rotation period, so it is not like the data is hard to get. You can estimate the time online from the squid logs but you have to kludge and assume that activity within X time units represents being online for that period. Most people are more interested in the fact that someone is tying up one of the modem pool rather than activity per se when it comes to hours.

    cheers

    tachyon

Re: squid-billing
by zigdon (Deacon) on Oct 27, 2004 at 12:43 UTC

    Sounds like you want to parse the squid log, keep track of which IPs are hitting it where, and how much data they're getting. Of course, you'll have to keep track of how big the items in the cache are, since squid doesn't report file size for cache hits. But it does when it loads a file into the cache, so you should be able to store it in a hash of some sort.

    Hope this sends you in the right direction!

    -- zigdon

Re: squid-billing
by hsinclai (Deacon) on Oct 27, 2004 at 14:13 UTC
    Billing and restricting traffic are 2 different things..

    You can do a simple time acl in squid (right from the docs):
    acl USER1 proxy_auth Dick acl USER2 proxy_auth Jane acl DAY time 06:00-18:00 http_access allow USER1 DAY http_access deny USER1 http_access allow USER2 !DAY http_access deny USER2
    or
    acl FOO src 10.1.2.3 10.1.2.4 acl WORKING time MTWHF 08:30-17:30 http_access allow FOO WORKING http_access deny FOO

    but it's not too flexible , so for controlling access based on units of time, may I suggest squidGuard.. you can install it without interfering with a running squid too much - it is an add-on program for squid. squidGuard has exactly what you want if you need to control end user access based on time of day, hours per week/day. It will allow you to write rules for ACLs which you create, then rewrite URLs, redirect to a CGI, or silently drop, based on these rules - and you can create if/then/else for actions based on being "within" or "outside" of your time space ACL . All of it is detailed in the documentation, and the contrib section contains some handy Perl examples too - so squidGuard might be a fast solution to most of your problem.

    If you really need to do billing, it'll be more work, not to mention hitting issues as pointed out by zigdon, with regard to cached content. tachyon's one-liner is awesome for getting the bytes used out of the squid log ( actually my 2.5.STABLE3 out of the box logs had a different field layout requiring minor changes to get the output ) but you'll have to take action based on that, and reading logs is always after the fact.