如何使用 Boto 读取 AWS 状态检查警报消息
How to read the AWS Status Check Alarm message with Boto
我使用 python 和 boto API 来:
- 创建 sns、sqs 并将 sqs 队列订阅到 sns。
- 连接到 cloudwatch,并为 "StatusCheckFailed" 指标创建警报。
- 使用put_metric_alarm启动报警监控和通知。
我启动了一个我自己的 AMI 来模拟 StatusCheckFailed 条件。
如果我转到 AWS 控制台>SQS,我可以看到我收到了警报通知。我也可以在 AWS 控制台中查看 JSON 格式的内容。
当我使用 python 和 SQS api 时,我可以:
- 查找队列 (
q = sqs.lookup(queue_name)
)
- 获取消息 (
msg = q.read()
)
- 获取邮件正文(
b = message.get_body_encoded()
)
- 或
message.get_body()
结果不可读。
get_body_encoded
得到一个没有任何分隔线的字符串。例如
TypeNotificationMessageId02a8ce9dc21e55eaaeca83b5258563ffTopicArnarnawssnsuswest21888888879statuscheckalarm17122223479SubjectStatusCheckAlarmbadvmstatusalarminUSWest2MessageAlarmNamebadvmstatusalarmAlarmDescriptionstatuscheckfori50602c5abadvmAWSAccountId11233333333NewStateValueALARMNewStateReasonThresholdCross....`
get_body
让你的编码变得不可读。
如何获取 JSON 格式的邮件正文,例如就像 AWS 控制台的 SQS 部分中显示的一样?
我在 AWS 论坛上读过类似的,对我有用。
如果您知道消息不会进行 base64 编码,您可以更改从队列中读取消息的代码,这样它就不会尝试解码消息,如下所示:
平方 = boto.connect_sqs()
队列 = sqs.get_queue('myqueue')
queue.set_message_class(boto.sqs.message.RawMessage)
get_body()
returns JSON 格式字符串的结果。
我通过创建一个 SQS 队列和一个 SNS 通知主题来重现您的情况,将 SQS 队列订阅到 SNS 主题,然后创建一个将通知推送到 SNS 主题的警报。然后我触发了警报情况并通过 Python:
检索结果
$ python
Python 2.6.9 (unknown, Sep 13 2014, 00:25:11)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import boto, boto.sqs
>>> from boto.sqs.message import Message
>>> q_conn = boto.sqs.connect_to_region("ap-southeast-2")
>>> q = q_conn.get_queue('bob')
>>> m = q.read(visibility_timeout=15)
>>> m.get_body()
u'{\n "Type" : "Notification",\n "MessageId" : "adcad528-680b-5b4f-88bd-98432f1bf252",\n "TopicArn" : "arn:aws:sns:ap-southeast-2:123412341234:bob-topic",\n "Subject" : "ALARM: \"my-alarm-name\" in APAC - Sydney",\n "Message" : "{\"AlarmName\":\"my-alarm-name\",\"AlarmDescription\":\"my-alarm-description\",\"AWSAccountId\":\"123412341234\",\"NewStateValue\":\"ALARM\",\"NewStateReason\":\"Threshold Crossed: 1 datapoint (1459.0) was greater than or equal to the threshold (0.0).\",\"StateChangeTime\":\"2015-01-11T00:33:20.013+0000\",\"Region\":\"APAC - Sydney\",\"OldStateValue\":\"INSUFFICIENT_DATA\",\"Trigger\":{\"MetricName\":\"NetworkIn\",\"Namespace\":\"AWS/EC2\",\"Statistic\":\"SUM\",\"Unit\":null,\"Dimensions\":[{\"name\":\"InstanceId\",\"value\":\"i-af41ec91\"}],\"Period\":60,\"EvaluationPeriods\":1,\"ComparisonOperator\":\"GreaterThanOrEqualToThreshold\",\"Threshold\":0.0}}",\n "Timestamp" : "2015-01-11T00:33:20.072Z",\n "SignatureVersion" : "1",\n "Signature" : "(removed)",\n "SigningCertURL" : "(removed)",\n "UnsubscribeURL" : "https://sns.ap-southeast-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:ap-southeast-2:123412341234:bob-topic:9bc1234c-1234-4995-927a-72b752866e5e"\n}'
对 get_body()
的响应是一个包含 JSON 的字符串。这可以使用 JSON 库进行解析:
>>> message_string = json.loads(m.get_body())['Message']
>>> message_string
u'{"AlarmName":"my-alarm-name","AlarmDescription":"my-alarm-description","AWSAccountId":"123412341234","NewStateValue":"ALARM","NewStateReason":"Threshold Crossed: 1 datapoint (1459.0) was greater than or equal to the threshold (0.0).","StateChangeTime":"2015-01-11T00:33:20.013+0000","Region":"APAC - Sydney","OldStateValue":"INSUFFICIENT_DATA","Trigger":{"MetricName":"NetworkIn","Namespace":"AWS/EC2","Statistic":"SUM","Unit":null,"Dimensions":[{"name":"InstanceId","value":"i-af41ec91"}],"Period":60,"EvaluationPeriods":1,"ComparisonOperator":"GreaterThanOrEqualToThreshold","Threshold":0.0}}'
>>> message = json.loads(message_string)
>>> message['AlarmName']
u'my-alarm-name'
我使用 python 和 boto API 来:
- 创建 sns、sqs 并将 sqs 队列订阅到 sns。
- 连接到 cloudwatch,并为 "StatusCheckFailed" 指标创建警报。
- 使用put_metric_alarm启动报警监控和通知。
我启动了一个我自己的 AMI 来模拟 StatusCheckFailed 条件。 如果我转到 AWS 控制台>SQS,我可以看到我收到了警报通知。我也可以在 AWS 控制台中查看 JSON 格式的内容。
当我使用 python 和 SQS api 时,我可以:
- 查找队列 (
q = sqs.lookup(queue_name)
) - 获取消息 (
msg = q.read()
) - 获取邮件正文(
b = message.get_body_encoded()
) - 或
message.get_body()
结果不可读。
get_body_encoded
得到一个没有任何分隔线的字符串。例如TypeNotificationMessageId02a8ce9dc21e55eaaeca83b5258563ffTopicArnarnawssnsuswest21888888879statuscheckalarm17122223479SubjectStatusCheckAlarmbadvmstatusalarminUSWest2MessageAlarmNamebadvmstatusalarmAlarmDescriptionstatuscheckfori50602c5abadvmAWSAccountId11233333333NewStateValueALARMNewStateReasonThresholdCross....`
get_body
让你的编码变得不可读。
如何获取 JSON 格式的邮件正文,例如就像 AWS 控制台的 SQS 部分中显示的一样?
我在 AWS 论坛上读过类似的,对我有用。
如果您知道消息不会进行 base64 编码,您可以更改从队列中读取消息的代码,这样它就不会尝试解码消息,如下所示: 平方 = boto.connect_sqs() 队列 = sqs.get_queue('myqueue') queue.set_message_class(boto.sqs.message.RawMessage)
get_body()
returns JSON 格式字符串的结果。
我通过创建一个 SQS 队列和一个 SNS 通知主题来重现您的情况,将 SQS 队列订阅到 SNS 主题,然后创建一个将通知推送到 SNS 主题的警报。然后我触发了警报情况并通过 Python:
检索结果$ python
Python 2.6.9 (unknown, Sep 13 2014, 00:25:11)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import boto, boto.sqs
>>> from boto.sqs.message import Message
>>> q_conn = boto.sqs.connect_to_region("ap-southeast-2")
>>> q = q_conn.get_queue('bob')
>>> m = q.read(visibility_timeout=15)
>>> m.get_body()
u'{\n "Type" : "Notification",\n "MessageId" : "adcad528-680b-5b4f-88bd-98432f1bf252",\n "TopicArn" : "arn:aws:sns:ap-southeast-2:123412341234:bob-topic",\n "Subject" : "ALARM: \"my-alarm-name\" in APAC - Sydney",\n "Message" : "{\"AlarmName\":\"my-alarm-name\",\"AlarmDescription\":\"my-alarm-description\",\"AWSAccountId\":\"123412341234\",\"NewStateValue\":\"ALARM\",\"NewStateReason\":\"Threshold Crossed: 1 datapoint (1459.0) was greater than or equal to the threshold (0.0).\",\"StateChangeTime\":\"2015-01-11T00:33:20.013+0000\",\"Region\":\"APAC - Sydney\",\"OldStateValue\":\"INSUFFICIENT_DATA\",\"Trigger\":{\"MetricName\":\"NetworkIn\",\"Namespace\":\"AWS/EC2\",\"Statistic\":\"SUM\",\"Unit\":null,\"Dimensions\":[{\"name\":\"InstanceId\",\"value\":\"i-af41ec91\"}],\"Period\":60,\"EvaluationPeriods\":1,\"ComparisonOperator\":\"GreaterThanOrEqualToThreshold\",\"Threshold\":0.0}}",\n "Timestamp" : "2015-01-11T00:33:20.072Z",\n "SignatureVersion" : "1",\n "Signature" : "(removed)",\n "SigningCertURL" : "(removed)",\n "UnsubscribeURL" : "https://sns.ap-southeast-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:ap-southeast-2:123412341234:bob-topic:9bc1234c-1234-4995-927a-72b752866e5e"\n}'
对 get_body()
的响应是一个包含 JSON 的字符串。这可以使用 JSON 库进行解析:
>>> message_string = json.loads(m.get_body())['Message']
>>> message_string
u'{"AlarmName":"my-alarm-name","AlarmDescription":"my-alarm-description","AWSAccountId":"123412341234","NewStateValue":"ALARM","NewStateReason":"Threshold Crossed: 1 datapoint (1459.0) was greater than or equal to the threshold (0.0).","StateChangeTime":"2015-01-11T00:33:20.013+0000","Region":"APAC - Sydney","OldStateValue":"INSUFFICIENT_DATA","Trigger":{"MetricName":"NetworkIn","Namespace":"AWS/EC2","Statistic":"SUM","Unit":null,"Dimensions":[{"name":"InstanceId","value":"i-af41ec91"}],"Period":60,"EvaluationPeriods":1,"ComparisonOperator":"GreaterThanOrEqualToThreshold","Threshold":0.0}}'
>>> message = json.loads(message_string)
>>> message['AlarmName']
u'my-alarm-name'