Go Back   HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials > Linux Forums > Server Operation

Do you like HowtoForge? Please consider supporting us by becoming a subscriber.
Reply
 
Thread Tools Display Modes
  #1  
Old 5th February 2007, 17:51
asyadiqin asyadiqin is offline
Senior Member
 
Join Date: Jul 2006
Posts: 120
Thanks: 0
Thanked 1 Time in 1 Post
Default 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.
Reply With Quote
Sponsored Links
  #2  
Old 5th February 2007, 19:08
martinfst martinfst is offline
Senior Member
 
Join Date: Dec 2006
Location: Hilversum, The Netherlands
Posts: 880
Thanks: 1
Thanked 18 Times in 17 Posts
Send a message via MSN to martinfst Send a message via Skype™ to martinfst
Default

I guess you need the php cli version installed; specify "|<path_to_phpcli_bin> full_path_of _your_script" in the CustomLog directive.
Reply With Quote
  #3  
Old 5th February 2007, 20:23
asyadiqin asyadiqin is offline
Senior Member
 
Join Date: Jul 2006
Posts: 120
Thanks: 0
Thanked 1 Time in 1 Post
Default

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.
Reply With Quote
  #4  
Old 5th February 2007, 21:09
asyadiqin asyadiqin is offline
Senior Member
 
Join Date: Jul 2006
Posts: 120
Thanks: 0
Thanked 1 Time in 1 Post
Default

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?
Reply With Quote
  #5  
Old 5th February 2007, 21:33
martinfst martinfst is offline
Senior Member
 
Join Date: Dec 2006
Location: Hilversum, The Netherlands
Posts: 880
Thanks: 1
Thanked 18 Times in 17 Posts
Send a message via MSN to martinfst Send a message via Skype™ to martinfst
Default

A quick Google revealed: http://www.php-scripts.com/php_diary/012103.php3 that might be start point.
Reply With Quote
  #6  
Old 5th February 2007, 21:52
asyadiqin asyadiqin is offline
Senior Member
 
Join Date: Jul 2006
Posts: 120
Thanks: 0
Thanked 1 Time in 1 Post
Default

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.
Reply With Quote
  #7  
Old 6th February 2007, 14:21
falko falko is offline
Super Moderator
 
Join Date: Apr 2005
Location: Lneburg, Germany
Posts: 41,711
Thanks: 1,899
Thanked 2,702 Times in 2,545 Posts
Default

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?
__________________
Falko
--
Download the ISPConfig 3 Manual! | Check out the ISPConfig 3 Billing Module!

FB: http://www.facebook.com/howtoforge

nginx-Webhosting: Timme Hosting | Follow me on:
Reply With Quote
  #8  
Old 6th February 2007, 14:34
asyadiqin asyadiqin is offline
Senior Member
 
Join Date: Jul 2006
Posts: 120
Thanks: 0
Thanked 1 Time in 1 Post
Default

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?
Reply With Quote
  #9  
Old 7th February 2007, 18:41
falko falko is offline
Super Moderator
 
Join Date: Apr 2005
Location: Lneburg, Germany
Posts: 41,711
Thanks: 1,899
Thanked 2,702 Times in 2,545 Posts
Default

Is it executable?
Do you get syntax errors when you run the script on the shell?
__________________
Falko
--
Download the ISPConfig 3 Manual! | Check out the ISPConfig 3 Billing Module!

FB: http://www.facebook.com/howtoforge

nginx-Webhosting: Timme Hosting | Follow me on:
Reply With Quote
  #10  
Old 7th February 2007, 20:47
asyadiqin asyadiqin is offline
Senior Member
 
Join Date: Jul 2006
Posts: 120
Thanks: 0
Thanked 1 Time in 1 Post
 
Default

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.
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Freebsd 6.1 support misterm Installation/Configuration 10 9th April 2009 09:29
Systemimager (rsync) doesn't copy all comedit HOWTO-Related Questions 11 19th January 2007 17:17
ubuntu ispconfig joomla .htaccess steve1084 General 8 6th January 2007 15:55
HotSaNIC domino Tips/Tricks/Mods 23 6th November 2006 05:19
jamed up my table borders?? Boon-Dog-Danny Installation/Configuration 5 23rd September 2006 16:12


All times are GMT +2. The time now is 21:47.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.