This page was last modified: July 28 2006 16:03:26   
Too Cool for Internet Explorer

Monitoring Apache activity

For a very long time, I struggled to find out why Apache some times had very high CPU usage. I went through my logfiles and many people tried to help, but nothing seemed to reveal the actual problem. As you can see, the load was pretty serious:

PID  USERNAME PRI NICE SIZE    RES    STATE TIME WCPU   CPU    COMMAND
2165 www      126   0  17392K  11956K RUN   9:44 49.02% 49.02% httpd
2171 www      126   0  15292K   9884K RUN   2:27 49.02% 49.02% httpd

What I needed, was some way to relate the process ID (PID) to the name of the script running. Then I almost accidently stumbled over the apache module mod_status. That is really a very nice tool... and extremely easy to use - thank you KarKomaOnline for showing the way.

mod_status shows exactly what is going on at the moment, and it is perfect for finding those hanging scripts consuming your CPU:

Open the apache configuration file (/usr/local/etc/apache/httpd.conf) and uncomment these lines:

ExtendedStatus On

...

<Location /server-status>
  SetHandler server-status
  Order deny,allow
  Deny from all
  Allow from your.ip.address.here
</Location>

Replace your.ip.address.here to make sure that only you can see this information.

.. and remember apache must be restarted to make the changes take effect:

apachectl stop
apachectl start

Then open your favorite browser, and go to:

http://your.server.here/server-status

You can also make the page refresh with an interval given in seconds:

http://your.server.here/server-status?refresh=60

...or make the output available to your scripts...

lynx -dump "http://server.ip.address.here/server-status?auto"

The output from mod_staus is pretty self-explanatory ... and you can now see exactly what those CPU consuming pids are doing. Happy monitoring :-)