Advertisement
Guest User

apache log parser

a guest
Dec 10th, 2012
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.95 KB | None | 0 0
  1. #!/usr/bin/php
  2. <?php
  3.     /**
  4.      * This script is designed to take in the standard apache "combined" log format
  5.      * and parse it into it's components. Then, store them in the the mongo database
  6.      * named whatever the hostname of the server is.
  7.      *
  8.      * @version 1.0
  9.      * @author Breaker One <breaker1@gmail.com>
  10.      */
  11.     $in = fopen("php://stdin", "r");
  12.     ob_implicit_flush(true);
  13.    
  14.     /**
  15.      * Beginning in Apache 2, piped log programs are
  16.      * expected to act like daemons
  17.      */
  18.     while($line = fgets($in) ) {
  19.            
  20.         /** Be sure we're not dealing with empty data here */
  21.         if( strlen($line) > 3){
  22.            
  23.             /**
  24.              * Using the "combined" log format, this should produce an array with the following
  25.              * remote_host, remote_logname, remote_user, request_datetime, request_type,
  26.              * request_resource, request_version, status_code, request_size, referer, user_agent
  27.              */
  28.             preg_match("/(?P<remote_host>[a-zA-Z0-9\.]+)\s(?P<remote_logname>[[:alpha:][:punct:]]+)\s(?P<remote_user>[[:alpha:][:punct:]]+)\s\[(?P<request_datetime>.+)]\s\"(?P<request_type>.*?)\s(?P<request_resource>.*?)\s(?P<request_version>.*?)\"\s(?P<status_code>[0-9]{3})\s(?P<request_size>[0-9\-]+)\s\"(?P<referer>.*?)\"\s\"(?P<user_agent>.*?)\"/", $line, $entry);
  29.            
  30.             // Loop through the results and make them look how we need
  31.             foreach($entry as $key=>$value){
  32.                
  33.                 // We only want our named groups, not the rest of the matches
  34.                 if( is_int($key) ) {
  35.                     unset($entry[$key]);
  36.                 }
  37.                
  38.                 // Turn the request time into the date object
  39.                 elseif($key == "request_datetime"){
  40.                     $entry[$key] = new MongoDate( strtotime($value) );
  41.                 }
  42.                
  43.                 // Apache uses - to represent null, we actually want null
  44.                 elseif($value == "-"){
  45.                     $entry[$key] = null;
  46.                 }
  47.                
  48.                 // Break the request resource out to the resource and an array of parameters
  49.                 elseif($key == "request_resource"){
  50.                     $parts = explode("?", $entry["request_resource"]);
  51.                     if( count($parts) == 2 ) {
  52.                        
  53.                         // break each set of key=val out
  54.                         $request_params = array();
  55.                         $params = explode("&", $parts[1]);
  56.                         foreach($params as $param){
  57.                            
  58.                             // insert the key and value into our array
  59.                             $keyval = explode("=", $param);
  60.                            
  61.                             // store things as ints when possible
  62.                             if( is_numeric($keyval[1]) ){
  63.                                 $request_params[$keyval[0]] = (int)$keyval[1];
  64.                             }
  65.                             else{
  66.                                 $request_params[$keyval[0]] = $keyval[1];
  67.                             }
  68.                            
  69.                         }
  70.                        
  71.                         // change the request resource to be just the file and add the request params array
  72.                         $entry[$key] = $parts[0];
  73.                         $entry["request_parameters"] = $request_params;
  74.                     }
  75.                 }
  76.             }
  77.            
  78.             try{
  79.                 $host = exec("hostname");
  80.                 $mongo = new Mongo("mongodb://127.0.0.1:27017");
  81.                 $database = $mongo->selectDB("apache_access_log_{$host}");
  82.                 $database->entries->insert($entry);
  83.             }
  84.             catch( MongoConnectionException $e){
  85.                 var_dump($e);
  86.             }
  87.            
  88.         }
  89.     }
  90.  
  91.     exit(0);
  92. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement