Memory usage by group of processes

While monitoring a http/php server, I needed to do some statistics about php-cgi memory usage.

Playing with memory_limit in PHP, we wanted to know the average memory usage per php-cgi process. This is easily calculated with our best friend awk.

First, get the number of php running processes:

# ps aux | grep php-cgi | grep -v grep | wc -l
126

Then, use awk to calculate the average memory usage for these processes:

# ps aux | grep --exclude=grep php-cgi | grep -v grep | awk 'BEGIN{s=0;}{s=s+$6;}END{print s/126;}'
33987.8

The number used in the calculation is the field RSS given by ps. The ps manual page says:

rss: resident set size, the non-swapped physical memory that a task has used (in kiloBytes)

You can also calculate the total memory used by all php-cgi processes:

# ps aux | grep --exclude=grep php-cgi | grep -v grep | awk 'BEGIN{s=0;}{s=s+$6;}END{print s;}'
4302028

If you need to watch the trend of this average memory usage, a little shell loop does the trick:

# while [ 1 ]; do ps aux | grep --exclude=grep php-cgi | grep -v grep | awk 'BEGIN{s=0;}{s=s+$6;}END{print s/126;}'; sleep 2; done
34401.3
34405.1
34408.4
34409.4
34414.2
34417

This entry was written by CharlyBr, posted on March 13, 2009 at 4:14 pm, filed under Benchmarks, Command line, Monitoring and tagged , , , . Leave a comment or view the discussion at the permalink.

Maintenance mode (HTTP 503) with lighttpd and PHP

When you put your website in maintenance mode, it’s a good idea to return a HTTP 503 error code to the client.

This code indicates that “the server is currently unable to handle the request due to a temporary overloading or maintenance of the server”.

The 503 code is used to avoid crawlers or caching proxy use the maintenance page as the new valid content for the request. You certainly don’t want Google save this content in his search index as the content of your website :)

To achieve this we will use a rewrite rule in lighttpd to redirect all requests to a single PHP script which will return a 503 error code and print an informative message.

Lighttpd configuration

url.rewrite = ( "" => "/maintenance.php" )

This configuration will redirect any request to maintenance.php script.

If you need to serve an image in your maintenance page, you have to add another rule to the rewrite process like that :

url.rewrite = ( "upgrading.png" => "$0",
                "" => "/maintenance.php" )

Let some users see the website

It might be usefull to let admins or developers access the website during the maintenance.
For that, you can disable the maintenance rewrite rule for certain IP addresses :

 $HTTP["remoteip"] != “192.168.1.42″ {
               url.rewrite = ( "upgrading.png" => "$0",
                      "" => "/maintenance.php" )
}

PHP code example

<?php
header("HTTP/1.1 503 Service Unavailable");
?>
We're currently upgrading our servers...

Links

This entry was written by CharlyBr, posted on July 22, 2008 at 2:28 am, filed under http and tagged , . Leave a comment or view the discussion at the permalink.