1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
#!/usr/bin/perl -w
use strict;
use warnings;
use Date::Calc qw( Date_to_Days );
########## VARIABLES ##########
my $choix;
my $EXPREG = q{(\S+)\s+\S*\s+\S*\s+\[(\d+)\/(\D+)\/(\d+):\d+:\d+:\d+\s+\+\d+\] \"(.*) (.*)\" (\S+) (\S+) \"(.*)\" \"(.*)\"};
my $EXPREG1 = q{(\S+)\s+\S*\s+\S*\s+\[(\d+)\/(\D+)\/(\d+):\d+:\d+:\d+\s+\+\d+] \"(.*) (.*)\" (\S+) (\S+) \"(.*)\" \"(.*)\"};
my ($ip,$jour,$mois,$annee,$method,$page,$ret_code,$byte,$referer,$user_a);
my ($lower,$upper,$date);
my ($jourin,$jourout,$moisin,$moisout,$anneein,$anneeout);
my (%haship,%hashpage,%hashbyte,%hashref,%hashua);
###############################
#### TEST FICHIER ET DATE #####
if ($#ARGV != 2){
print ("\nUtilisation : script.pl <fichier_log> <date_debut> <date_fin>\n");
print ("Exemple : script.pl log.txt 20/01/2009 20/02/2009 \n");
exit();
}
my $file = $ARGV[0];
my $datein = $ARGV[1];
my $dateout = $ARGV[2];
if (!(-e $file)){
print $file," existe pas.\n";
exit();
}
if ($datein =~/([0-9]*)\/([0-9]*)\/([0-9]*)/) {
$jourin = $1;
$moisin = $2;
$anneein = $3;
}
else {
print ("Erreur de saisie sur date_debut");
exit();
}
if ($dateout =~/([0-9]*)\/([0-9]*)\/([0-9]*)/) {
$jourout = $1;
$moisout = $2;
$anneeout = $3;
}
else {
print ("Erreur de saisie sur date_fin");
exit();
}
###############################
############ MAIN #############
open (Fichier, "< $file") or die "Ne peux pas ouvrir le fichier $ARGV[0]\n";
while (my $ligne = <Fichier>){
($ip,$jour,$mois,$annee,$method,$page,$ret_code,$byte,$referer,$user_a) = $ligne =~ $EXPREG;
($ip,$jour,$mois,$annee,$method,$page,$ret_code,$byte,$referer,$user_a) = $ligne =~ $EXPREG1;
$mois = '01' if ($mois eq "Jan");
$mois = '02' if ($mois eq "Feb");
$mois = '03' if ($mois eq "Mar");
$mois = '04' if ($mois eq "May");
$mois = '05' if ($mois eq "Apr");
$mois = '06' if ($mois eq "Jun");
$mois = '07' if ($mois eq "Jul");
$mois = '08' if ($mois eq "Aug");
$mois = '09' if ($mois eq "Sep");
$mois = '10' if ($mois eq "Oct");
$mois = '11' if ($mois eq "Nov");
$mois = '12' if ($mois eq "Dec");
$lower = Date_to_Days($anneein,$moisin,$jourin);
$upper = Date_to_Days($anneeout,$moisout,$jourout);
$date = Date_to_Days($annee,$mois,$jour);
if (($date >= $lower) && ($date <= $upper)) {
$haship{$ip} += 1;
$hashpage{$page} += 1;
$hashbyte{$byte} += 1;
$hashref{$referer} += 1;
$hashua{$user_a} += 1;
}
}
&subaffich();
$choix = (<STDIN>);
if ($choix == "1") {
&subip();
}
elsif ($choix == "2") {
&subpage();
}
elsif ($choix == "3") {
&subbyte();
}
elsif ($choix == "4") {
&subref();
}
elsif ($choix == "5") {
&subua();
}
###############################
############# SUB #############
sub subaffich {
system("cls"); # A changer pour prendre en compte clear
print " | Bienvenu dans le programme d\'analyseur de log |\n";
print " | Programmer par Nickname |\n";
print " |-----------------------------------------------------------|\n\n";
print " Veuillez entrer le numero correspondant a l\'action voulue :\n\n";
print " 1.Hits des adresses IP\n";
print " 2.Hits des pages les plus visitees\n";
print " 3.Hits des ko les plus importants\n";
print " 4.Hits des referer les plus presents\n";
print " 5.Hits des user agent les plus presents\n";
}
sub subip {
system("cls"); # A changer pour prendre en compte clear
open(SORTIE, ">log-ip.txt");
foreach $ip ( sort { $haship{$b} <=> $haship{$a} } keys %haship) {
print "IP : $ip a ete rencontre $haship{$ip} fois\n";
print SORTIE "IP : $ip a ete rencontre $haship{$ip} fois\n";
}
print "\nEcriture dans le fichier log-ip.txt en cours...\n";
sleep(1);
print "\nEcriture reussie\n";
print "\n ** A Bientot **\n";
exit;
}
sub subpage {
system("cls"); # A changer pour prendre en compte clear
open(SORTIE, ">log-page.txt");
foreach $page ( sort { $hashpage{$b} <=> $hashpage{$a} } keys %hashpage) {
print "Page : $page a ete rencontre $hashpage{$page} fois\n";
print SORTIE "Page : $page a ete rencontre $hashpage{$page} fois\n";
}
print "\nEcriture dans le fichier log-page.txt en cours...\n";
sleep(1);
print "\nEcriture reussie\n";
print "\n ** A Bientot **\n";
exit;
}
sub subbyte {
system("cls"); # A changer pour prendre en compte clear
open(SORTIE, ">log-byte.txt");
foreach $byte ( sort { $hashbyte{$b} <=> $hashbyte{$a} } keys %hashbyte) {
print "Kilo-octets : $byte ko - $hashbyte{$byte} fois\n";
print SORTIE "Kilo-octets : $byte ko - $hashbyte{$byte} fois\n";
}
print "\nEcriture dans le fichier log-byte.txt en cours...\n";
sleep(1);
print "\nEcriture reussie\n";
print "\n ** A Bientot **\n";
exit;
}
sub subref {
system("cls"); # A changer pour prendre en compte clear
open(SORTIE, ">log-referer.txt");
foreach $referer ( sort { $hashref{$b} <=> $hashref{$a} } keys %hashref) {
print "Referer : $referer a ete rencontre $hashref{$referer} fois\n";
print SORTIE "Referer : $referer a ete rencontre $hashref{$referer} fois\n";
}
print "\nEcriture dans le fichier log-referer.txt en cours...\n";
sleep(1);
print "\nEcriture reussie\n";
print "\n ** A Bientot **\n";
exit;
}
sub subua {
system("cls"); # A changer pour prendre en compte clear
open(SORTIE, ">log-user_agent.txt");
foreach $user_a ( sort { $hashua{$b} <=> $hashua{$a} } keys %hashua) {
print "User agent : $user_a a ete rencontre $hashua{$user_a} fois\n";
print SORTIE "User agent : $user_a a ete rencontre $hashua{$user_a} fois\n";
}
print "\nEcriture dans le fichier log-user-_agent.txt en cours...\n";
sleep(1);
print "\nEcriture reussie\n";
print "\n ** A Bientot **\n";
exit;
}
#############################
close(Fichier); |
Partager