问题
Below is my part of Python script to retrieve Redshift's PercentageDiskSpaceUsed
metric.
I have changed my code from the previous post. When i write script using boto3, its not working. But working when written using boto2. Pasting both the scripts. Please check and correct:-
Script using 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:-
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'}]))
回答1:
It appears that you also need to supply Dimensions
.
First, get the metrics working via the AWS Command-Line Interface (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']
)
回答2:
Try using newer StartTime and EndTime (March for example) or a different period (try 3600).
You are setting a period of 600, which is 10 minutes. 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
You are asking for data from January, which is more than 63 days in the past.
来源:https://stackoverflow.com/questions/43523173/amazon-cloudwatch-is-not-returning-redshift-metrics