VB icon

Webserver Logfile Analyzer

Email
Submitted on: 1/2/2015 4:22:00 AM
By: Lucas Moten (from psc cd)  
Level: Beginner
User Rating: By 3 Users
Compatibility: C, C++ (general), Microsoft Visual C++, UNIX C++
Views: 491
 
     I wrote this quick bit of code after looking at the cost of a product from the leader in the analysis of logfiles. - I'm a website operator and I have my sites hosted, and as such, the little things, like bandwidth usage, become rather important. Free Online Service type methods for tracking website usage cant provide bandwidth reports because a website is more then just html files. This program will go through a logfile of a specified format and add up the total bandwidth from server to client.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Webserver Logfile Analyzer
// Description:I wrote this quick bit of code after looking at the cost of a product from the leader in the analysis of logfiles. - I'm a website operator and I have my sites hosted, and as such, the little things, like bandwidth usage, become rather important. Free Online Service type methods for tracking website usage cant provide bandwidth reports because a website is more then just html files. This program will go through a logfile of a specified format and add up the total bandwidth from server to client.
// By: Lucas Moten (from psc cd)
//
// Inputs:When run, the program looks at the first command line parameter. If a value is present, then it will try to open that value as the filename for the logfile to analyze. If no value is supplied, it assumes that the file to look for is 'wwwtraffic.log'
//
// Returns:The number of total actual hits (not pageviews) are provided, as well as total bandwidth, in either Bytes, Kilobytes, or Megabytes.
//
// Assumes:It assumes that the field in question, generally named "sc-bytes" is the 11th field in a hit line. If your logfile format differs, then you'll need to adjust the number of "strtok" calls.
//
// Side Effects:Values of gigabytes and terrabytes will be reported in megabytes. For example, 3 gigabytes, would be reported as 3000MB.
//**************************************

#include <stdio.h>		// io access (filestream, screen)
#include <stdlib.h>		// atoi
#include <string.h>		// strtok
int main(int argc, char* argv[])
{
	printf("\n");
	printf("-------------------------------------------------------------------------------\n");
	printf("Bandwidth Calculator for WWW Logfiles v. 0.03a\n");
	printf("Written by Lucas Moten(krozy@theonlinegamer.com) 2001-04-30\n");
	printf("-------------------------------------------------------------------------------\n");
	//FILE FORMAT: (code utilizes the 11th field)
	//
	//#Fields: date time c-ip cs-username s-ip s-port cs-method cs-uri-stem cs-uri-query sc-status sc-bytes cs-bytes time-taken cs-version cs(User-Agent) cs(Cookie) cs(Referer) 
	//2000-12-01 00:02:09 64.20.169.144 - 64.27.79.90 80 GET /favicon.ico - 200 3228 239 828 HTTP/1.1 Mozilla/4.0+(compatible;+MSIE+5.5;+Windows+98) ASPSESSIONIDGQGQQJCQ=OACMLIMAMCGBPMFAKFGMHAOO -
	int bytes = 0;
	int kbytes = 0;
	int mbytes = 0;
	int linebytes = 0;
	char line[4096];
	int toknum = 0;
	char * token;
	int hits = 0;
	// Open the file supplied in the first command line argument
	// If no filename is supplied, try a default of 'wwwtraffic.log'
	FILE* logfile;
	if(argv[1] != NULL) {
		logfile = fopen(argv[1], "r");
	} else {
		printf("Name of logfile was not supplied, trying 'wwwtraffic.log'\n");
		logfile = fopen("wwwtraffic.log", "r");
	}
	if(logfile == NULL) {
		printf("The file wasn't found.\n");
		return 0;
	}
	// If we have an error opening the file, then bail out.
	if(!(ferror(logfile))) {
		// Continue reading until we reach the end of the file
		while( fgets(line, 4096, logfile) != NULL )
		{	
			// Ignore Comments
			if(line[0] != '#')		
			{
				// Not a comment, increase our hit count. Report the number
				// of hits as "eye-candy", for every 50,000 hits so that the
				// user doesnt think the program isn't working
				hits ++;
				if ((hits%50000)==0) printf("Total Hits so far: %i\n", hits); //status
				// Reset information for this line
				toknum = 0;
				linebytes = 0;
				// Prep the String Tokenizer. Note that this would return
				// the first 'token'. A token is deliminated by the 2nd parameter
				strtok(line, " ");
				// bleed off 9 more 'tokens'.
				for(toknum = 0; toknum < 9; toknum ++) strtok(NULL, " ");
				// We've reached the 11th token, this is the one we want, so capture it
				token = strtok(NULL, " ");
				// Make sure the token is valid, and if so, set the amount of bytes
				if (token != NULL) linebytes = atoi(token);
				// Add the bytes and perform conversions
				bytes = bytes + linebytes;
				if(bytes > 1024) {
					kbytes ++;
					bytes -= 1024;
					if(kbytes > 1024) {
						mbytes ++;
						kbytes -= 1024;
					}
				}
			}
		}
		// Done with the file, close it out
		fclose(logfile);	
	} else {
		// Report the fact that there was an error
		printf("error opening file!\n");
	}
	// Show bandwidth + total hit results. Not that in terms of bandwidth,
	// we're showing whatever the largest type was. - easily convertible to
	// include gigabytes, terrabytes, etc. as this becomes necessary in the
	// future.
	if(mbytes)
	{
		printf("Bandwidth in Megabytes: %i\n", mbytes);
	} else {
		if(kbytes)
		{
			printf("Bandwidth in Kilobytes: %i\n", kbytes);
		} else {
			printf("Bandwidth in Bytes: %i\n", bytes);
		}
	}
	printf("Total Hits: %i\n", hits);
	return 0;
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.