1

我正在尝试基于资源组导入 SQS 队列并添加队列中可用的所有可见消息。问题是 boto3 有时不会返回所有队列。我知道这是 boto3 的问题,因为大多数时候我在运行相同的代码时都得到了正确的值。就我而言,boto3 应该返回 3 个队列,但有时缺少一个或两个队列。这是我写的代码

import boto3
import time
from datetime import timedelta, datetime

#clients for SQS, Cloudwatch, AutoScalingGroups, and ResourceGroups
sqs = boto3.client('sqs')
cloudwatch = boto3.client('cloudwatch')
asg = boto3.client('autoscaling')
rg = boto3.client('resource-groups')

#Importing SQS Queues with the tag Env:Production
response = rg.list_group_resources(Group='env_prod')
resources = response.get('Resources')
    
    
soa = 0.0 
noi = 0.0 
ABPI = 100    

def SumOfAverages(resources, soa, response, cloudwatch):
    for idents in resources:
        identifier = idents.get('Identifier')
        resourcetype = identifier.get('ResourceType')
        if resourcetype == 'AWS::SQS::Queue':
            RArn = identifier.get('ResourceArn') 
            step0 = RArn.split(':')
            step1 = step0[5]
            response1 = cloudwatch.get_metric_statistics(
            Namespace='AWS/SQS',
            MetricName='ApproximateNumberOfMessagesVisible',
            Dimensions=[
                {
                    'Name': 'QueueName',
                    'Value': step1
                },
            ],
            StartTime=datetime.utcnow() - timedelta(minutes=1),
            EndTime=datetime.utcnow(),
            Period=60,
            Statistics=[
                'Average',
            ],
            
            Unit='Count'
            )
            datapoints = response1['Datapoints']
            for values in datapoints:
                averages = values['Average']
                soa += averages
               
    return(soa)

result = SumOfAverages(resources, soa, response, cloudwatch)
print(result)


Response:
[{'Timestamp': datetime.datetime(2021, 5, 8, 6, 34, tzinfo=tzutc()), 'Average': 122.0, 'Unit': 'Count'}]
[{'Timestamp': datetime.datetime(2021, 5, 8, 6, 34, tzinfo=tzutc()), 'Average': 101.0, 'Unit': 'Count'}]
[{'Timestamp': datetime.datetime(2021, 5, 8, 6, 34, tzinfo=tzutc()), 'Average': 94.0, 'Unit': 'Count'}]


[{'Timestamp': datetime.datetime(2021, 5, 8, 6, 34, tzinfo=tzutc()), 'Average': 122.0, 'Unit': 'Count'}]
[{'Timestamp': datetime.datetime(2021, 5, 8, 6, 34, tzinfo=tzutc()), 'Average': 101.0, 'Unit': 'Count'}]
[]
4

0 回答 0