#!/usr/bin/perl
use DBI;
use conf;
use Tie::File;

%sym_month=
(
'Jan'=>'01',
'Feb'=>'02',
'Mar'=>'03',
'Apr'=>'04',
'May'=>'05',
'Jun'=>'06',
'Jul'=>'07',
'Aug'=>'08',
'Sep'=>'09',
'Oct'=>'10',
'Nov'=>'11',
'Dec'=>'12');
#conf read
$conf_name="billing.conf";
$ctest=conf->var_read($conf_name);
$main_table=$ctest->{'mysql_tables.data'};
$pkt_log=$ctest->{'mysql_tables.ipkt'};
$ipt_log_path=$ctest->{'environment.ipt_log_path'};
#db_connect
$ipt_log_path="$ipt_log_path.0";
$dbh = 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'});
tie @array, 'Tie::File', $ipt_log_path,mode=>O_RDONLY;
for(@array)
    {
    m/^(\w+)\s+(\d+)\s(\d+:\d+:\d+)/;
    $month=$sym_month{$1};$day="$2";$time="$3";
    $proto=$1 if(m/PROTO=(\w+)/);
    $in=$1 if(m/IN=(\w+)/);
    $out=$1 if(m/OUT=(\w+)/);
    $src=$1 if(m/SRC=(\d+\.\d+\.\d+\.\d+)/);
    $dst=$1 if(m/DST=(\d+\.\d+\.\d+\.\d+)/);
    $spt=0; $spt=$1 if(m/SPT=(\d+)/);
    $dpt=0; $dpt=$1 if(m/DPT=(\d+)/);
    $ttl=0; $ttl=$1 if(m/TTL=(\d+)/);
    $len=0; $len=$1 if(m/LEN=(\d+)/);
    $syn=0; $syn=1 if(m/^.+SYN.+$/);
    $ack=0; $ack=1 if(m/^.+ACK.+$/); 
    $fin=0; $fin=1 if(m/^.+FIN.+$/);
    $rst=0; $rst=1 if(m/^.+RST.+$/);
    $type=99; $type=$1 if(m/TYPE=(\d+)/);
    $code=99; $code=$1 if(m/CODE=(\d+)/);
    $nip_src=$dbh->selectrow_array("select inet_aton('$src')");
    $nip_dst=$dbh->selectrow_array("select inet_aton('$dst')");
$dbh->do("insert into $pkt_log 
(time,proto,int_in,nip_src,prt_src,int_out,nip_dst,prt_dst,len,ttl,syn,ack,fin,rst,type,code)
values
('0000-$month-$day $time','$proto','$in',$nip_src,$spt,'$out',$nip_dst,$dpt,$len,$ttl,$syn,$ack,$fin,$rst,$type,$code)");
    }
$dbh->disconnect;
undef $ctest;
untie @array;