Skip to content

Instantly share code, notes, and snippets.

@sholsinger
Created October 12, 2011 06:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sholsinger/1280429 to your computer and use it in GitHub Desktop.
Save sholsinger/1280429 to your computer and use it in GitHub Desktop.
A quick 'n' dirrrty PHP script to parse IIS log files and insert them into a relational database table. (ala MySQL)
<?
define("DEBUG", FALSE);
function get_log_file($file_path)
{
if(file_exists($file_path))
{
return file_get_contents($file_path);
}
return false;
}
function get_lines($string)
{
return explode("\r\n", $string);
}
# main
$lines = get_lines( get_log_file('report.txt') );
$mysqli = new mysqli('hostname', 'user', 'password', 'db');
if($mysqli->connect_error || mysqli_connect_error())
{
$msg = 'Failed to connect to the database. ';
if($mysqli->connect_error)
{
$msg .= '(' . $mysqli->connect_errno . ') ' . $mysqli->connect_error;
} else {
$msg .= '(' . mysqli_connect_errno() . ') ' . mysqli_connect_error();
}
die($msg);
}
$inserted=0;
foreach($lines as $line)
{
if (strlen(trim($line)) > 0) {
$data = sscanf($line, "%12s:%19[ -~] %s %s %s %s %s - %s %s %s %s %s");
if(trim($data[5])=="-" || trim($data[5]=="")) {
$data[5] = '';
} else {
$data['4'] = $data[4] . "?" . $data[5];
}
$qry = sprintf("INSERT INTO iis_logs (`orig_report_file`, `timestamp`, `server_ip`, `request_method`, `server_port`, `client_ip`, `browser_info`, `response`, `num1`, `num2`) VALUES ('%s','%s','%s','%s',%s,'%s','%s',%s,%s,%s)", $mysqli->real_escape_string($data[0]), $data[1], $mysqli->real_escape_string($data[2]), $mysqli->real_escape_string($data[3]), $mysqli->real_escape_string($data[6]), $mysqli->real_escape_string($data[7]), $mysqli->real_escape_string($data[8]), $data[9], $data[10], $data[11]);
if(!DEBUG) {
$mysqli->real_query($qry);
}
if ($mysqli->affected_rows > 0 || DEBUG)
{
$inserted++;
} else if ($mysqli->error) {
die("Error (" . $mysqli->errno . ") " . $mysqli->error . "\nQuery used: " . $qry . "\nData used: " . print_r($data, TRUE) . "\nLine used: " . $line ."\nRows inserted: ". $inserted . " out of " . count($processed) . "\n");
}
$processed[] = $data;
if(DEBUG){
if ($data[5]!=''){
echo "Row: $line\n" . print_r($data, TRUE) . "\nSQL=\"$qry\"\n";
}
}
}
}
/*
0 -> report_file
1 -> orig_report_file
2 -> timestamp
3 -> server_ip
4 -> request_method
5 -> server_port
6 -> client_ip
7 -> browser_info
8 -> response
9 -> num1
10 -> num2
*/
echo sprintf("Inserted %s of %s rows.", $inserted, count($processed));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment