PDA

View Full Version : Piping apache logs to a PHP file in httpd.conf


asyadiqin
5th February 2007, 18:51
I would like to pipe apache logs directly to a php file in httpd.conf using CustomLog


CustomLog "|/path/to/php_script" combined


Can this be done and how do I get the php file to read and parse the apache logs.

I know there is an apache module called mod_log_sql, but to use that module, it require me to recompiled apache, which is something I am not really keen to do at this point of time.

What I actually require is to pipe the logs to a php file, which will then insert the logs into a MySQL table.

Anyone have any idea if there is any such script available. I know that some would be advise me to get a log parser or reader and parse the log files, but I would prefer to do this in httpd.conf using the CustomLog directive.

Thanks.

martinfst
5th February 2007, 20:08
I guess you need the php cli version installed; specify "|<path_to_phpcli_bin> full_path_of _your_script" in the CustomLog directive.

asyadiqin
5th February 2007, 21:23
I'm not sure what you meant by PHP CLI version. I've tested the php script by running it on the command line and it works fine and inserted a empty row into the MySQL table.

However, when I added the following line in httpd.conf, nothing happened, ie. no MySQL record was inserted into the table.


CustomLog "|/var/www/web1/log_test.php" combined


Is there any way you can give me a sample php script that actually can read and parse the logs piped into the php script. Is there any guide as to how php read and parse logs piped to it.

Thanks again.

asyadiqin
5th February 2007, 22:09
I've just looked into the apache log and found the following lines that kept repeating until I stopped apache and removed the CustomLog line I added.


piped log program '/var/www/web1/log_test.php' failed unexpectedly


Any ideas what could cause this error?

martinfst
5th February 2007, 22:33
A quick Google revealed: http://www.php-scripts.com/php_diary/012103.php3 that might be start point.

asyadiqin
5th February 2007, 22:52
Thanks martinfs.

That guide is only if I wish to parse the log files, which is not what I really require. I wanted the log details to be added into the MySQL table in realtime, which is why I wanted to pipe to the php script using CustomLog directive in httpd.conf.

I am tempted to use the Apache mod_log_sql, but I don't really like to recompiled Apache just for that purpose.

Any other ideas? Maybe some Apache and PHP gurus out there can give a tip or two on how this could be done. FYI, I am running Apache 2.2, MySQL 5.0.27 and PHP 5. Thanks again.

falko
6th February 2007, 15:21
I've just looked into the apache log and found the following lines that kept repeating until I stopped apache and removed the CustomLog line I added.



Any ideas what could cause this error?
What's in /var/www/web1/log_test.php? Did you include the path to your PHP interpreter at the beginning (like #!/usr/bin/php)? Is it exectuable? Did you save it with Unix linebreaks?

asyadiqin
6th February 2007, 15:34
This is what is in the file log_test.php


#!/usr/bin/php -q
<?php
include("/var/www/web1/include/ez_sql_core.php");
include("/var/www/web1/include/ez_sql_mysql.php");

$junk = fopen('php://stdin', 'r');

$handle = fopen("/var/www/web1/log/log.txt", "wb");
fputs($handle, $junk);
fclose($handle);

$db = new ezSQL_mysql("sqluser", "sqlpwd", "dbname", "dbhost");

$sql = "INSERT INTO logfile(id, junk, dateAdded) VALUES (NULL, '".$junk."', NOW())";
$db->query($sql);
?>


Any ideas what I did wrong?

falko
7th February 2007, 19:41
Is it executable?
Do you get syntax errors when you run the script on the shell?

asyadiqin
7th February 2007, 21:47
No,

I ran the file on the commandline and it does everything, ie. create the log file and insert a row in the table.

So I have no idea why it won't work in the httpd.conf file.

falko
8th February 2007, 21:47
Is it executable by your Apache user as well?
Please switch to your Apache user:
su -m apache_user
and try to run the script again.

asyadiqin
8th February 2007, 23:46
Hi again,

The file permission was set to 777 when I first had problem as I did suspect that it might be a permission issue. I've even change the owner to apache.

Any other ideas?

falko
9th February 2007, 16:44
Instead of
CustomLog "|/var/www/web1/log_test.php" combined
you could try
CustomLog "|/usr/bin/php /var/www/web1/log_test.php" combined
and remove the #!/usr/bin/php -q line from your script.