DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Traffic Meter By Xorkrus :)

10.07.2008
| 2132 views |
  • submit to reddit
        // harsh but worker
#!/usr/bin/perl
use DBD::mysql;
use DBI;
$nowdate = `date +'%Y%m%d'`;chop($nowdate);
$host = ('localhost');
$dbase = ('tmbx');
$user = ('tmbx');
$pass = ('tmbx');
$tzinb = `cat /proc/net/dev | grep ppp0 | awk '{printf \$2}'`;
$tzoutb = `cat /proc/net/dev | grep ppp0 | awk '{printf \$10}'`;
$trc = DBI->trace(0);
        my $dbh = DBI->connect("DBI:mysql:$dbase;$host", "$user", "$pass",{'RaiseError' => 1});
        $lsn = $dbh->prepare("SELECT MAX(sn) FROM session") or die print "$DBI::errstr\n";
        $lsn->execute();
        my $ref = $lsn->fetchrow_hashref();
        $mlsn = $ref->{'MAX(sn)'};
        $lsn->finish();
        my $oth = $dbh->prepare("SELECT * FROM session WHERE sn='$mlsn'") or die print "$DBI::errstr\n";
        $oth->execute();
        my $refa = $oth->fetchrow_hashref();
        $tinb = $refa->{'inb'};
        $toutb = $refa->{'outb'};
        $tdate = $refa->{'date'};
        $oth->finish();
        $nsn = $mlsn+1;
        $tndate = `echo $tdate | sed -e 's/-//'|sed -e 's/-//'`;chop($tndate);
if ($tzinb > $tinb && $tndate < $nowdate) {
        $nzinb = $tzinb-$tinb;
        $nzoutb = $tzoutb-$toutb;
        my $sth = $dbh->prepare("INSERT INTO session (date, sn, inb, outb) VALUES ('$nowdate', '$nsn', '$nzinb', '$nzoutb')") or die print "$DBI::errstr\n";
        &DayRef;
        $sth->execute();
        }
if ($tzinb > $tinb && $tndate == $nowdate) {
        my $sth = $dbh->prepare("UPDATE session SET date='$nowdate', sn='$mlsn', inb='$tzinb', outb='$tzoutb' WHERE sn='$mlsn';") or die print "$DBI::errstr\n";
        $sth->execute();
        }
if ($tzinb < $tinb) {
        my $sth = $dbh->prepare("INSERT INTO session (date, sn, inb, outb) VALUES ('$nowdate', '$nsn', '$tzinb', '$tzoutb');") or die print "$DBI::errstr\n";
        $sth->execute();
        }
sub DayRef {
        $ddate=$nowdate-1;
        my $ath = $dbh->prepare("SELECT SUM(inb), SUM(outb) FROM session WHERE date='$ddate'") or die print "$DBI::errstr\n";
        $ath->execute();
        my $aref = $ath->fetchrow_hashref();
        $mtinb = $aref->{'SUM(inb)'};
        $mtoutb = $aref->{'SUM(outb)'};
        $ath->finish();
        my $ath2 = $dbh->prepare("INSERT INTO traffic (date,inb,outb) VALUES ('$ddate','$mtinb','$mtoutb')") or die print "$DBI::errstr\n";
        $ath2->execute();
        $ath2->finish();}
        $dbh->disconnect();