Re: mailserver logfile analyser

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



woldhekkie wrote:
> Our mailserver writes a logfile every day, but it takes hours to analyse
> it
> manualy.
>
> I (newby) am trying to write a php script that reads the the logfile line
> by
> line and try to get a top 50 of spammers/clients that mail too much...
>
> I can get line by line all information like email-adres, from , to , bytes
> send , but how to store it in a array ?
> If a line is read and the emailadres already pressent in the array, it
> should not ad this emailadress again, but add a counter +1
>
> In the end i want an array containing all emailadreses and how often they
> were counted.
>
> This is how far i get: (sorry, it is in Dutch)
> It shows all from and to emailadresses. Not bad for a newby, but it took
> one
> week to do.....
>
> <?php
> $fp="";$line="";$aantal=0;

$emails = array(); //maybe use $froms = array(); $tos = array();

> echo "<TABLE BORDER=3>";
> echo "<TR><TD ALIGN=\"center\"> From:</TD><TD
> ALIGN=\"center\">To:</TD></TR>";
> if($fp = fopen("C:\smtp\SMTP-Activity-050226.log","r")) {
>     while (!feof ($fp)) {
>        $line = fgets($fp, 4096);
>        if(strstr($line,"SMTP-IN")) {
>           if(strstr($line,"MAIL FROM" )) {
>           $aantal++;
>           $start=strpos($line,"<");
>           $end=strpos($line,">");
>           $from=str_replace(">", " ", str_replace("<", " ",
> substr($line,$start,$end-$start)));

$emails[$from]++; //maybe use $froms[$email]++;

//Fancier version:
$emails[$from] = isset($emails[$from]) ? $emails[$from] + 1 : 1;

>           echo "<TR><TD>".$from."</TD>";
>                     }
>           if(strstr($line,"RCPT TO" )) {
>           $start=strpos($line,"<");
>           $end=strpos($line,">");

//Same pattern as above
$to = str_replace(">", " ", str_replace("<", " ",
substr($line,$start,$end-$start)));
$emails[$to]++;

>           echo "<TD>" . str_replace(">", " ", str_replace("<", " ",
> substr($line,$start,$end-$start)))."</TD>";
>           }
>           }
>           }
> echo "</TR></TABLE>";
> fclose($fp);
>           }
> echo "Totaal aantal:  $aantal <BR>";

echo "Top 10:<table>";
arsort($emails);
reset($emails);
for ($i = 0; $i < 10; $i++){
  list($email, $count) = each($emails);
  echo "<tr><td align='right'>$email</td><td
align='right'>$count</td></tr>\n";
}
echo "</table>";

//If you want $froms/$tos separate, copy/paste code above and do it for both

> ?>

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux