#!/usr/bin/perl
#use strict;
use Socket;

my ($num,$cnt,$name);

format LINEFORM =
@<<<<< : @<<<<<<<<<<<<<<  @*
$cnt,$num,$name
.

my %nums;
my @filelist=(
"/etc/httpd/logs/access_log",
"/etc/httpd/logs/access_log.1",
"/etc/httpd/logs/access_log.2",
"/etc/httpd/logs/access_log.3",
"/etc/httpd/logs/access_log.4");

foreach my $file (@filelist){
open (FILE,$file)or die("can't open $file");
print "\nAnalyzing File: $file\n";
my $icnt=0;
while(<FILE>){
	m/^(\d+\.\d+\.\d+\.\d+) .*$/;
	if ($icnt eq "1000"){
		print ".";
		$icnt=0;
		}
	else{
	$icnt=$icnt+1;
	}
	if ($nums{"$1"} eq undef){
		$nums{"$1"}=1;
		}
	else{
		$nums{"$1"}=$nums{"$1"}+1;
		}
	}
}
print "\n";

my @cnts=keys(%nums);
@cnts=sort {$nums{$b} <=> $nums{$a}}(@cnts);

foreach $num (@cnts){
	$cnt=$nums{"$num"};
	$name=ip2name($num);
	$~ = "LINEFORM";
	write (STDOUT);
	#print "$cnt\t: $num \t$name\n";
}

sub ip2name
{
    my $ad=$_[0] or return("undefined");
    return (gethostbyaddr(pack('C4', split(/\./, $ad)), AF_INET) or $ad);
}
