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.



Installation



  • 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
    wget http://dembol.org/downloads/cband/mod-cband-0.9.7.5.tgz

  • Untar the file
    tar -xzf mod-cband-0.9.7.5.tgz

  • Enter newly created directory
    cd mod-cband-0.9.7.5

  • Add -lm to the APXS OPTS in Makefile.in like so:
    APXS_OPTS=-lm -Wc,-Wall -Wc,-DDST_CLASS=@DST_CLASS@

  • Configure and install
    ./configure
    make
    make install

  • Restart Apache
    /etc/apache2/restart



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

Configuration



  • 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>
    <Location /cband-status-me>
    SetHandler cband-status-me
    </Location>

  • 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/domain.com.scoreboard
    </IfModule>

  • 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:
    http://server/cband-status?xml



Security


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



Credits


http://blog.lael.be/290
http://dembol.org/mod_cband
http://pessetto.com/question2answer/index.php?qa=3&qa_1=why-does-mod_cband-have-a-truncf-symbol-undefined
http://www.howtoforge.com/mod_cband_apache2_bandwidth_quota_throttling
http://www.uno-code.com/?q=node/64
http://www.sourcefiles.org/Internet/WebServers/Modules/Bandwidth/mod-cband-0.9.7.2.tgz.shtml
http://httpd.apache.org/docs/2.0/howto/auth.html

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:
${invoice?.invoiceItems[0]?.name}

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:
${invoice?.invoiceItems.getAt(0).name}


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
null.getAt(0)
The solution is to avoid using the [] operator when accessing lists and to place a ? before every full stop in your statement chain.
${invoice?.invoiceItems?.getAt(0)?.name}


Credits


http://grails.org/doc/latest/guide/6.%20The%20Web%20Layer.html#6.1.6 Data Binding