
5th February 2007, 17:51
|
|
Senior Member
|
|
Join Date: Jul 2006
Posts: 120
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Piping apache logs to a PHP file in httpd.conf
I would like to pipe apache logs directly to a php file in httpd.conf using CustomLog
Quote:
|
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.
|

5th February 2007, 19:08
|
|
Senior Member
|
|
Join Date: Dec 2006
Location: Hilversum, The Netherlands
Posts: 880
Thanks: 1
Thanked 16 Times in 15 Posts
|
|
I guess you need the php cli version installed; specify "|<path_to_phpcli_bin> full_path_of _your_script" in the CustomLog directive.
|

5th February 2007, 20:23
|
|
Senior Member
|
|
Join Date: Jul 2006
Posts: 120
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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.
Quote:
|
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.
|

5th February 2007, 21:09
|
|
Senior Member
|
|
Join Date: Jul 2006
Posts: 120
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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.
Quote:
|
piped log program '/var/www/web1/log_test.php' failed unexpectedly
|
Any ideas what could cause this error?
|

5th February 2007, 21:33
|
|
Senior Member
|
|
Join Date: Dec 2006
Location: Hilversum, The Netherlands
Posts: 880
Thanks: 1
Thanked 16 Times in 15 Posts
|
|
A quick Google revealed: http://www.php-scripts.com/php_diary/012103.php3 that might be start point.
|

5th February 2007, 21:52
|
|
Senior Member
|
|
Join Date: Jul 2006
Posts: 120
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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.
|

6th February 2007, 14:21
|
|
Super Moderator
|
|
Join Date: Apr 2005
Location: Lüneburg, Germany
Posts: 41,665
Thanks: 1,896
Thanked 2,594 Times in 2,445 Posts
|
|
Quote:
|
Originally Posted by asyadiqin
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?
|

6th February 2007, 14:34
|
|
Senior Member
|
|
Join Date: Jul 2006
Posts: 120
Thanks: 0
Thanked 1 Time in 1 Post
|
|
This is what is in the file log_test.php
PHP Code:
#!/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?
|

7th February 2007, 18:41
|
|
Super Moderator
|
|
Join Date: Apr 2005
Location: Lüneburg, Germany
Posts: 41,665
Thanks: 1,896
Thanked 2,594 Times in 2,445 Posts
|
|
Is it executable?
Do you get syntax errors when you run the script on the shell?
|

7th February 2007, 20:47
|
|
Senior Member
|
|
Join Date: Jul 2006
Posts: 120
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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.
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +2. The time now is 18:40.
|
|
Recent comments
17 hours 19 min ago
22 hours 23 min ago
1 day 2 hours ago
1 day 4 hours ago
1 day 18 hours ago
1 day 18 hours ago
1 day 23 hours ago
2 days 6 hours ago
2 days 7 hours ago
2 days 8 hours ago