简体   繁体   English

Amazon CloudWatch不返回Redshift指标

[英]Amazon CloudWatch is not returning Redshift metrics

Below is my part of Python script to retrieve Redshift's PercentageDiskSpaceUsed metric. 下面是我获取Redshift的PercentageDiskSpaceUsed指标的Python脚本的一部分。

I have changed my code from the previous post. 我已经更改了上一篇文章的代码。 When i write script using boto3, its not working. 当我使用boto3编写脚本时,它不起作用。 But working when written using boto2. 但是使用boto2编写时可以工作。 Pasting both the scripts. 粘贴两个脚本。 Please check and correct:- 请检查并更正:

Script using boto2:- 使用boto2的脚本:-

from boto.ec2 import cloudwatch
from datetime import datetime, timedelta
import boto

REDSHIFT_REGION = 'ap-south-1'
connection = boto.ec2.cloudwatch.connect_to_region(REDSHIFT_REGION)

def set_time_ranges():
    return {
        "start": datetime.utcnow() - timedelta(seconds=600),
        "end": datetime.utcnow()
    }

time_range = set_time_ranges()

data = connection.get_metric_statistics(60,time_range["start"], time_range["end"],'PercentageDiskSpaceUsed','AWS/Redshift', 'Average',                                  dimensions={
                                        "ClusterIdentifier": 'test'})    
print (data)

Script using boto3:- 使用boto3的脚本:-

import boto3
from datetime import datetime, timedelta

access_key = <xxxxxxxxxxxxxx>
secret_key = <xxxxxxxxxxxxxxx>


def set_time_ranges():
return {
    "start": datetime.utcnow() - timedelta(seconds=600),
    "end": datetime.utcnow()
}
time_range = set_time_ranges()

client = boto3.client('cloudwatch', aws_access_key_id = access_key , aws_secret_access_key = secret_key, region_name='ap-south-1')
print(client.get_metric_statistics(Period=60, StartTime=time_range["start"], EndTime=time_range["end"], MetricName="PercentageDiskSpaceUsed", Namespace="AWS/RedShift", Statistics=["Average"], Unit="Percent", Dimensions=[{'Name': 'ClusterIdentifier', 'Value': 'test'}]))

It appears that you also need to supply Dimensions . 看来您还需要提供Dimensions

First, get the metrics working via the AWS Command-Line Interface (CLI) : 首先,通过AWS命令行界面(CLI)使指标起作用:

aws cloudwatch get-metric-statistics \
--namespace 'AWS/Redshift' \
--metric-name PercentageDiskSpaceUsed \
--start-time 2017-04-22T00:00:00Z \
--end-time 2017-04-22T05:00:00Z \
--period 60 \
--statistics Average \
--dimensions Name=NodeID,Value=Shared Name=ClusterIdentifier,Value=lab

(Adjust the cluster name and time period for your particular needs.) (根据您的特定需求调整集群名称和时间段。)

To discover available namespace and dimensions values, use: 要发现可用的名称空间和维度值,请使用:

aws cloudwatch list-metrics --namespace 'AWS/Redshift'

This code then works: 此代码然后工作:

import boto3

from datetime import datetime, timedelta

client = boto3.client('cloudwatch',region_name='ap-southeast-2')

client.get_metric_statistics(
    Namespace='AWS/Redshift',
    MetricName='PercentageDiskSpaceUsed',
    Dimensions=[{'Name':'NodeID','Value':'Shared'},
                {'Name':'ClusterIdentifier','Value':'lab'}
                ],
    StartTime=datetime.utcnow() - timedelta(seconds=3600),
    EndTime=datetime.utcnow(),
    Period=60,
    Statistics=['Average']
    )

Try using newer StartTime and EndTime (March for example) or a different period (try 3600). 尝试使用更新的StartTime和EndTime(例如3月)或其他时间段(尝试3600)。

You are setting a period of 600, which is 10 minutes. 您设置的期限为600,即10分钟。 To construct the response on a 10 min level, CloudWatch needs two 5-min datapoints and 5-min datapoints are retained only for 63 days: http://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_GetMetricStatistics.html 要构建10分钟级别的响应,CloudWatch需要两个5分钟数据点,而5分钟数据点仅保留63天: http : //docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_GetMetricStatistics.html

You are asking for data from January, which is more than 63 days in the past. 您需要的是1月份(过去63天以上)的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM