Monday, February 13, 2012

Enabling SSL on Apache2 for Testing

Enabling SSL on an apache2 installation is easy. There are plenty of instructions on the Web for doing this, but I thought I'd describe the way to do it using the latest Ubuntu installation of apache2, which is idiosyncratic.

  1. First you need to generate a self-signed certificate. I used the following command:
    openssl req -new -x509 -nodes -out server.crt -keyout server.key
    Now create a directory for these files inside your apache2 installation:
    sudo mkdir /etc/apache2/certs/
    And move the certificates to that location:
    sudo mv server.* /etc/apache2/certs/
  2. Next, edit /etc/apache2/sites-available/default-ssl, and change the two directives:
    SSLCertificateFile    /etc/apache2/certs/server.crt
    SSLCertificateKeyFile /etc/apache2/certs/server.key
    So they now point to your files.
  3. Now enable the ssl module in apache2, and the default ssl site:
    sudo a2enmod ssl
    sudo a2ensite default-ssl
  4. Finally, restart apache2:
    /etc/init.d/apache2 restart
    And it should work. Test it by going to https://localhost in the browser. It should give you a dialog complaining about how insecure this is. Just say that you understand the risks, enable the exception, and it will take you to the index.html page.

Setting up Joomla! as a test website

Joomla! has quite a lot of sample data that is useful for HTTP stress-testing. It provides a variety of resources that can be parsed and retrieved by the http_bot of botloader. Setup is easy, but I thought I'd put it on record so people can follow the installation quickly.

  1. On Ubuntu and other versions of Linux you'll need to install Mysql, php5 and apache2. To get the php to work you'll need the apache php module (libapache2-mod-php5). For the mysql you'll need to install both the server and client. When you install mysql server it asks for a user name and password. Use "root" and give any password, but remember it, because you'll need it later.
  2. Now download Joomla!. Unzip the files and rename the directory to "joomla". Now copy the joomla directory to /var/www or wherever your web-root is located:
    cp -r joomla /var/www
    Now make sure that the installer can modify the joomla directory. cd into /var/www and type:
    sudo chown -R www-data joomla
    At least on Ubuntu 'www-data' is the name of the user who is running apache. Or you can use chmod -R +w joomla if you prefer, but that's a lot less secure, though it doesn't matter on a local testbed.
  3. Now edit index.html, which you'll find in /var/www, and add a line somewhere in the body of the HTML:
    <p>Why not visit our wonderful <a href="/joomla/">Joomla! site</a>?</p>
    This provides a link into the main sample data which http_bot will follow when attacking the site. Otherwise it will only find index.html, and keep downloading that - pretty ineffective. So this step is important
  4. Now run the Joomla! installer. It's located at http://localhost/joomla/. Click through the pages, making sure that it detects Mysql.
    • If it says that the installation directory is unwritable, try the chmod -R +w joomla command from /var/www.
    • If it says that mysql is undetectable you need to install something - check that you have the mysql plugin for Apache.
    • When it asks for the mysql username and password give the ones you specified above in step 1.
    • When it asks if you want to install sample data, say YES.
    • For the rest, just follow the suggested options
  5. Now test the installation. Navigate to http://localhost, click on the link you created earlier and make sure that the website is all working. If it says "downloading" when you access a php page, you must have failed to install php5 correctly.

Sunday, February 12, 2012

Measure CPU usage on Windows with PDH

On Windows to measure the CPU usage you need to use the Performance Data Helper (PDH). Microsoft distribute a dll (pdh.dll). Although not ideal you can link with this. Pdh.lib would be better but I have no idea where to get it. I'm going to describe how to measure total CPU usage using pdh.dll, and three headers, windows.h, pdh.h and pdhmsg.h. The latter two come with the Microsoft SDK. The tools I used were the MinGW tool set in NetBeans.

You have to do five things:

  1. Create a query using PdhOpenQuery.
  2. Add a counter to it via PdhAddCounter
  3. Call PdhCollectQueryData twice on the query, sleeping in between
  4. Call PdhGetFormattedCounterValue on the counter
  5. Close the query

Now you should have a formatted data value you can display using printf or whatever. Here's a minimal program that does it (proper error-handling is left as an exercise for the reader):

PDH_STATUS status = PdhOpenQuery(NULL,(DWORD_PTR)NULL,&query);
if (status==ERROR_SUCCESS )
  HCOUNTER hCounter;
  status = PdhAddCounter( query, 
    "\Processor(_Total)\% Processor Time", 0, &hCounter );
    if (status==ERROR_SUCCESS)
      status = PdhCollectQueryData(query);
      if (status==ERROR_SUCCESS)
        status = PdhCollectQueryData(query);
        DWORD ret;
        status = PdhGetFormattedCounterValue(hCounter, 
        if (status==ERROR_SUCCESS)
          printf("CPU Total usage: %2.2f\n",value.doubleValue);
    PdhCloseQuery( query );