#!/usr/bin/perl
#

require "ctime.pl";
use POSIX;

#Unbuffered output
$| = 1;

# Various what-not that we need to get things done.

# My Stuff
use DBI;
#use DBD::mysql
my $database    = "radius";
my $hostname    = "212.50.53.196";
my $port        = "5432";
my $user        = "postgres";
my $password    = "";
my $table = 'call_history_daily';




sub readdb {
        my @fields = qw( h323connecttime acctsessiontime calledstationid h323remoteaddress NASIPAddress );
        my %results;
        my $fields = join(', ', @fields);

        $dbh = DBI->connect("DBI:Pg:dbname=$database;host=$hostname", "$user", "$password")
                or die "Couldn't connect to database: " . DBI->errstr;

        my $sth = $dbh->prepare("SELECT $fields FROM $table")
                or die "Couldn't prepare statement: " . $dbh->errstr;

        $sth->execute()             # Execute the query
           or die "Couldn't execute statement: " . $sth->errstr;

        @results{@fields} = ();
        $sth->bind_columns(map { \$results{$_} } @fields);

        print "h323connecttime\t\tacctsessiontime\tcalledstationid\t\th323remoteaddress\n";

        while ($sth->fetch()) {
                print "$results{h323connecttime}\t$results{acctsessiontime}\t$results{calledstationid}\t$results{h323remoteaddress}\t$results{NASIPAddress}\n";
        }


        $sth->finish;
        print "\n";

        ### Now, disconnect from the database
        $dbh->disconnect
            or warn "Disconnection failed: $DBI::errstr\n";
}

sub main
{
	&readdb;
}
&main;

