#!/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'};
$users_log=$ctest->{'mysql_tables.traf'};
$hourly_sum=$ctest->{'mysql_tables.hour'};
$script_log=$ctest->{'mysql_tables.slow'};
$edit_log=$ctest->{'mysql_tables.edit'};

@data=();
@flags_array=();
@counts_array=();
@in=(tcp_in,udp_in,icmp_in,rest_in);
@out=(tcp_out,udp_out,icmp_out,rest_out);
@counts=(tcp_in_count,udp_in_count,icmp_in_count,rest_in_count,tcp_out_count,udp_out_count,icmp_out_count,rest_out_count);
@flags=(log_flag,add_flag,del_flag);
$select_string=join(',',sort {$rows2select{$a}<=>$rows2select{$b}} keys %rows2select);
$uip=$ENV{REMOTE_ADDR};
#HTML header
print"Content-type: text/html;charset=cp-1251;\n\n";
print"<html><head><title>Tech stat</title>";
print"<style>";
print"body,input,td{font-family: monospace}";
print"hr {width: 80%;}";
print"td.primaryleft{border-bottom: 1px solid #ffffff; border-left: 1px solid #ffffff; border-right: 1px solid #ffffff; color:#000000; padding: 2px;}";
print"td.primary{border-bottom: 1px solid #ffffff; border-right: 1px solid #ffffff; padding: 2px; background-color: #ff00ff;}";
print"td.primary_spacer{border-bottom: 1px solid #ffffff; padding: 2px; color: #000000;}";
print"td.secondaryleft{border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; color: #ffffff; padding: 2px;}";
print"td.secondary{border-bottom: 1px solid #000000; border-right: 1px solid #000000; color: #ffffff; padding: 2px; background-color: purple;}";
print"td.secondary_spacer{border-bottom: 1px solid #000000; color: #ffffff; padding: 2px; color: #000000;}";
print"td.primary{border-bottom: 1px solid #ffffff; border-right: 1px solid #ffffff; padding: 2px; background-color: #ff00ff;}";
print"table.main {border-collapse: collapse; border-top: 1px solid #ffffff; border-right: none; border-left: none; padding: 0; spacing: 0;}";
print"td.firstleft{border-bottom: 1px solid #ffffff; border-left: 1px solid #ffffff; border-right: 1px solid #ffffff; color:#000000; padding: 2px;}";
print"td.first{border-bottom: 1px solid #ffffff; border-right: 1px solid #ffffff; padding: 2px;}";
print"td.secondleft{border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; color:#000000; padding: 2px;}";
print"td.second{border-bottom: 1px solid #000000; border-right: 1px solid #000000; color: #ffffff; padding: 2px;}";
print"</style>";
print"</head>\n";
print"<body bgcolor=#888888>\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'});
$timestamp=$db_handle->selectrow_array("SELECT now();");
$current_nip=$db_handle->selectrow_array("SELECT inet_aton('$uip');");
    print"<table class=main border=1>\n";
	@data=();
	$select_string="show tables;";
    $sth=$db_handle->prepare("$select_string");
    $sth->execute();
    while(@data=$sth->fetchrow_array())
    {
    print"<tr align>";
    $i=0;
        while(defined $data[$i])
	{
	    print"<td>$data[$i]</td>";
	    $i++;
	}
	print"</tr>\n";
    }
    print"</table>\n";
    $sth->finish();
    print"<hr><table class=main border=1>\n";
    @data=();
    $select_string="show table status;";
    $sth=$db_handle->prepare("$select_string");
    $sth->execute();
    while(@data=$sth->fetchrow_array())
	{
        print"<tr>";
	$i=0;
        while($i<20)
	{
	    print"<td>$data[$i]</td>";
	    $i++;
	}
	print"</tr>\n";
	}
    print"</table>\n";
    $sth->finish();
