Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Knowledgeable monks,

Is there a way of making a small Perl script execute every time an Apache server is accessed, perhaps using mod_perl or something similar? The script I have in mind (in fact I've already created it) logs the number of users currently "using" the entire server, similar to the "Criminals Online -> Network:" counter on this GTA fan site (it's at the bottom of the left menu). However, I'm not sure how to make this script run every time the server is accessed.

I've tried all kinds of long and drawn-out approaches, such as using mod_perl and SetHandler/PerlHandler, and with this method the script will be run but no data will be output because the original request is not completed after the script has finished executing.

If anyone has any ideas for implementing this, I'd really appreciate their input. If I've been horribly vague and not detailed enough, please let me know and I'll elaborate some more.

Thanks in advance!
  • Comment on Executing a Perl script every time a server is accessed?

Replies are listed 'Best First'.
Re: Executing a Perl script every time a server is accessed?
by merlyn (Sage) on Mar 26, 2005 at 16:32 UTC
    What is a "user"? What is "current"? I think you'll find both of those are ultimately meaningless. So you might as well just put a random number up there.

    A user is not an IP address. AOL proxies show many hits from different IP addresses even in the same session. NAT-ted boxes all appear to be the same IP address. So unless you have an explict "login", you don't have knowledge of a "user".

    And as for "current", a web session doesn't really exist, since what you really have is a bunch of independent hits.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: Executing a Perl script every time a server is accessed?
by Zaxo (Archbishop) on Mar 26, 2005 at 16:43 UTC

    You can set that up in your httpd configuration:

    Action do-my-thing /cgi-bin/myscript.pl AddHandler do-my-thing

    As a handler, your script becomes responsible for directing further actions. For what you're doing, it may be preferable to set this up as an output filter, rather than a handler.

    That's from the Apache 2.0 manual, but I think it's the same for 1.3. A good read of the manual would be in order, there are lots of possibilities and gotchas.

    After Compline,
    Zaxo

Re: Executing a Perl script every time a server is accessed?
by jhourcle (Prior) on Mar 26, 2005 at 17:21 UTC

    You can get Apache 2 to run an extra script on every input with the SetInputFilter directive, so it runs, but you don't completely usurp control, but like merlyn said, knowing what qualifies as a 'login' and 'session' is a while 'nother thing.

Re: Executing a Perl script every time a server is accessed?
by BUU (Prior) on Mar 26, 2005 at 21:48 UTC
    With the above caveats about "what the hell is a user anyways", why not approach it from a slightly different direction: tail the log file. Write a small daemon that constantly tails the apache log file (preferably just the one for the virtual host if there is one) and updates modifies a counter everytime it sees a new line in the file. You'll probably have to filter out multiple image requests on the same page, but it should give you an idea and be easier to setup.