#!/usr/bin/perl
use DBI;
use conf;

#conf open_read
$conf_name="billing.conf";
$ctest=conf->var_read($conf_name);
$main_table=$ctest->{'mysql_tables.data'};
$css_path=$ctest->{'environment.css_path'};
@data=();
$uip=$ENV{REMOTE_ADDR};
#$uip="192.168.0.180";
#HTML header
print"Content-type: text/html;charset=cp-1251;\n\n";
print"<html><head><title>$uip user stat</title>\n";
print"<link href=\"$css_path\" rel=\"stylesheet\" type=\"text/css\">\n";
print"</head>\n";
print"<body>\n";
print"<center>\n";
$db_handle=DBI->connect("DBI:mysql:database=$ctest->{'mysql_connect.db_db'};host=$ctest->{'mysql_connect.db_host'}",$ctest->{'mysql_connect.db_user'},$ctest->{'mysql_connect.db_password'});
$current_nip=$db_handle->selectrow_array("SELECT inet_aton('$uip');");
@data=();
$select_string="select proto,inet_ntoa(nip_dst) as ip,prt_dst,count(*) as count,sum(len) from pkt_log where nip_dst not between 3232235520 and 3232235775 and nip_src=$current_nip group by ip order by count desc limit 50;";
$sth=$db_handle->prepare("$select_string");
$sth->execute();

print"<table border=1>\n";
print"<tr>";
print"<td colspan=2 align=center>$uip</td>";
print"</tr>";
print"<tr>";
print"<td align=center>";
print"OUT";
print"<table border=1>\n";
while(@data=$sth->fetchrow_array())
    {
    print"<tr>";
    $i=0;
    while(defined $data[$i]) {print"<td class=\"two\">$data[$i]</td>";$i++;}
    print"</tr>\n";
    @data=();
    }
print"</table>\n";

print"</td>";
print"<td align=center>";
print"IN";

@data=();
$select_string="";
$select_string="select proto,inet_ntoa(nip_src) as ip,prt_src,count(*) as count,sum(len) from pkt_log where nip_src not between 3232235520 and 3232235775 and nip_dst=$current_nip group by ip order by count desc limit 50;";
$sth=$db_handle->prepare("$select_string");
$sth->execute();
print"<table border=1>\n";
while(@data=$sth->fetchrow_array())
    {
    print"<tr>";
    $i=0;
    while(defined $data[$i]) {print"<td class=\"one\">$data[$i]</td>";$i++;}
    print"</tr>\n";
    @data=();
    }
print"</table>\n";
print"</td>";
print"</tr>";
print"</table>\n";

$db_handle->disconnect;
print"</center>\n";
print"</body>\n";
print"</html>\n";