Home > Code Snippets, PHP, Tips > Class DumpIO – Inspired by Apache mod_dumpio but reluctant to restart webserver

Class DumpIO – Inspired by Apache mod_dumpio but reluctant to restart webserver

January 27th, 2012 Leave a comment Go to comments

Though there are far and wide systems for live debugging, and the sort, for forensic or load analysis, our php-extjs framework did not have anything pre planned, other than some query loggers, and background processing systems to log into db etc. While recently the Master MySQL server started showing variations in the cacti patterns (normal was about 4 to 20 in working window, but was steady between 35 and 40 in the tantrum period), we started to worry and could not identify the situation. Also restarting all application servers and clearing the session store would immediately drop the MySQL fsync graph to a standard pattern. This is the time when I looked for a input logger for Apache, and found about the dumpio, but needed the webserver to be restarted. Actually the time was ripe that the application was in a tantrum, and the MySQL graphs showing about 35 fsyncs average.

Revisiting Importance of event logging in server side scripting and other articles on the net, the out come was a class with a single static method. This was designed to pick and log any input. This was later moved as the lite version, and a full version capable of capturing the output also was built.

The lite version is as follows.
<?php
  
/**
 * @package Generic
 * @subpackage DumpIO
 *
 * @author: jijutm@saturn.in http://www.saturn.in
 * Got inspired by the Apache dump_io module, but was reluctant to restart the
 * webserver to start a dump window for doing forensic on our project.
 */
 
  
class DumpLite
{
    public static function 
Input(){
    
        
$dx = array("_GET""_POST""_SESSION""_FILES");
        
$d = array();
        foreach(
$dx as $x){
           global $
$x;
           if(!empty($
$x)){
              
$d[$x] = $$x;
           }
        }
        
$logfile '/var/log/phpdumpio.log';
        
$write = array(date("c"),$_SERVER['REMOTE_ADDR'],$_SERVER['REQUEST_URI'],base64_encode(serialize($d)), "\n");
        
file_put_contents($logfilejoin(' '$write), FILE_APPEND);
    }
}
  
DumpLite::Input();

The full version is as follows, and just needs to be included into the primary controller.
<?php
  
/**
 * @package Generic
 * @subpackage DumpIO
 *
 * @author: jijutm@saturn.in http://www.saturn.in
 * Got inspired by the Apache dump_io module, but was reluctant to restart the
 * webserver to start a dump window for doing forensic on our project.
 */
 
  
class phpDumpIO
{
  
    private 
$get;
    
    public function 
__construct(){
        
$this->get = array();
    }
  
    public function 
__destruct(){
       
$this->get['_OUTPUT'] = ob_get_contents();
       
$this->writelog($this->get);
    }
  
    public function 
DumpIOInput($get false){
        
$dx = array("_GET""_POST""_SESSION""_FILES");
        
$d = array();
        foreach(
$dx as $x){
           global $
$x;
           if(!empty($
$x)){
              
$d[$x] = $$x;
           }
        }   
        if(
$get == false)   
            
$this->writelog($d);
        else
            
$this->get $d;    
    }
  
    public function 
DumpIOOutput(){
        
ob_start();
    }
  
    public function 
DumpIOBoth(){
        
$this->DumpIOInput(true);
        
$this->DumpIOOutput();
    }
  
    private function 
writelog($d){
        
$logfile '/var/log/phpdumpio.log';
        
$write = array(date("c"),$_SERVER['REMOTE_ADDR'],$_SERVER['REQUEST_URI'],base64_encode(serialize($d)), "\n");
        
file_put_contents($logfilejoin(' '$write), FILE_APPEND);
    }
}
  
$tracedump = new phpDumpIO();
$tracedump->DumpIOBoth();

The files which are created has some parts as base64_encoded, since the _SESSION if poorly coded, can have newlines and even html code which could break if stored otherwise. So an analyser is also made, but piping in more than a 100 lines of log to the analyser would overload that in a browser. Any way all the code is combined to a zip for easy download.PHP DumpIO - Log inupt and output (204)

  1. July 27th, 2012 at 07:54 | #1

    This is a great solution, thanks! One questions though — the DumpIOOutput() function doesn’t seem to dump anything to the log file. Any thoughts on how best to do this?

  2. July 30th, 2012 at 13:18 | #2

    @Vito Sorelli
    Hi thanks for the wonderful comment. The DumpIOOutput does not work instantaneously, but depends on ob_start() and should write the output as soon as the class instance is destroyed. Infact it is invoked in the destructor. In case your script dies or gets killed abnormally this wont do any help. Also the $logfile should point to a file which the webserver (apache user, www-data in stock linux distros) is able to write.

  1. No trackbacks yet.