#!/usr/bin/perl -wT
BEGIN {
$|=1;
print "Content-type: text/html\n\n";
use CGI::Carp('fatalsToBrowser');
}
use DBI;
use CGI;
$query = new CGI;
$month = $query->param('m');
$year = $query->param('y');
$table_name = $month."_".$year;
printf "Connecting to the database ............
\n";
($dbhost, $dbname) = qw/xx.xxxxxxx.com.xx xxxxxxxxx/;
($user, $pass) = qw/xxxxxxxx xxxxxx /;
$dbh = DBI->connect("DBI:mysql:hostname=$dbhost:database=$dbname", $user, $pass);
printf "Creating temp table if not exist ............
\n";
$sql = "CREATE TABLE IF NOT EXISTS tmp_table (ClientIP Char(20),Qty INT)";
if (!$dbh->do($sql))
{
printf "Error creating tmp_table table: " . $DBI::errstr;
$dbh->disconnect;
exit;
}
printf "Select from $table_name ............
\n";
$sql = "SELECT ClientIP, count(ClientIP) as Qty FROM " . $table_name;
$sql .= " GROUP BY ClientIP";
$res = $dbh->prepare($sql);
if ($res->execute)
{
while($data = $res->fetchrow_hashref)
{
$hits = int($data->{Qty});
if ($hits > 100)
{
printf "Insert " . $data->{ClientIP} . " - " . $data->{Qty} . " ............
\n";
$sql = "INSERT INTO tmp_table (ClientIP,Qty) values (";
$sql .= "'" . $data->{ClientIP} . "',";
$sql .= "'" . $data->{Qty} . "')";
if (!$dbh->do($sql) )
{
printf "Error adding tmp_table table: $DBI::errstr";
}
}
}
}
else
{
printf "Error selecting from " . $table_name ." table: $DBI::errstr\n";
}
$res->finish;
printf "that's all for now Folks!!
\n";