Friday, November 2, 2012

AWS Cloudwatch Custom Metric for Apache Workers

Our goal: Create two custom metrics to store in Cloudwatch the value "Busy Workers" and "Idle Workers" from our Apache HTTP server.
With this simple example we will learn how to use Cloudwatch to monitor any kind of information generated inside our EC2 instance. Cloudwatch is ready to monitor metrics that can be obtained from the outside (CPU, Network and EBS I/O) but it needs some help to get into our machine.

We need:
- Apache HTTP server installed and running with the module mod_status installed and activated. Here the mod_status documentation.
- Previous knowledge to interact with AWS Cloudwatch monitoring API and a working AWS_CREDENTIAL_FILE. You can use this previous article for help.

Test mod_status configuration:
- Access to the "Apache Server Status" page using a browser. URL: http://(you-web-server-public-DNS)/server-status and you should see something like this:

First, we load all the environment variables that could be used in the exercise (and others). We expect to use this script with crontab so the more the better. Then we load SERVER with the instance name obtained from EC2 Instance Metadata to identify this metric into the Cloudwatch cloud. Now we load BUSYWORKERS and IDLEWORKERS with its values obtained from Apache mod_status using a wget call. The command mon-put-datadoes the magic to insert those two metrics into Cloudwatch.

Save the script and execute it 3 or 4 times from command line to be sure that it works and to create some results to play with. At your system log you should see something like this:

Now we can go to locate our new metrics in Cloudwatch. Open Cloudwatch console. On the left menu "Navigation" click "All Metrics". Then on "Metrics Viewing" select on the pull-down "EC2: HTTPD:IntanceId". Two metrics are shown then. Under your EC2 Instace ID should be present httpd-busyworkers and httpd-idleworkers. Select both metrics and Cloudwatch should create a graphic similar to this:

Note: New metrics in Cloudwatch could take some minutes to consolidate and to be present on the console. If after a while they do not appear, close your AWS Console, open it again and notice whether the value "Cloud Metrics Available" at your Cloudwatch Console Dashboard has increased. Also note that by default all the API interaction are directed to the us-east-1 Region (N.Virginia). Be sure you have selected that region on your console.

With our script tested we could easily create a crontab entry to execute it every 5 minutes. Create a crontab entry like this using your script path and name.

The best way to find out if our crontab configuration is running is to check /var/log/messages file and watch for the script activity every 5 minutes. Now we have a custom metric extracting monitoring information from our application and storing it in Cloudwatch. You could easily configure alarms and notifications using the AWS Console or use those metrics as trigger for an auto scaling group.

Ah! Remember that all data stored in Cloudwatch is deleted after 15 days.