print"<hr>";
print"Current total users:",$db_handle->selectrow_array("SELECT count(*) from $main_table;"),"<br>";
print"<hr>";
print"Log_user entries:",$db_handle->selectrow_array("SELECT count(*) from $users_log;"),"<br>";
print"First log_user entry:",$db_handle->selectrow_array("SELECT time from $users_log order by time asc limit 1;"),"<br>";
print"Last log_user entry:",$db_handle->selectrow_array("SELECT time from $users_log order by time desc limit 1;"),"<br>";
print"<hr>";
print"Log_hour entries:",$db_handle->selectrow_array("SELECT count(*) from $hourly_sum;"),"<br>";
print"First hourly_sum entry:",$db_handle->selectrow_array("SELECT time from $hourly_sum order by time asc limit 1;"),"<br>";
print"Last hourly_sum entry:",$db_handle->selectrow_array("SELECT time from $hourly_sum order by time desc limit 1;"),"<br>";
print"<hr>";
print"Log_slow entries:",$db_handle->selectrow_array("SELECT count(*) from $script_log;"),"<br>";
print"<table border=1><tr><td>";
print"dTime average:",$db_handle->selectrow_array("select avg((time_to_sec(time_stop)-time_to_sec(time_start))) from $script_log where code='c';"),"<br>";
print"dTime last:",$db_handle->selectrow_array("select (time_to_sec(time_stop)-time_to_sec(time_start)) from $script_log where code='c' limit 1;"),"<br>";
print"c total:",$db_handle->selectrow_array("select count(*) from $script_log where code='c';"),"<br>";
print"</td><td>\n";
print"dTime average:",$db_handle->selectrow_array("select avg((time_to_sec(time_stop)-time_to_sec(time_start))) from $script_log where code='h';"),"<br>";
print"dTime last:",$db_handle->selectrow_array("select (time_to_sec(time_stop)-time_to_sec(time_start)) from $script_log where code='h' limit 1;"),"<br>";
print"h total:",$db_handle->selectrow_array("select count(*) from $script_log where code='h';"),"<br>";
print"</td><td>\n";
print"dTime average:",$db_handle->selectrow_array("select avg((time_to_sec(time_stop)-time_to_sec(time_start))) from $script_log where code='u';"),"<br>";
print"dTime last:",$db_handle->selectrow_array("select (time_to_sec(time_stop)-time_to_sec(time_start)) from $script_log where code='u' limit 1;"),"<br>";
print"u total:",$db_handle->selectrow_array("select count(*) from $script_log where code='u';"),"<br>";
print"</td></tr></table>\n";
print"First script_log entry:",$db_handle->selectrow_array("SELECT time_start from $script_log order by time_start asc limit 1;"),"<br>";
print"Last script_log entry:",$db_handle->selectrow_array("SELECT time_stop from $script_log order by time_stop desc limit 1;"),"<br>";
print"<hr>";
print"Log_edit entries:",$db_handle->selectrow_array("SELECT count(*) from $edit_log;"),"<br>";
print"First edit_log entry:",$db_handle->selectrow_array("SELECT time from $edit_log order by time asc limit 1;"),"<br>";
print"Last edit_log entry:",$db_handle->selectrow_array("SELECT time from $edit_log order by time desc limit 1;"),"<br>";
print"<hr>";

@tdclass=("primary","secondary");
%r_month=(
'01'=>"I",
'02'=>"II",
'03'=>"III",
'04'=>"IV",
'05'=>"V",
'06'=>"VI",
'07'=>"VII",
'08'=>"VIII",
'09'=>"IX",
'10'=>"X",
'11'=>"XI",
'12'=>"XII");
    $day=0;
    $state=0;
    print"<table class=main>\n";
    @data=();
    $select_string="select * from $edit_log order by time desc limit 17520;";
    $sth=$db_handle->prepare("$select_string");
    $sth->execute();
    while(@data=$sth->fetchrow_array())
    {
	$daychange=0;
        $i=0;
        @datetime=split(' ',$data[0]);
        @date=split('-',$datetime[0]);
        if($date[2]!=$day) {$state=1-$state;$day=$date[2];$daychange=1;}
	if($daychange eq 1) {print"<tr><td colspan=100 align=center class=\"$tdclass[$state]\_spacer\">\n$date[2]\.$r_month{$date[1]}\.$date[0]</td>\n</tr>\n";}
	print"<tr align=right>";
        while(defined $data[$i])
	{
	    if($i==0) {print"<td align=right class=$tdclass[$state]left>$datetime[1]</td>";}
	    else {print"<td class=$tdclass[$state]>$data[$i]</td>";}
	    $i++;
	}
	print"</tr>\n";
    }
    print"</table>\n";
$sth->finish();
$db_handle->do("INSERT INTO $script_log SET time_start=\"$timestamp\",time_stop=CURRENT_TIMESTAMP,users=(SELECT count(*) from $main_table),code='t';");
$db_handle->disconnect;
print"<hr>$uip<br><br>\n";
print"</center>\n";
print"</body>\n";
print"</html>\n";
