#!/usr/bin/perl use strict; ## ## This is a perl replacement for the linux 'rtacct' tool, ## showing how you can easily script retriving accounting information. ## ## Moo, (c) 2002 Theo Zourzouvillys, . ## Do what you want with it, it's hardly original or complicated! ;) ## my $Data = undef; open(RT, "/proc/net/rt_acct") || die "Can't open '/proc/net/rt_acct': $!\n"; $Data .= $_ while (); close(RT); my $Tables = LoadTableNames(); print "Realm BytesTo PktsTo BytesFrom PktsFrom \n"; for (my $i = 0 ; $i < 256 ; $i++) { my (@int) = (); $int[$_] = unpack('L', substr($Data, ($i * 16) + ($_ * 4), 4)) for (0..4); printf("%-10s %-10u %-10u %-10u %-10u \n", $Tables->[$i], $int[0], $int[1], $int[2], $int[3]) if ($int[0] || $int[1] || $int[2] || $int[3]); } sub LoadTableNames() { my @Table = (); open(TABLE, "/etc/iproute2/rt_realms") || die "Can't open '/etc/iproute2/rt_realms': $!\n"; while () { $Table[$1] = $2 if (/^\s*(\d+)\s+(\S+)/) } close(TABLE); return \@Table; }