Advise for parsing Apache-logs to display
I was just wondering what would be the better way to show a graph of '# of visitors' per month/week.
开发者_C百科1: Write a few functions that go off and parse apaches logs then returns an array and converts it into a graph.
2: cronjobs run at night and insert the log files into a mysql db then when the 'client' requests to see a graph of the visitors per month/week, sends query to mysql and returns and graphs.
With #1 I first thought this would be a good idea but then began to think about the toll on the server plus it seems that if a user refreshed the page the whole process would start over when the data would more-or-less be the same(Wasting processor/memory time)
With #2, I think this is the better idea or the two but was wondering if anyone else did something similar and if so how did it go.
Any advise would be appreciated.
Thanks.
If you have a database handy, there's no reason not to use it. You can parse up to say, one second prior to start of script, store that time, and start again from there the next go-around. You can get the cron to run as quickly as every minute with very little server impact that way.
Further, in languages like Python and Perl, you can run an infinite loop on readline() / readline, and it will keep returning either an empty string or the net line as soon as one exists. Add a short sleep every time you see an empty line and you can have realtime updates with a long-lived process, without the overhead of constant seeks and parses. Naturally you might want to have a cron that tests if they're alive and revives them if not.
I can provide code if you like.
精彩评论