Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/php
- <?php
- /**
- * This script is designed to take in the standard apache "combined" log format
- * and parse it into it's components. Then, store them in the the mongo database
- * named whatever the hostname of the server is.
- *
- * @version 1.0
- * @author Breaker One <breaker1@gmail.com>
- */
- $in = fopen("php://stdin", "r");
- ob_implicit_flush(true);
- /**
- * Beginning in Apache 2, piped log programs are
- * expected to act like daemons
- */
- while($line = fgets($in) ) {
- /** Be sure we're not dealing with empty data here */
- if( strlen($line) > 3){
- /**
- * Using the "combined" log format, this should produce an array with the following
- * remote_host, remote_logname, remote_user, request_datetime, request_type,
- * request_resource, request_version, status_code, request_size, referer, user_agent
- */
- 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);
- // Loop through the results and make them look how we need
- foreach($entry as $key=>$value){
- // We only want our named groups, not the rest of the matches
- if( is_int($key) ) {
- unset($entry[$key]);
- }
- // Turn the request time into the date object
- elseif($key == "request_datetime"){
- $entry[$key] = new MongoDate( strtotime($value) );
- }
- // Apache uses - to represent null, we actually want null
- elseif($value == "-"){
- $entry[$key] = null;
- }
- // Break the request resource out to the resource and an array of parameters
- elseif($key == "request_resource"){
- $parts = explode("?", $entry["request_resource"]);
- if( count($parts) == 2 ) {
- // break each set of key=val out
- $request_params = array();
- $params = explode("&", $parts[1]);
- foreach($params as $param){
- // insert the key and value into our array
- $keyval = explode("=", $param);
- // store things as ints when possible
- if( is_numeric($keyval[1]) ){
- $request_params[$keyval[0]] = (int)$keyval[1];
- }
- else{
- $request_params[$keyval[0]] = $keyval[1];
- }
- }
- // change the request resource to be just the file and add the request params array
- $entry[$key] = $parts[0];
- $entry["request_parameters"] = $request_params;
- }
- }
- }
- try{
- $host = exec("hostname");
- $mongo = new Mongo("mongodb://127.0.0.1:27017");
- $database = $mongo->selectDB("apache_access_log_{$host}");
- $database->entries->insert($entry);
- }
- catch( MongoConnectionException $e){
- var_dump($e);
- }
- }
- }
- exit(0);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement