OLD | NEW |
(Empty) | |
| 1 .. cloudwatch_tut: |
| 2 |
| 3 ========== |
| 4 CloudWatch |
| 5 ========== |
| 6 |
| 7 First, make sure you have something to monitor. You can either create a |
| 8 LoadBalancer or enable monitoring on an existing EC2 instance. To enable |
| 9 monitoring, you can either call the monitor_instance method on the |
| 10 EC2Connection object or call the monitor method on the Instance object. |
| 11 |
| 12 It takes a while for the monitoring data to start accumulating but once |
| 13 it does, you can do this:: |
| 14 |
| 15 >>> import boto |
| 16 >>> c = boto.connect_cloudwatch() |
| 17 >>> metrics = c.list_metrics() |
| 18 >>> metrics |
| 19 [Metric:NetworkIn, |
| 20 Metric:NetworkOut, |
| 21 Metric:NetworkOut(InstanceType,m1.small), |
| 22 Metric:NetworkIn(InstanceId,i-e573e68c), |
| 23 Metric:CPUUtilization(InstanceId,i-e573e68c), |
| 24 Metric:DiskWriteBytes(InstanceType,m1.small), |
| 25 Metric:DiskWriteBytes(ImageId,ami-a1ffb63), |
| 26 Metric:NetworkOut(ImageId,ami-a1ffb63), |
| 27 Metric:DiskWriteOps(InstanceType,m1.small), |
| 28 Metric:DiskReadBytes(InstanceType,m1.small), |
| 29 Metric:DiskReadOps(ImageId,ami-a1ffb63), |
| 30 Metric:CPUUtilization(InstanceType,m1.small), |
| 31 Metric:NetworkIn(ImageId,ami-a1ffb63), |
| 32 Metric:DiskReadOps(InstanceType,m1.small), |
| 33 Metric:DiskReadBytes, |
| 34 Metric:CPUUtilization, |
| 35 Metric:DiskWriteBytes(InstanceId,i-e573e68c), |
| 36 Metric:DiskWriteOps(InstanceId,i-e573e68c), |
| 37 Metric:DiskWriteOps, |
| 38 Metric:DiskReadOps, |
| 39 Metric:CPUUtilization(ImageId,ami-a1ffb63), |
| 40 Metric:DiskReadOps(InstanceId,i-e573e68c), |
| 41 Metric:NetworkOut(InstanceId,i-e573e68c), |
| 42 Metric:DiskReadBytes(ImageId,ami-a1ffb63), |
| 43 Metric:DiskReadBytes(InstanceId,i-e573e68c), |
| 44 Metric:DiskWriteBytes, |
| 45 Metric:NetworkIn(InstanceType,m1.small), |
| 46 Metric:DiskWriteOps(ImageId,ami-a1ffb63)] |
| 47 |
| 48 The list_metrics call will return a list of all of the available metrics |
| 49 that you can query against. Each entry in the list is a Metric object. |
| 50 As you can see from the list above, some of the metrics are generic metrics |
| 51 and some have Dimensions associated with them (e.g. InstanceType=m1.small). |
| 52 The Dimension can be used to refine your query. So, for example, I could |
| 53 query the metric Metric:CPUUtilization which would create the desired statistic |
| 54 by aggregating cpu utilization data across all sources of information available |
| 55 or I could refine that by querying the metric |
| 56 Metric:CPUUtilization(InstanceId,i-e573e68c) which would use only the data |
| 57 associated with the instance identified by the instance ID i-e573e68c. |
| 58 |
| 59 Because for this example, I'm only monitoring a single instance, the set |
| 60 of metrics available to me are fairly limited. If I was monitoring many |
| 61 instances, using many different instance types and AMI's and also several |
| 62 load balancers, the list of available metrics would grow considerably. |
| 63 |
| 64 Once you have the list of available metrics, you can actually |
| 65 query the CloudWatch system for that metric. Let's choose the CPU utilization |
| 66 metric for our instance.:: |
| 67 |
| 68 >>> m = metrics[5] |
| 69 >>> m |
| 70 Metric:CPUUtilization(InstanceId,i-e573e68c) |
| 71 |
| 72 The Metric object has a query method that lets us actually perform |
| 73 the query against the collected data in CloudWatch. To call that, |
| 74 we need a start time and end time to control the time span of data |
| 75 that we are interested in. For this example, let's say we want the |
| 76 data for the previous hour:: |
| 77 |
| 78 >>> import datetime |
| 79 >>> end = datetime.datetime.now() |
| 80 >>> start = end - datetime.timedelta(hours=1) |
| 81 |
| 82 We also need to supply the Statistic that we want reported and |
| 83 the Units to use for the results. The Statistic can be one of these |
| 84 values:: |
| 85 |
| 86 ['Minimum', 'Maximum', 'Sum', 'Average', 'SampleCount'] |
| 87 |
| 88 And Units must be one of the following:: |
| 89 |
| 90 ['Seconds', 'Percent', 'Bytes', 'Bits', 'Count', |
| 91 'Bytes/Second', 'Bits/Second', 'Count/Second'] |
| 92 |
| 93 The query method also takes an optional parameter, period. This |
| 94 parameter controls the granularity (in seconds) of the data returned. |
| 95 The smallest period is 60 seconds and the value must be a multiple |
| 96 of 60 seconds. So, let's ask for the average as a percent:: |
| 97 |
| 98 >>> datapoints = m.query(start, end, 'Average', 'Percent') |
| 99 >>> len(datapoints) |
| 100 60 |
| 101 |
| 102 Our period was 60 seconds and our duration was one hour so |
| 103 we should get 60 data points back and we can see that we did. |
| 104 Each element in the datapoints list is a DataPoint object |
| 105 which is a simple subclass of a Python dict object. Each |
| 106 Datapoint object contains all of the information available |
| 107 about that particular data point.:: |
| 108 |
| 109 >>> d = datapoints[0] |
| 110 >>> d |
| 111 {u'Average': 0.0, |
| 112 u'SampleCount': 1.0, |
| 113 u'Timestamp': u'2009-05-21T19:55:00Z', |
| 114 u'Unit': u'Percent'} |
| 115 |
| 116 My server obviously isn't very busy right now! |
OLD | NEW |