#!/usr/bin/perl use CGI qw/:standard/; use URI::Escape; use warnings; use strict; #-------------------------------------------------------------------------------- # tiny Stats generator -- Gaurang Khetan (http://gaurang.org) # I would appreciate it if you let me know that you are using this tiny stats program # Version 0.3 : Last modified: 2005/10/18 # DONT MAKE THE MISTAKE OF RELYING ON THIS SCRIPT TO RUN CORRECTLY ALWAYS #-------------------------------------------------------------------------------- # #-------------------------------------------------------------------------------- # What this does: # this stats program will generate a stats file consisting of stats like: # # Page URL: # Time: Mon Dec 20 16:49:55 2004 # Remote Address: 24.77.96.255 # Hostname: S0106000802f28668.wp.shawcable.net # Location: Vancouver, British Columbia, Canada with Certainty: 97 # Referrer URL: http://www.google.ca/search?hl=en&q=%22tell me my application status%22&meta= # Browser: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322) # # Page URL: # Time: Mon Dec 20 18:06:03 2004 # Remote Address: 128.125.104.195 # Hostname: bern.usc.edu # Location: Los Angeles, California, United States with Certainty: 99 # Referrer URL: # Browser: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0 #-------------------------------------------------------------------------------- # #-------------------------------------------------------------------------------- # How to set this script up: # #**** Step 1: rename this to file with a ".pl" extension, and place it in the public_html/cgi-bin directory or equivalent # if unsure, ask your webhhost's tech support, as to where perl scripts should be placed # #**** Step 2: make sure that the path to perl is correct on the first line of this file above, or else change it # if unsure, ask your webhhost's tech support, as to what should be the first line of perl scripts # #**** Step 3: Customize the following 3 variables # the output file name of the file to contain the stats # (might want to periodically archive if this file grows too big) my $output_stats_file = "../../public_html/files/stats/stats.txt"; my $output_stats_file_maxsize = 300000; #after this it saves it as a oldstats file my $output_stats_old_dir = "../../public_html/files/stats"; my $output_stats_old_file_pattern_glob = "oldstats*.txt"; my $output_stats_old_file_pattern_perl = "oldstats(\\d+).txt"; # your home ip address that you want to avoid taking stats of -- since you dont want your own visits to interfere in stats file my $avoid_ip_address = "24.7.30.173"; # optional: set your time zone because server may be anywhere (use Eastern/Central/Mountain/Pacific etc) # also check whether the path exists, if not, comment this line $ENV{TZ} = ':/usr/share/zoneinfo/US/Pacific'; #**** Step 4: Add the following javascript code to your HTML pages which are to be stat'ed # make sure that the path to the stats.pl file is correct for your system # # # # #**** Thats it. You are done. #-------------------------------------------------------------------------------- # if ($ENV{"REMOTE_ADDR"} ne $avoid_ip_address ) { my $referrer = uri_escape(param("referrer"), " "); #since many ppl block the referer, we should take "page" param preferably to atleast show the actual Page URL # if page param is not present, then we could try to get that Page URL through the referer of *this script* my $pageurl; if (param("page")) { $pageurl = param("page"); } else { $pageurl = $ENV{"HTTP_REFERER"}; } my $strbuf = ""; $strbuf = $strbuf."Page URL: $pageurl\n"; $strbuf = $strbuf."Time: ".(my $t=localtime())."\n"; $strbuf = $strbuf."Remote Address: ".$ENV{"REMOTE_ADDR"}."\n"; $strbuf = $strbuf.host_string($ENV{"REMOTE_ADDR"}); $strbuf = $strbuf.location_string($ENV{"REMOTE_ADDR"}); $strbuf = $strbuf."Referrer URL: ".$referrer."\n"; $strbuf = $strbuf."Browser: ".$ENV{"HTTP_USER_AGENT"}."\n"; if (-e "$output_stats_file" && (-s "$output_stats_file" > "$output_stats_file_maxsize") ) { # archive this log file # instead of below, could also have done : # opendir (DIRHANDLE, $output_stats_old_dir) or die "cannot open dir"; # @filenames = readdir(DIRHANDLE); #returns list of all files # closedir(DIRHANDLE); #get high number of the current log files my $high_number = 0; my $high_number_string; my @filenames = <${output_stats_old_dir}/${output_stats_old_file_pattern_glob}>; foreach my $filen ( @filenames ) { if ($filen =~ /${output_stats_old_file_pattern_perl}/ ) { if ($1 > $high_number) { $high_number = $1; } } } $high_number++; $high_number_string = sprintf("%03d", $high_number); my $new_archive_name = $output_stats_old_file_pattern_glob; $new_archive_name =~ s/\*/${high_number_string}/; $new_archive_name = "${output_stats_old_dir}/${new_archive_name}"; rename $output_stats_file, $new_archive_name; } open(FH, ">>$output_stats_file"); print FH "\n$strbuf"; close(FH); } # # now instead of reading the GIF file data from a gif file, we will put it directly here # my $file = ""; # $image_file_name = "img.gif"; # if (-e $image_file_name && -r $image_file_name) # { # #read the image file into $file # open(FH, "<$image_file_name"); # binmode(FH); # read(FH, $file, 45); # close(FH); # } # following is a 74 byte dump of a PNG file my $file = pack ( "C*", 137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 4, 0, 0, 0, 4, 8, 0, 0, 0, 0, 140, 154, 193, 162, 0, 0, 0, 17, 73, 68, 65, 84, 120, 156, 99, 252, 207, 192, 192, 192, 196, 128, 66, 0, 0, 19, 81, 1, 7, 128, 78, 141, 30, 0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130 ); #print it out { undef $\; #so that the \n character does not appear at the end of the print statement print header(-type=>'image/png',-Content_length=>74,-Cache_control=>'no-cache',-Pragma=>'no-cache'); binmode(STDOUT); print $file; } exit(0); # # Takes ip address as a scalar string argument # returns a string to be added to the output file like # "Hostname: phantom.rr.sun.com\n" # sub host_string { my $ipad = shift(@_); use Socket; my $iaddr = inet_aton($ipad); my $name = gethostbyaddr($iaddr, AF_INET); my $to_ret = "Hostname: "; if ($name) { $to_ret = $to_ret.$name."\n"; } else { $to_ret = $to_ret."Does not exist\n"; } return($to_ret); } # # Takes ip address as a scalar string argument # returns a string to be added to the output file like # "Location: Los Angeles, California, United States Certainty: 99 \n" # sub location_string { my $ipad = shift(@_); my $to_ret = "Location: "; my $url = 'http://www.geobytes.com/IpLocator.htm?GetLocation&cid=0&c=0&Template=iplocator.htm&ipaddress='.$ipad; # get the page via libwww-perl (LWP) use LWP::Simple; my $outputwhole = get($url); # # get the page via a system command wget # my $outputwhole = `wget -q -O - '$url'`; my @output = split "\n", $outputwhole; #break it up into different lines my @temp; @temp = grep(/ro-no_bots_pls17/, @output); $temp[0] =~ /value=\"(.*?)\"/; $to_ret = $to_ret.$1; @temp = grep(/ro-no_bots_pls15/, @output); $temp[0] =~ /value=\"(.*?)\"/; $to_ret = $to_ret.", ".$1; @temp = grep(/ro-no_bots_pls13/, @output); $temp[0] =~ /value=\"(.*?)\"/; $to_ret = $to_ret.", ".$1; @temp = grep(/ro-no_bots_pls18/, @output); $temp[0] =~ /value=\"(.*?)\"/; $to_ret = $to_ret." with Certainty: ".$1; # @temp = grep(!/Capital/, grep(!/CityId/, grep(!/Code/, grep(/value/, grep(/City/, @output))))); # $temp[0] =~ /value=\"(.*?)\"/; # $to_ret = $to_ret.$1; # @temp = grep(!/Code/, grep(/value/, grep(/Region/, @output))); # $temp[0] =~ /value=\"(.*?)\"/; # $to_ret = $to_ret.", ".$1; # @temp = grep(!/Code/, grep(/value/, grep(/Country/, @output))); # $temp[0] =~ /value=\"(.*?)\"/; # $to_ret = $to_ret.", ".$1; # @temp = grep(/value/, grep(/Certainty/, @output)); # $temp[0] =~ /value=\"(.*?)\"/; # $to_ret = $to_ret." with Certainty: ".$1; $to_ret = $to_ret."\n"; return($to_ret); }