#!/usr/bin/perl
#
# Dette scriptet brukes av appleten som viser "current connections".
# Det ser på de oppslagene som har vært de siste 2 minuttene.
# Gjør om ip adressene til koordinater på verdenskartet, og skriver dem ut.
#
#                                                 K.A.Mork & A.Vonheim

$MAX_HOSTS       = 10;
$TAIL_PATH       = '/usr/bin/tail';
$CUT_PATH        = '/usr/bin/cut';
$ACCESS_LOG_PATH = '/var/lib/httpd/logs/almashriq/access_log';
$TAIL_SIZE       = 300;

# Landskoder mappet til koordinater
$domains{'is'}  = "252,71";
$domains{'ie'}  = "268,99";
$domains{'dk'}  = "296,92";
$domains{'be'}  = "290,104";
$domains{'nl'}  = "291,99";
$domains{'lu'}  = "292,106";
$domains{'ch'}  = "295,111";
$domains{'at'}  = "305,110";
$domains{'pl'}  = "313,110";
$domains{'cz'}  = "309,106";
$domains{'sk'}  = "313,108";
$domains{'ru'}  = "345,93";
$domains{'gr'}  = "318,125";
$domains{'tr'}  = "334,126";
$domains{'za'}  = "318,244";
$domains{'eg'}  = "330,147";
$domains{'il'}  = "339,139";
$domains{'sa'}  = "358,155";
$domains{'mx'}  = "122,162";
$domains{'gt'}  = "136,170";
$domains{'bz'}  = "141,146";
$domains{'hn'}  = "146,171";
$domains{'pa'}  = "153,179";
$domains{'co'}  = "164,187";
$domains{'cr'}  = "177,181";
$domains{'br'}  = "211,213";
$domains{'ar'}  = "181,252";
$domains{'cl'}  = "165,254";
$domains{'pe'}  = "162,254";
$domains{'bo'}  = "178,220";
$domains{'cu'}  = "151,156";
$domains{'jm'}  = "165,161";
$domains{'nz'}  = "564,258";
$domains{'pg'}  = "514,204";
$domains{'id'}  = "460,205";
$domains{'sv'}  = "156,196";
$domains{'sg'}  = "449,192";
$domains{'my'}  = "477,186";
$domains{'th'}  = "445,169";
$domains{'in'}  = "407,158";
$domains{'ph'}  = "480,175";
$domains{'hk'}  = "467,154";
$domains{'mk'}  = "463,155";
$domains{'cn'}  = "465,140";
$domains{'jp'}  = "505,131";
$domains{'kr'}  = "438,133";
$domains{'kp'}  = "485,126";
$domains{'gl'}  = "211,60";
$domains{'pk'}  = "390,148";
$domains{'af'}  = "386,136";
$domains{'ir'}  = "370,141";
$domains{'iq'}  = "354,140";
$domains{'sy'}  = "344,134";
$domains{'lb'}  = "340,137";
$domains{'jo'}  = "143,139";
$domains{'ma'}  = "272,139";
$domains{'ly'}  = "312,146";
$domains{'lk'}  = "412,181";
$domains{'kw'}  = "385,145";
$domains{'us'}  = "120,128";
$domains{'ca'}  = "103,83";
$domains{'no'}  = "296,81";
$domains{'uk'}  = "279,100";
$domains{'fr'}  = "286,112";
$domains{'es'}  = "276,125";
$domains{'pt'}  = "268,126";
$domains{'de'}  = "297,103";
$domains{'au'}  = "500,238";
$domains{'it'}  = "306,124";
$domains{'se'}  = "306,82";
$domains{'fi'}  = "324,76";

$domains{'com'}  = "120,128";
$domains{'net'}  = "120,128";
$domains{'org'}  = "120,128";


print "Content-type:  text/html\n\n";


$remote_host = $ENV{'REMOTE_HOST'};

$allowed_diff = "120";    # 2 minutes;

$real_time = `date +%T`;
$real_time =~ /([0-9][0-9]):([0-9][0-9]):([0-9][0-9])/;
$num_seconds = ($1 * 3600) + ($2 * 60) + ($3);

#Ser på klokka
$real_date = `date +%d%h%Y`;
$real_date =~ /([0-9]*)([a-zA-Z]*)([0-9]*)/;
$real_date = "$1/$2/$3";

#Trekker ut de siste linjene fra access_log
@tail_org = `$TAIL_PATH -$TAIL_SIZE $ACCESS_LOG_PATH`; 

#Ser på tidstemplene i loggen
$i = -1;
foreach $line (@tail_org)
{
    $i++;
    $line =~ /\[([0-9]*\/[a-zA-Z]*\/[0-9]*):([0-9][0-9]):([0-9][0-9]):([0-9][0-9])/;
    $date[$i] = "$1";
    $time[$i] = ($2 * 3600) + ($3 * 60) + $4;
}

$i = -1;
while (<@time>)
{
    $i++;
    if ($_ > $num_seconds - $allowed_diff)
    {
	if ($date[$i] eq $real_date)
	{
	    $tail_org[$i] =~ /^([^ ]*) /;
	    $tail[$i] = "$1\n";
	}
    }
}

# Trekk ut adressen til de 300 siste requestene.
#@tail = `cat /home/almash/admin/statistics/map_access_log | $CUT_PATH -d' ' -f1`;

# Trekk ut $MAX_HOSTS _unike_ internett adresser, start bakfra.
foreach $host (reverse @tail) {
	chomp $host;
	next if( $host =~ $remote_host);   # Fjern appletes IP fra lista

	last if( $#new > ($MAX_HOSTS-2) ); # Avslutt
	#print $host;
	if( not grep /^$host$/, @new ) {
		push @new, $host;
	}
}


# Fjerner alt foer topp-domenet.  (www.hiof.no -> no)
foreach (@new) {
	( $_ ) = ( $_ =~ /([^\.]+)$/ );
}


# Summer fra hvert domene
foreach (@new) {
        $_='us' if( $_ =~ /(edu|gov|mil)/i ); #Gjør om edu,gov,mil -> US
	$counter{$_}++;
}

foreach $domain (keys %counter) {
    if( $domains{$domain} ) {
	($x,$y) = split /,/ , $domains{$domain};
	print "$x\n$y\n";
    }
    else {
	print "95\n275\n"; # Indikerer ukjent topp domene
    }
    print $counter{$domain}."\n";
}
