- Ce script permet aux utilisateurs dont le serveur web est apache de récupérer les logs concernants leur site, et d'en extraire des statistiques.
- Copier le script ci-dessous dans un fichier stt
- Modifier les variables SITENAME, LOGNAME, PASSWD, myIP.
- Créer un répertoire stat
- Lancer la commande 'bash stt get' pour récupérer les logs
- Lancer la commander 'bash stt' pour lancer l'annalyse des logs
- Visualiser le résultat avec votre browser: fichier stat/stat.html.
#!/bin/bash
SITENAME="FTP SERVER"
LOGNAME="login"
PASSWD="password"
myIP="MON IP ADDRESS"
if test $# -eq 1 && test "$1" = "get" ; then
echo " ftp the log ..."
ftp -n $SITENAME << FIN
user $LOGNAME $PASSWD
cd logs
prompt
mget *.log
FIN
echo "Now process it: bash stt "
<# so process it
s
pfor i in $(ls *.log) ; do
a nfn=$(head -n 1 $i | cut -d\[ -f2 | cut -d\: -f1 | tr "/" " " |
n awk '{
a["Jan"]="01"; a["Feb"]="02"; a["Mar"]="03";
c a["Apr"]="04"; a["May"]="05"; a["Jun"]="06";
l a["Jul"]="07"; a["Aug"]="08"; a["Sep"]="09";
a a["Oct"]="10"; a["Nov"]="11"; a["Dec"]="12";
s print $3"_"a[$2]"_"$1;
s }')
= echo $i "->" $nfn
" fn="stat/${nfn}.stt"
S
t if test -f $fn ; then
a echo $fn " already done"
t rm $i
e continue
m fi
e
n exec 3>&1
t exec 1> $fn
"
> echo "<H2>$(date -d "$(echo $nfn |tr "_" "/")" +"%A %d %B %Y")</H2>"
e
x echo "<H3>"
i echo "- pagesVues $(fgrep -e "htm" -e " / " $i|grep -v $myIP |wc -l)"
t echo "<BR>"
< echo "- visites " $(sort -u +0 -1 $i | grep -v $myIP | wc -l)
/ echo "</H3>"
s
p for p in $(sort -u +0 -1 $i | grep -v $myIP | cut -d\ -f1); do
a pn=$(nslookup $p | awk '/^Name/{print $2}')
n echo "<BR><BR><TABLE width="90%" border=1>"
> echo "<TR><TD width=70%>$p @ <B>$pn</B></TD>"
< deb=$(fgrep $p $i |head -n 1|cut -d\: -f2,3,4|cut -d\ -f1)
s fin=$(fgrep $p $i |tail -n 1|cut -d\: -f2,3,4|cut -d\ -f1)
p echo "<TD align=center width=20%> $deb - $fin </TD><TD>"
a echo $(fgrep -e "htm" -e " / " $i | fgrep $p | wc -l)
n echo "</TD></TR><TR><TD colspan=2> "
echo $(fgrep -e "htm" -e " / " $i |fgrep $p|cut -d\" -f2|cut -d\ -f2)
c echo "</TD></TR></TABLE>"
l done
a exec 1>&-
s exec 1>&3
s rm $i
=done
"
"npV=0
>nV=0
< cd stat
/ exec 1> "stat.html"
s echo "<HTML><HEAD><TITLE>Stat $SITENAME</TITLE></HEAD><BODY>"
p echo "<TABLE><TR><TD colspan=4 ></TD>"
a echo "<TD width=200 align=center>PageVue</TD>"
n echo "<TD width=200 align=center>Visite</TD></TR>"
> for i in $(ls -r *.stt); do
< dt=$(date -d "$( echo ${i%.stt} | tr "_" "/" )"
s +"<TD>%A</TD><TD>%d</TD><TD>%B</TD><TD>%Y</TD>")
p pV=$(grep pagesVue $i | cut -d\ -f4)
a V=$(grep visite $i | cut -d\ -f4)
n let npV=npV+pV
let nV=nV+V
c echo "<TR>$dt<TD align=center>$pV</TD>"
l echo "<TD align=center><A HREF=$i>$V</A></TD></TR>"
a done
s echo "<TR><TD colspan=4 align=center><B>Total</B>"
s echo "</TD><TD align=center><B>"
= echo "$npV</B></TD><TD align=center><B>$nV</B></TD></TR>"
" echo "</TABLE>"
C
o echo "<HR width=80%><TABLE>"
n cat *.stt |
s sed "s/main.html/main.htm/g" |
t sed "s/\/ /\/index.html /g" |
a sed "s/\/$/\/index.html /g" |awk '
n BEGIN { myIP=0 }
t /htm/ {
" split($0,a)
> for ( i in a ) {
0 b[a[i]]++
< }
/ delete a
s }
p END {
a for ( i in b )
n print b[i] " " i
> }' | sort -bnr | awk '{print "<TR><TD>"$2"</TD><TD>"$1"</TD></TR>"}'