Jump to content
php.lv forumi

Apache log to mysql


Recommended Posts

Labdien!

Es lietoju tādu progresīvu web serveri, kā Apache :-)

Bet, man nav ieslēgts nekāds logrotate, ne kas cits, kas sadalītu logus. Viena lieta gan ir, katram virtuālajam hostam ir savs error un access logs.

Ar šo sistēmu šobrīd neesmu apmierināts, jo daži access logi ir izauguši vairākus simtus megabaitus lieli, tas man pavisam nepatīk.

Varētu jau laist logrotate un dalīt pa dienām tos logus, bet tomēr vairāk mani vilina šāds risinājums: katru nakti palaižas logrotate, pēc tam kaut kāds skripts savāc visus vecos log failus, izbrauc tiem cauri un visu sainserto mysql datubāzē. Attiecīgi pēc tam ir skripts, kas to visu attēlo smuku grafiku veidā.

 

Jautājums ir tāds, vai kādam nav zināmi skripti, kas no log faila insertotu iekš sql un arī statistikas attēlošanas skripti, kas to darītu no sql?

Link to comment
Share on other sites

neesmu nekad domaajis par taada veida logoshanu, bet peec idejas jau nekas labaaks nebuutu, jo varbuut vizuaali vienkaarshaak apskatiities faila izmeeru ne datubaazes (kaut gan tie pashi fail ivien ir), bet datubaazes buus lielaaks...

 

galu galaa ir speciaali logu analizeetaaji, kas to visu dara bez datu baazju starpnieciibas. kaapeec jaasarezjgja dziive?

Link to comment
Share on other sites

Kur ir problēma? Katru nakti tev palaižas logrotate un uzģenerē vajadzīgos logus. Kādu laiciņu pēc logrotate darbības palaid cron skripiņu, kurš nolasa uzģenerēto log failu, sasplito pa tev vajadzīgajiem laukiem un iemet bāzē. Pēc tam uzraksti nelielus skripiņus, kuri no bāzes vilks laukā tev vajadzīgo informāciju

Link to comment
Share on other sites

Kāpēc tu domā, ka es domāju, ka nelietojat to ko var atrast guuglee? :o

Es gribēju zināt, ko no tā visa lieto!

25094[/snapback]

 

Kādu gadu atpakaļ uzcepu šādu brīnumu:

http://paste.php.lv/2698

 

Var paspēlēties un paparsēt logus.

 

What is Logparser class?

This class was written for parsing apache log files. In general, all it does is parses the log file and counts the number of lines parsed and amount of traffic that has been transfered. There are no statistics features added, as almost each and every programmer needs his own functions, calculations and general approach for building statistics.

 

Where can you use this class?

For example, you can build a statistics system on MySQL or any other type of database and don't count the statistics live, but parse the apache log file once a day and add all the records to database.

 

What about incremental log file parsing?

This class does not limit you in any way - it just knows how to parse the records in log file. How you read the file, it's up to you. By the way, if you are interested, I can provide you with an example how to parse the log files incrementally. All you need to do is to store the parsed offset in let's say text file, and when you resume the parsing, just skip the bytes that are before the offset border:

 

 

<?php
   // function for checking parse time
   function microtime_float(){
       list($usec, $sec) = explode(" ", microtime());
       return ((float)$usec + (float)$sec);
   }

   $time_start = microtime_float();

   // set time limit to infinite, as we don't know how long will it take to parse the file
   set_time_limit(0);
   require_once('logparser.php');
   $logParser = new ApacheLogParser();
   
   // log file
   $fp = fopen('/var/www/html/ltv/logs/access.log', 'r');

   // if offset is saved, read it from file, if not, offset is 0
   if (file_exists('offset.txt')){
       $offset = (int)file_get_contents('offset.txt');
   }else{
       $offset = 0;
   }

   // incremental log parsing
   fseek($fp, $offset);
   while ($data = fgets($fp, 4096)){
       $logInfo = $logParser -> parse($data);
       
       // perform the needed actions with $logInfo array
   }
   
   
   // save the current offset and save to offset.txt
   $pos = ftell($fp);
   $fp2 = fopen('offset.txt', 'w');
   fwrite($fp2, $pos);
   fclose($fp2);
   fclose($fp);
   
   $time_end = microtime_float();
   $time = $time_end - $time_start;

   // output some info
   echo "<p>Parsed {$logParser -> rowsParsed} rows in $time seconds.</p>";
   echo '<p>Total ' . $logParser -> bytesTransfered . ' bytes have been transfered!</p>';
?>

 

 

What about speed?

From the tests I've done, the speed was about 3000 to 4000 lines of log file per second. I know this is not as fast as webalizer or awstats, but if you parse the log files regularly, it's enough for a medium size web site.

Link to comment
Share on other sites

×
×
  • Create New...