[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: TOR traffic measurement with iptables



* Julius Plenz (tor-or-talk@xxxxxxxxx) wrote:
> Hi, Marcel!
> 
> * Marcel <u-281@xxxxxxx> [2006-03-24 09:58]:
> > > [iptables TOR traffic accounting]
> 
> > I gave up on this since they are so many servers using custom tor
> > ports so results are unreliable.  I choose to measure traffic
> > directly from /var/lib/tor/bw_accounting.  I already made a pearl
> > script doing this if you're interested (uses RRD too).
> 
> Cool, I didn't know this file existed. Looks like it gets updated
> every 60 seconds, so it fits my needs.
> 
> It'd be nice, too, if I could take a look at your Perl Script.

Sure.  I use 2 tor, 1 client ($c) and a server ($s).

Here we go.

#!/usr/bin/perl

use RRDs;

my $rrd = '/var/lib/rrd';
my $img = '/var/www/html/rrdtool';
my $c = '/var/lib/torclient/bw_accounting';
my $s = '/var/lib/tor/bw_accounting';

&ProcessInterface("ctor", "client tor ", "$c");
&ProcessInterface("stor", "server tor ", "$s");

sub ProcessInterface
{
        my $in = /bin/cat "$_[2]" | ( /usr/bin/line >/dev/null ; \
/usr/bin/line >/dev/null; /usr/bin/line >/dev/null; /usr/bin/line );
        my $out = /bin/cat "$_[2]" | ( /usr/bin/line >/dev/null ; \
/usr/bin/line >/dev/null ; /usr/bin/line >/dev/null; /usr/bin/line \
>/dev/null; /usr/bin/line );

        if ($in == "") {$in = 0;}
        if ($out == "") {$out = 0;}

        # remove eol chars
        chomp($in);
        chomp($out);


        print "$_[1] traffic in, out: $in, $out\n";

        # if rrdtool database doesn't exist, create it
        if (! -e "$rrd/$_[0].rrd")
        {
                print "creating rrd database for $_[0] interface...\n";
                RRDs::create "$rrd/$_[0].rrd",
                        "-s 300",
                        "DS:in:DERIVE:600:0:12500000",
                        "DS:out:DERIVE:600:0:12500000",
                        "RRA:AVERAGE:0.5:1:576",
                        "RRA:AVERAGE:0.5:6:672",
                        "RRA:AVERAGE:0.5:24:732",
                        "RRA:AVERAGE:0.5:144:1460";
        }

        # insert values into rrd
        RRDs::update "$rrd/$_[0].rrd",
                "-t", "in:out",
                "N:$in:$out";

        # create traffic graphs
        &CreateGraph($_[0], "day", $_[1]);
        &CreateGraph($_[0], "week", $_[1]);
        &CreateGraph($_[0], "month", $_[1]);
        &CreateGraph($_[0], "year", $_[1]);
}

sub CreateGraph
{
        RRDs::graph "$img/$_[0]-$_[1].png",
                "-s -1$_[1]",
                "-t traffic for $_[2]",
                "--lazy",
                "-h", "80", "-w", "600",
                "-l 0",
                "-a", "PNG",
                "-v bytes/sec",
                "DEF:in=$rrd/$_[0].rrd:in:AVERAGE",
                "DEF:out=$rrd/$_[0].rrd:out:AVERAGE",
                "CDEF:out_neg=out,-1,*",
                "AREA:in#32CD32:Incoming",
                "LINE1:in#336600",
                "GPRINT:in:MAX:  Max\\: %5.1lf %s",
                "GPRINT:in:AVERAGE: Avg\\: %5.1lf %S",
                "GPRINT:in:LAST: Current\\: %5.1lf %Sbytes/sec\\n",
                "AREA:out_neg#4169E1:Outgoing",
                "LINE1:out_neg#0033CC",
                "GPRINT:out:MAX:  Max\\: %5.1lf %S",
                "GPRINT:out:AVERAGE: Avg\\: %5.1lf %S",
                "GPRINT:out:LAST: Current\\: %5.1lf %Sbytes/sec",
                "HRULE:0#000000";
        if ($ERROR = RRDs::error) { print "$0: unable to generate $_[0] \
$_[1] traffic graph: $ERROR\n"; }
}

> 
> Julius