Wednesday, December 7, 2011

Installing and configuring Cband for Apache2 on Ubuntu

If you need to manage bandwidth usage on Apache web server (particularly on a per virtualhost basis) CBand could be that hero you were hoping for. Below are the steps I followed to get up and running with a basic CBand setup.


  • Install additional apache libraries. Only one of these two maybe required.
    apt-get install apache2-prefork-dev
    apt-get install apache2-threaded-dev

  • Download the module

  • Untar the file
    tar -xzf mod-cband-

  • Enter newly created directory
    cd mod-cband-

  • Add -lm to the APXS OPTS in like so:

  • Configure and install
    make install

  • Restart Apache

Hopefully you should now have successfully installed Cband on your Ubuntu server.
Now let's configure it.


  • In order to improve performance of Cband, add the following to /etc/apache2/mods-available/cband.conf
    CBandScoreFlushPeriod 1
    CBandRandomPulse On

  • Add sym link in mods-enabled folder
    ln -s ../mods-available/cband.conf cband.conf

  • Make directory to store the accumulated data
    mkdir /var/www/scoreboard

  • Change ownership of directory to the apache user
    chown www-data:www-data /var/www/scoreboard/

  • Configure access to cband-status. Cband provides a status page, which you must activate via the apache configuration. It is likely you should restrict access to this page by some means. For unrestricted access to this page add the following to cband.conf
    <Location /cband-status>
    SetHandler cband-status
    <Location /cband-status-me>
    SetHandler cband-status-me

  • In your browser, navigate to http://server/cband-status and observe your new cband status page

  • Now add cband configuration for one or more virtual hosts, restart apache, and refresh your cband status page
    <IfModule mod_cband.c>
    CBandLimit 6G
    CBandPeriod 4W
    CBandPeriodSlice 1W
    CBandSpeed 500kbps 10 30
    CBandExceededSpeed 128kbps 5 15
    CBandScoreboard /var/www/scoreboard/

  • If you would like to do something more with all the data Cband is collecting you can retrieve an XML representation by using a url of the form:


You don't want to let just anyone find intimate details about a site your hosting. You need to password-protect that data!

  • One of the simplest ways to password-protect is using the http auth module of apache. To get started on this path, reopen the file /etc/apache2/mods-available/cband.conf

  • Add the following lines to both of your Location elements replacing knownSystemUser appropriately:
    AuthType Basic
    AuthName "Restricted"
    AuthUserFile /usr/local/apache/passwd/passwords
    Require user knownSystemUser

  • Now create a password for the specified user in the specified file
    mkdir -p /usr/local/apache/passwd/passwords
    htpasswd -c /usr/local/apache/passwd/passwords knownSystemUser


Monday, December 5, 2011

Groovy safe navigation gotcha

I just came across this Groovy gotcha today while accessing a list within a chain of attributes. I had a statement like this in a gsp:

This is the (erroneous) recommended style when data binding many ended associations in Grails, but it could easily arise in other scenarios. If I try this "safe" statement where invoiceItems is null, I'll see a null pointer exception. In fact I'll even see it when invoice is null. The reason for the exception is a combination of how lists and the safe navigation work in Groovy.

Lists are much more elegant in Groovy than Java, as they share the square brackets that primitive arrays use. Creating a list is as easy as writing:
def x = [1,2,3]

Just like an array you can access the first element of this list like so:
println x[0]

However, unlike a primitive array the [] operator on a list is mapped (by some Groovy magic) behind the scenes to the getAt method of the particular List object. We can expand our example as follows:

Safe navigation is a beautiful, and elegant means of protecting your code for scenarios where a variable could be null. It doesn't work as you might expect though. In my example above if invoice is null you might expect that after discovering invoice to be null execution would move to the next statement. Instead what happens is that each part of the chain is executed in turn, and the ? is equivalent to a try/catch block, where if a null pointer exception is caught, the exception will be swallowed, and null returned.

The end result of these two Groovy behaviors is that in my example (and indeed the examples in the grails documentation) we will see the attempted execution of
The solution is to avoid using the [] operator when accessing lists and to place a ? before every full stop in your statement chain.

Credits Data Binding