| 1 |
.. Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
|
| 3 |
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 |
| 4 |
International License (the "License"). You may not use this file except in compliance with the |
| 5 |
License. A copy of the License is located at http://creativecommons.org/licenses/by-nc-sa/4.0/. |
| 6 |
|
| 7 |
This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 8 |
either express or implied. See the License for the specific language governing permissions and |
| 9 |
limitations under the License. |
| 10 |
|
| 11 |
.. _aws-boto3-cw-metrics: |
| 12 |
|
| 13 |
###################################### |
| 14 |
Getting metrics from Amazon CloudWatch |
| 15 |
###################################### |
| 16 |
|
| 17 |
This Python example shows you how to: |
| 18 |
|
| 19 |
* Get a list of published CloudWatch metrics |
| 20 |
|
| 21 |
* Publish data points to CloudWatch metrics |
| 22 |
|
| 23 |
The scenario |
| 24 |
============ |
| 25 |
|
| 26 |
Metrics are data about the performance of your systems. You can enable detailed monitoring of some |
| 27 |
resources, such as your Amazon CloudWatch instances, or your own application metrics. |
| 28 |
|
| 29 |
In this example, Python code is used to get and send CloudWatch metrics data. |
| 30 |
The code uses the AWS SDK for Python to get metrics from CloudWatch |
| 31 |
using these methods of the CloudWatch client class: |
| 32 |
|
| 33 |
* `paginate('list_metrics') <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/cloudwatch.html#CloudWatch.Client.paginate>`_. |
| 34 |
|
| 35 |
* `put_metric_data <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/cloudwatch.html#CloudWatch.Client.put_metric_data>`_. |
| 36 |
|
| 37 |
For more information about CloudWatch metrics, see `Using Amazon CloudWatch Metrics <http://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/working_with_metrics.html>`_ in the |
| 38 |
*Amazon CloudWatch User Guide*. |
| 39 |
|
| 40 |
All the example code for the Amazon Web Services (AWS) SDK for Python is available `here on GitHub <https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/python/example_code>`_. |
| 41 |
|
| 42 |
Prerequisite tasks |
| 43 |
================= |
| 44 |
|
| 45 |
To set up and run this example, you must first configure your AWS credentials, as described in :doc:`quickstart`. |
| 46 |
|
| 47 |
|
| 48 |
List metrics |
| 49 |
=============== |
| 50 |
|
| 51 |
List the metric alarm events uploaded to CloudWatch Logs. |
| 52 |
|
| 53 |
The example below shows how to: |
| 54 |
|
| 55 |
* List metric alarms of incoming log events using |
| 56 |
`paginate('list_metrics') <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/cloudwatch.html#CloudWatch.Client.paginate>`_. |
| 57 |
|
| 58 |
For more information about paginators see, :doc:`paginators` |
| 59 |
|
| 60 |
Example |
| 61 |
------- |
| 62 |
|
| 63 |
.. code-block:: python |
| 64 |
|
| 65 |
import boto3 |
| 66 |
|
| 67 |
# Create CloudWatch client |
| 68 |
cloudwatch = boto3.client('cloudwatch') |
| 69 |
|
| 70 |
# List metrics through the pagination interface |
| 71 |
paginator = cloudwatch.get_paginator('list_metrics') |
| 72 |
for response in paginator.paginate(Dimensions=[{'Name': 'LogGroupName'}], |
| 73 |
MetricName='IncomingLogEvents', |
| 74 |
Namespace='AWS/Logs'): |
| 75 |
print(response['Metrics']) |
| 76 |
|
| 77 |
|
| 78 |
Publish custom metrics |
| 79 |
====================== |
| 80 |
|
| 81 |
Publish metric data points to Amazon CloudWatch. Amazon CloudWatch associates the data points with |
| 82 |
the specified metric. If the specified metric does not exist, Amazon CloudWatch creates the metric. |
| 83 |
When Amazon CloudWatch creates a metric, it can take up to fifteen minutes for the metric to appear |
| 84 |
in calls to ListMetrics. |
| 85 |
|
| 86 |
The example below shows how to: |
| 87 |
|
| 88 |
* Publish custom metrics using |
| 89 |
`put_metric_data <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/cloudwatch.html#CloudWatch.Client.put_metric_data>`_. |
| 90 |
|
| 91 |
|
| 92 |
Example |
| 93 |
------- |
| 94 |
|
| 95 |
.. code-block:: python |
| 96 |
|
| 97 |
import boto3 |
| 98 |
|
| 99 |
# Create CloudWatch client |
| 100 |
cloudwatch = boto3.client('cloudwatch') |
| 101 |
|
| 102 |
# Put custom metrics |
| 103 |
cloudwatch.put_metric_data( |
| 104 |
MetricData=[ |
| 105 |
{ |
| 106 |
'MetricName': 'PAGES_VISITED', |
| 107 |
'Dimensions': [ |
| 108 |
{ |
| 109 |
'Name': 'UNIQUE_PAGES', |
| 110 |
'Value': 'URLS' |
| 111 |
}, |
| 112 |
], |
| 113 |
'Unit': 'None', |
| 114 |
'Value': 1.0 |
| 115 |
}, |
| 116 |
], |
| 117 |
Namespace='SITE/TRAFFIC' |
| 118 |
) |
| 119 |
|