#!/usr/bin/perl
use DBI;
use conf;
use CGI qw/:standard/;

#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'};
$pkt_log=$ctest->{'mysql_tables.ipkt'};
@data=();
#$uip=$ENV{REMOTE_ADDR};
$current_nip=param("nip");
#$uip="192.168.0.4";
$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'});
$uip=$db_handle->selectrow_array("SELECT inet_ntoa($current_nip);");
#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";
@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;";
$select_string="select prt_dst,proto,count(*) as count from $pkt_log where nip_dst not between 3232235520 and 3232235775 and nip_src=$current_nip group by prt_dst 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 valign=top>$uip</td>";
print"</tr>";
print"<tr>";
print"<td align=center valign=top>";
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 valign=top>";
print"IN";

@data=();
$select_string="";
$select_string="select prt_src,proto,count(*) as count from $pkt_log where nip_src not between 3232235520 and 3232235775 and nip_dst=$current_nip group by prt_src 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";