Archive for April, 2008

World’s smallest Apache log analyser.

Friday, April 11th, 2008

Right here:

#!/usr/bin/perl

##
# Simple Apache access log parser that yields useful statistics.
# Designed to serve basic web analysis needs.
#
# Alex Balashov 

use strict;
use warnings;

## GLOBALS ##
my      @hits = ();
#############

# Preload.

while() {
        chomp;

        if(/^([0-9.]+)\s+\-\s+\-\s+\[(.[^]]*)\]\s+\”(.[^”]*)\”\s+[0-9]+\s+[0-9]+
\s+\”(.[^”]+)\”/o) {
                push(@hits,
                        {
                                ‘ip_addr’ => $1,
                                ‘date’ => $2,
                                ‘request’ => $3,
                                ‘referrer’ => $4
                        });
        }
}

# Generate reports.

print “Access log report: \n\n”;

print “Top 20 IP addresses:\n” .
      “——————-\n\n”;

&sorted_report(’ip_addr’, 20);

print “\nTop 50 requests: \n” .
      “—————\n\n”;

&sorted_report(’request’, 50);

print “\nTop 50 referrers: \n” .
      “——————\n\n”;

&sorted_report(’referrer’, 50);

# Generate function to hash out unique requests and sort by a certain
# hashed criterion.

sub     sorted_report {
        my      ($key, $limit) = @_;
        my      %unique_tokens = ();

        foreach(@hits) {
                $unique_tokens{$_->{$key}} = 0 unless
                        exists ($unique_tokens{$_->{$key}});

                $unique_tokens{$_->{$key}} ++;
        }

        foreach(sort { $unique_tokens{$b} <=>
                        $unique_tokens{$a}
                     } keys %unique_tokens) {
                $limit — unless $limit == 0;

                printf ”  %-50s %d\n”, $_, $unique_tokens{$_};

                last if $limit == 0;
        }
}

Tells me everything I need to know. Try it yourself:

   cat access.log | perl analyse.pl

Khrushchev on Western mass food-processing, Dutch chickens.

Thursday, April 10th, 2008

I apologise for my quietude lately. Rumours of my disappearance have been greatly exaggerated, but I have been busy running a business (see last post).

In the meantime:
khrushchev.jpg

From the second volume of Khruschev’s memoirs, the section “Plight of the Consumer”:


When the State mismanages agriculture, the average Soviet citizen suffers. How do we know when the State is mismanaging agriculture? I believe the food counters more than I believe the statistics I read. For that matter, I think the mood of the average housewife is a better indicator than the bureau of statistics about the health of our economy. As I’ve already said, our statisticians sometimes deliberately distort reality; the rosy figures they publish in the newspapers can’t be sold in the stores and made into soup.


What does the mood of the housewives tell us? What do the food counters in the stores indicate about the current level of our economy? They tell us that all is not well, that the State has failed to satisfy both the quantitative and qualitative demands of our consumers.


Even in Moscow, which has always enjoyed special privileges, shoppers can’t be sure of finding the meat they want. There’s also a shortage of eggs and poultry. In fact, if you’re determined to buy chicken, you’ll probably have to settle for poultry imported from Holland and other countries. These birds are usually too fat for our people’s taste, and the Dutch chicken has the additional disadvantage of smelling like fish.