steveosoule
3/25/2014 - 6:40 PM

Process Traffic & Data Usage from Server Access Log

Process Traffic & Data Usage from Server Access Log

107.20.124.22 - - [24/Mar/2014:04:13:23 -0400] "GET /Merchant2/graphics/00000007/FLY-INVERSION-HAT.jpg HTTP/1.1" 200 70187 "-" "Apache-HttpClient/4.1.2 (java 1.5)"
107.20.124.22 - - [24/Mar/2014:04:13:23 -0400] "GET /Merchant2/graphics/00000007/FLY-INVERSION-HAT.jpg HTTP/1.1" 200 70186 "-" "Java/1.6.0_20"
124.184.11.252 - - [24/Mar/2014:04:13:23 -0400] "GET /parts/parts-finder_v3.js?v=2&_=1395648805530 HTTP/1.1" 200 1676 "http://www.btosports.com/" "Mozilla/5.0 (iPad; CPU OS 7_1 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Version/7.0 Mobile/11D167 Safari/9537.53"
107.20.124.22 - - [24/Mar/2014:04:13:24 -0400] "GET /Merchant2/graphics/00000007/FLY-INVERSION-HAT.jpg HTTP/1.1" 200 70186 "-" "Java/1.6.0_20"
93.202.14.58 - - [24/Mar/2014:04:13:24 -0400] "GET /Merchant2/images/win-helmet.png HTTP/1.1" 200 51081 "http://www.btosports.com/c/SEVEN" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0"
107.20.124.22 - - [24/Mar/2014:04:13:24 -0400] "GET /Merchant2/graphics/00000007/FLY-2013-INVERSION-HOODY.jpg HTTP/1.1" 200 60988 "-" "Apache-HttpClient/4.1.2 (java 1.5)"
107.20.124.22 - - [24/Mar/2014:04:13:24 -0400] "GET /Merchant2/graphics/00000007/FLY-2013-INVERSION-HOODY.jpg HTTP/1.1" 200 60987 "-" "Java/1.6.0_20"
107.20.124.22 - - [24/Mar/2014:04:13:24 -0400] "GET /Merchant2/graphics/00000007/FLY-2013-INVERSION-HOODY.jpg HTTP/1.1" 200 60987 "-" "Java/1.6.0_20"
68.196.143.0 - - [24/Mar/2014:04:13:23 -0400] "GET /p/SCOTT-DEUCE-GRIPS HTTP/1.1" 200 116715 "http://www.btosports.com/p/PROTRI" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.59.10 (KHTML, like Gecko) Version/5.1.9 Safari/534.59.10"
216.232.249.160 - - [24/Mar/2014:04:13:23 -0400] "GET /p/ASVF3CLUTCHHONDA HTTP/1.1" 200 116904 "http://www.btosports.com/p/ASVF3CLUTCHPERCHHONDA" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.59.8 (KHTML, like Gecko) Version/5.1.9 Safari/534.59.8"
107.20.124.22 - - [24/Mar/2014:04:13:24 -0400] "GET /Merchant2/graphics/00000007/FLY-2013-INVERSION-LS-TEE.jpg HTTP/1.1" 200 58130 "-" "Apache-HttpClient/4.1.2 (java 1.5)"
107.20.124.22 - - [24/Mar/2014:04:13:25 -0400] "GET /Merchant2/graphics/00000007/FLY-2013-INVERSION-LS-TEE.jpg HTTP/1.1" 200 58129 "-" "Java/1.6.0_20"
168.87.3.33 - - [24/Mar/2014:04:13:25 -0400] "GET /Merchant2/js/jquery.ie-select-width.js HTTP/1.1" 200 3751 "http://www.btosports.com/p/FLY-RACING-2013-KINETIC-DASH-HELMET" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)"
68.196.143.0 - - [24/Mar/2014:04:13:25 -0400] "GET /parts/search_v3.php?v=289728 HTTP/1.1" 200 1326 "http://www.btosports.com/p/SCOTT-DEUCE-GRIPS" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.59.10 (KHTML, like Gecko) Version/5.1.9 Safari/534.59.10"
68.196.143.0 - - [24/Mar/2014:04:13:25 -0400] "GET /parts/parts-finder_v3.js?v=2&_=1395648805216 HTTP/1.1" 200 1675 "http://www.btosports.com/p/SCOTT-DEUCE-GRIPS" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.59.10 (KHTML, like Gecko) Version/5.1.9 Safari/534.59.10"
168.87.3.33 - - [24/Mar/2014:04:13:25 -0400] "GET /pwr/engine/js/full.js HTTP/1.1" 200 28042 "http://www.btosports.com/p/FLY-RACING-2013-KINETIC-DASH-HELMET" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)"
168.87.3.33 - - [24/Mar/2014:04:13:24 -0400] "GET /p/FLY-RACING-2013-KINETIC-DASH-HELMET HTTP/1.1" 200 118082 "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)"
107.20.124.22 - - [24/Mar/2014:04:13:25 -0400] "GET /Merchant2/graphics/00000007/FLY-2013-INVERSION-LS-TEE.jpg HTTP/1.1" 200 58129 "-" "Java/1.6.0_20"
216.232.249.160 - - [24/Mar/2014:04:13:25 -0400] "GET /parts/search_v3.php?v=48895 HTTP/1.1" 200 1327 "http://www.btosports.com/p/ASVF3CLUTCHHONDA" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.59.8 (KHTML, like Gecko) Version/5.1.9 Safari/534.59.8"
101.161.34.117 - - [24/Mar/2014:04:13:25 -0400] "GET /Merchant2/v2/images/icn-rarr-red.png HTTP/1.1" 200 636 "http://www.btosports.com/" "Mozilla/5.0 (Linux; U; Android 4.1.2; en-au; GT-N8000 Build/JZO54K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Safari/534.30"
<?
	// process_file("sample.txt");
	process_file("access_log.txt");

	function process_file($file = 'sample.txt')
	{
		// Declare Variables
		$total_bytes = 0;
		$graphic_bytes = 0;
		$line_count = 0;
		$user_agents = array();
		$product_hits = 0;
		$ips = array();

		// Process File
		$handle = fopen($file, "r") or die("Couldn't get handle");
		if ($handle) {
			while (!feof($handle)) {

				// Manage count
				$line_count++;
				/*if( $line_count > 10 ){
					exit();
				}*/

				// Parse Line
				$buffer = fgets($handle, 4096);

				// Get Bytes
				$line_array = explode(' HTTP', $buffer);
				$line_array = explode(' ', $line_array[1]);
				$line_bytes = $line_array[2];

				// Get UserAgent
				$ua_array = explode('"', $buffer);
				$index = count($ua_array) - 2;
				$ua = $ua_array[ $index ];
				if( isset($user_agents[$ua]) ) $user_agents[$ua]++;
				else $user_agents[$ua] = 1;

				// Get IPs
				$ip_array = explode(' ', $buffer);
				$ip = $ip_array[0];

				/*$ip_range = explode('.', $ip_array[0]);
				array_pop($ip_range);
				$ip = implode('.', $ip_range );*/

				if( isset($ips[$ip]) ) {
					$ips[$ip]['count']++;
					$ips[$ip]['bytes'] += $line_bytes;
				} else {
					$ips[$ip] = array(
						'count' => 1,
						'bytes' => $line_bytes
					);
				}

				// Check for product page view
				if( preg_match("/\/product\//", $buffer) ){
					$product_hits++;
				}

				// Check for image file
				if( preg_match("/mm5\/graphics\/00000001/", $buffer) ){
					$graphic_bytes += $line_bytes;
				}

				// Add to total
				$total_bytes += $line_bytes;
			}
			fclose($handle);
		} else {
			echo 'No handle';
		}

		/*return array(
			'graphics' => bytes_to_gb($graphic_bytes),
			'total' => bytes_to_gb($total_bytes),
			'percentage' => percentage($graphic_bytes, $total_bytes)
		);*/

		echo "File: $file\n\n";
		echo "Graphic Data: ".bytes_to_gb($graphic_bytes)." GB\n";
		echo "Product Page Hits: ".$product_hits."\n";
		echo "Total Data: ".bytes_to_gb($total_bytes)." GB\n";
		echo "Percentage:".percentage($graphic_bytes, $total_bytes)."%\n\n";

		/*array_multisort($user_agents, SORT_DESC);
		var_dump($user_agents);*/

		array_multisort($ips, SORT_DESC);
		$output_count = 0;
		echo "ip\tcount\tGB\t%\n";
		foreach ($ips as $key => $value) {
			if( $output_count > 10 ) exit();
			$output_count++;
			echo $key."\t".$value['count']."\t".bytes_to_gb($value['bytes'])."\t".percentage($value['bytes'], $total_bytes)."\n";
		}
		// var_dump($ips);

		echo "\n\n!@!\n\n";
	}

	function percentage($val1, $val2, $precision = 2)
	{
		$division = $val1 / $val2;
		$res = $division * 100;
		$res = round($res, $precision);
		return $res;
	}

	function bytes_to_gb($bytes){
		return round($bytes / 1073741824, 2);
	}
?>