miércoles, 7 de mayo de 2014

Creating CloudWatch custom metrics

By default, AWS provides useful metrics of your services. But sometimes, this is not enough. For example, for an instance AWS provides next metrics:

  • CPUUtilization
  • DiskReadOps
  • DiskWriteOps
  • DiskReadBytes
  • DiskWriteBytes
  • NetworkIn
  • NetworkOut
  • StatusCheckFailed
  • StatusCheckFailed_Instance
  • StatusCheckFailed_System
For detailed information, read next link. As you can see, some useful metrics for instance health checks are not present, for example, disk usage. This kind of information are easy to obtain, for example with a simple 'df' command:

For this instance, root volume usage is 17%. One of the facilities AWS CloudWatch offers is the possibility to create custom metrics. Using AWS CLI and put-metric-data command from CloudWatch service you can populate data for a custom metric. Detailed process is explained here, but I think it´s easier to understand with an example. You´ll need to define (at least):
  • Metric name: The name of the metric
  • Name space: The namespace for the metric data
  • Timestamp: The time stamp used for the metric. If not  specified,  the  default  value  is  set  to  the  time  the metric data was received.
  • Value: The value for the metric
It´s easy to obtain percent disk usage for volume using shell-scripting, for example:

With this value and provided information, we can create a simple shell-script that obtain this value and push it as a custom metric in AWS CloudWatch:

Once shell-script is created, we need to run it each 5 minutes, using cron facility for example:

Now, if you open AWS web console, CloudWatch service. In 'Custom Metrics' tab you can select new custom metric. I recommend you to assign a representative name. For example, I select vol-bdcf2e92 as namespace (volume-id associated in AWS for this disk) and VolumeUsage as metric name:

Select metric name and registered values will appear:

Additionally, you can create alarms from custom metrics. To do this, just press Alarms -> Create Alarm, select custom metric and define alarm. For our example, we´re going to create an email alarm when disk usage associated to volume vol-bdcf2e92 is above 80% during 20 minutes:

As you can see, it´s easy to create custom metrics and associated alarms. For detailed information about CloudWatch pricing, please visit next url