#!/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'};
$css_path=$ctest->{'environment.css_path'};

@data=();
@letter=(c,h,p,u,a,t,i);
%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");
$uip=$ENV{REMOTE_ADDR};
#HTML header
print"Content-type: text/html;charset=cp-1251;\n\n";
print"<html><head><title>techstat</TITLE><link href=\"$css_path\" rel=\"stylesheet\" type=\"text/css\">\n</head>\n";
print"<body>\n";
print"<TABLE width=100% height=100%><TR><TD align=center valign=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"log_slow advanced\n";
print"<table width=75%><tr>";
for $temp (@letter)
    {
    $last=$db_handle->selectrow_array("select timediff(time_stop,time_start) from $script_log where code='$temp' order by time_start desc limit 1;");
    @sec=split(':',$last);
    if($sec[2]>5){$class="one";}else{$class="two";}
    $time=$db_handle->selectrow_array("select avg(timediff(time_stop,time_start)) from $script_log where code='$temp';");
    $time=sprintf("%.2f", $time);
    print"<td class=$class>";
    print"$temp total: ",$db_handle->selectrow_array("select count(*) from $script_log where code='$temp';"),"<br>";
    print"avg: $time<br>";
    print"last: $sec[2]";
    print"</td>";
    }
print"</tr></table>\n";

print"log_edit<br>";
print"<textarea>\n";
@data=();
$select_string="select * from $edit_log order by time desc limit 17520;";
$sth=$db_handle->prepare("$select_string");
$sth->execute();
$day=0;
    while(@data=$sth->fetchrow_array())
    {
    $i=0;
    @datetime=split(' ',$data[0]);
    @date=split('-',$datetime[0]);
    while(defined $data[$i]) {if($i==0) {print"$date[2]\.$r_month{$date[1]}\.$date[0]...$datetime[1]\n";} else {print"$data[$i].";} $i++;}
    print"\n";
    }
print"</textarea><br>\n";
$sth->finish();

@data=();
$select_string="show table status;";
print"show table status\n";
print"<table width=75%>\n";
$sth=$db_handle->prepare("$select_string");
$sth->execute();
while(@data=$sth->fetchrow_array())
    {
    print"<tr>";
    $i=0;
    while($i<15)
    {
if($data[6]>1024*1024*100){$class="one";}else{$class="two";}
    if($i==0) {print"<td class=one>$data[$i]</td>";}
    elsif($i==4) {$result=$ctest->readable($data[$i]);print"<td class=$class>$data[$i]</td><td class=$class>($result)</td>";}
    elsif($i==6) {$result=$ctest->mb($data[$i]);print"<td class=$class>$data[$i]</td><td class=$class>($result Mb)</td>";}
#    elsif($i==8) {print"<td class=two>$data[$i]</td>";}
    elsif($i==11) {print"<td class=two>$data[$i]</td>";}
    elsif($i==12) {print"<td class=two>$data[$i]</td>";}
    $i++;
    }
    print"</tr>\n";
    }
print"</table>\n";
$sth->finish();

print"server time: ",$db_handle->selectrow_array("SELECT now();"),"<br>\n";
$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"</TD></TR></TABLE>\n";
print"</body></html>\n";