【问题标题】:AttributeError: 'ResultSet' object has no attribute 'create_alarm'AttributeError:“ResultSet”对象没有属性“create_alarm”
【发布时间】:2015-05-13 23:13:22
【问题描述】:

我想用 cloudwatch 监控一个名为“topic”的主题。

使用 cloudwatch,我希望当这个主题“主题”的 NumberOfMessagesPublished > 100 时,我想向其他名为“cloudwatchSNS”的主题发送通知。

我尝试使用以下代码执行此操作,但出现此错误:

AttributeError: 'ResultSet' object has no attribute 'create_alarm'

你能帮我把它正常工作吗?

import boto.sns
from boto.ec2.cloudwatch import MetricAlarm
import boto.ec2.cloudwatch

sns = boto.sns.connect_to_region("us-east-1")
cloudwatch = boto.ec2.cloudwatch.connect_to_region("us-east-1")

#topic to send notification when NumberOfMessagesPublished > 100
topicToSendNotification = sns.create_topic("cloudwatchSNS")

topicArnToSendNotification = topicarn['CreateTopicResponse']['CreateTopicResult']['TopicArn']

#topicName to control the number of messages published
topicNameToMonitor = "topic"

#subscriptor in topic to receive an email when NumberOfMessagesPublished > 100
subscription = sns.subscribe(topicArnToSendNotification, "email", "mail@mail.com")

metric = cloudwatch.list_metrics(dimensions={'TopicName':topicNameToMonitor},
                         metric_name="NumberOfMessagesPublished")

alarmName = "test"

metric.create_alarm(name=alarmName, comparison='>=', threshold=100, period=300,
                    evaluation_periods=2, statistic='Average', alarm_actions=[topicarnToSendNotification])

【问题讨论】:

  • 您应该发布错误的完整回溯并标记错误的代码行(使用易于识别的注释等)。到目前为止发布的消息只是表明该对象没有所需的属性(听听)。

标签: python amazon-web-services boto amazon-sns


【解决方案1】:

调用cloud watch.list_metrics(...) 将始终返回一个名为ResultSet 的类似列表的对象,即使只有1 个结果。在尝试对其创建警报之前,您需要从列表中取出实际的 Metric 对象。

metric = cloudwatch.list_metrics(dimensions={'TopicName':topicNameToMonitor},
                     metric_name="NumberOfMessagesPublished")[0]

【讨论】:

  • 感谢您的回答。但是有了你的解决方案,我得到了这个“IndexError:list index out of range”
  • 好吧,那么对list_metrics 的调用一定不会返回任何结果。尝试检查 ResultSet 以查看它包含的内容。你确定你的TopicName 正确吗?
  • 是的,主题名称“topic”是正确的,并且出现在 SNS 的 aws 管理控制台中。然而,这个主题名称“主题”没有出现在 cloudwatch sns 指标的 cloudwatch 管理控制台中......但我不明白为什么。但这应该是问题所在。因为在维度上,如果我使用出现在 aws cloudwatch 管理控制台中的 topicName,它就可以工作......
  • 你真的用过这个话题吗?我认为在该主题存在实际指标之前,该名称不会出现在 CloudWatch 中。
  • 不,我还没用过这个话题。必须先使用主题,使用后才能创建cloudwatch警报?
猜你喜欢
  • 2017-04-25
  • 1970-01-01
  • 1970-01-01
  • 2016-08-16
  • 2018-05-10
  • 1970-01-01
  • 2018-08-06
  • 2017-07-22
  • 1970-01-01
相关资源
最近更新 更多