1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
function logparse() {
// - DATABASE INFOTMATIONS - //
$squiddbconf="/srv/www/squid/config/squidconf.ini";
switch (is_readable($squiddbconf)) {
case true:
$db_conf = parse_ini_file($squiddbconf);
$db_driver = $db_conf['db_driver'];
$db_host = $db_conf['db_host'];
$db_port = $db_conf['db_port'];
$db_name = $db_conf['db_name'];
$db_user = $db_conf['db_user'];
$db_password = $db_conf['db_password'];
$db_source = "$db_driver".":"."$db_host".";"."$db_name";
$db_table = 'access_log';
$db_insert = "INSERT INTO $db_table (timestamp,elapsed,client,action,size,method,url,ident,hierarchy,content) VALUES (:timestamp,:duration,:client_address,:result_code,:size,:request_method,:url,:ident_lookup,:hierarchy_code,:type)";
break;
case false:
echo "this file is not readable:"."<br>";
break;
}
// - LOGFILE PARSING AND PROCESSING - //
$squidlogfile = "/srv/www/lisquid/content/access.log";
$pattern = "/\s+/";
switch (is_readable($squidlogfile)) {
case true:
try{
$db_object = new PDO($db_source, $db_user, $db_password);
$db_request = $db_object->prepare($db_insert);
}catch(PDOException $error){
echo 'Exception PDO Reçue: ', $error->getMessage(), "\n";
}
$parsedfile = new SplFileObject("$squidlogfile","r");
while(!$parsedfile->eof()){
$lineread = $parsedfile->fgets();
$linesplit = preg_split($pattern,$lineread,10,PREG_SPLIT_NO_EMPTY); //Est-un tableau contenant le fichier parsé ex [0]=>1353315988.513 & [1]=>1057 etc.
list($timestamp,$duration,$client_address,$result_codes,$size,$request_method,$url,$ident_lookup,$hierarchy_code,$type) = $linesplit;
$db_request->bindParam(':timestamp',$timestamp);
$db_request->bindParam(':duration',$duration);
$db_request->bindParam(':client_address',$client_address);
$db_request->bindParam(':result_code',$result_code);
$db_request->bindParam(':size',$size);
$db_request->bindParam(':request_method',$request_method);
$db_request->bindParam(':url',$url);
$db_request->bindParam(':ident_lookup',$ident_lookup);
$db_request->bindParam(':hierarchy_code',$hierarchy_code);
$db_request->bindParam(':type',$type);
$db_request->execute();
var_dump($db_request);
}
break;
case false:
echo "the file $squidlogfile is not readable:"."<br>";
break;
}
} |