ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I want to process squid's access.log into a mysql DB so I can use the DB to make proxy's analysis. I think I'll make a cron job that every hour updates the DB so the requests made during the last hour are inserted into the DB.
My feeling is that doing it with a bash script could be a little tedious.
I have read some time ago that PHP can be used as a shell script machine... but in my kubuntu box, I see no php executable... at least, not in the $PATH.
How can I enable it so I can use it?
And can Anybody provide an example of a php-shell script?
You can run php from the command line, you need to look at installing + configuring php-cgi which you may not already have installed. You simply link you php script in cron to run at whatever times you want. There are some Squid logfile analysers available that you might want to look at first rather than doing your own, check out the squid website.
Anyways, this is what I have running to parse DansGuardian logs into a MySQL database. It's set as Squid log file formatting, but you might need to change some of the row names accordingly:
Code:
// Set the location of the DansGuardian access.log file
$fcontents = file ($logfile);
// Clear out all the existing data in the table
$result = @mysql_query("DELETE FROM dglogs");
// Make sure that query was formed correctly, otherwise let user know there's a problem
if (!$result) {
echo("<br>Error performing query: " . mysql_error() . ". Please contact the server administrator with details of your actions.");
exit();
}
// Execute the mySQL query to output the data into the database
mysql_query($result);
echo "<br>Done!";
echo "<br><br>Starting to parse DansGuardian access.log file and adding users.";
echo "<br>Depending on the size of your log file, this could take several minutes...\n\n";
// Run through file and get contents of data ready to output to array
for($i=0; $i<sizeof($fcontents); $i++) {
$line = addslashes(trim($fcontents[$i]));
$line = preg_replace("/\s+/", " ", $line);
// Create new array called '$parsed' based on contents of the DansGuardian access log
//$parsed = list(,$rfc931,,$time,,$authuser,$request,,,$bytes,$status)=explode(" ", $line);
$parsed = list($date,$time,$user,$ip,$request,$result,$status)=explode(" ", $line);
// Create SQL query to insert data from '$parsed' array into mySQL data
$result = @mysql_query("INSERT INTO dglogs VALUES ('','$date','$time','$user','$ip','$request','$result','$status')");
// Make sure that query was formed correctly, otherwise let user know there's a problem
if (!$result) {
echo("Error performing query: " . mysql_error() . ". Please contact the server administrator with details of your actions.");
exit(); }
// Execute the mySQL query to output the data into the database
mysql_query($result);
}
That's set to run each evening at midnight, and then the logs are rotated so tidy things up.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.