0

在使用 get_metric_statistics 将来自 cloudwatch 的响应制成表格后,我有以下输出。我想添加验证,如果任何 Sum 大于 0,它将打印一条消息,说明存在错误。如果所有 Sum 都等于 0,它会说没有错误。

> +| Timestamp              |   Sum | Unit   | |
> 2021-01-12T09:31:00+00:00 |     0 | Count  | |
> 2021-01-12T09:30:00+00:00 |     0 | Count  | |
> 2021-01-12T09:33:00+00:00 |     0 | Count  | |
> 2021-01-12T09:29:00+00:00 |     0 | Count  | |
> 2021-01-12T09:32:00+00:00 |     0 | Count  | |
> 2021-01-12T09:28:00+00:00 |     0 | Count  |
> +---------------------------+-------+--------+

下面是我的python脚本

import json
import boto3
import os
from datetime import datetime, timedelta
from tabulate import tabulate

client = boto3.client("lambda")


class DateTimeEncoder(json.JSONEncoder):
    def default(self, o):
        if isinstance(o, datetime):
            return o.isoformat()

        return json.JSONEncoder.default(self, o)


client = boto3.client("cloudwatch")
response = client.get_metric_statistics(
    Namespace="AWS/Lambda",
    MetricName="Errors",
    Dimensions=[{"Name": "FunctionName", "Value": "XXX"}],
    StartTime=datetime.utcnow() - timedelta(seconds=360),
    EndTime=datetime.utcnow(),
    Period=60,
    Statistics=["Sum"],
)

message = json.dumps(response, cls=DateTimeEncoder)
message2 = json.loads(message)
message3 = message2["Datapoints"]
message4 = tabulate(message3, headers="keys", tablefmt="psql")
print(message4)

我知道这看起来很容易,但请帮忙。谢谢!

4

1 回答 1

1

添加类似的内容:

for message in message2:
   if message["Sum"] > 0:
      print("Some error occured")
   else:
      pass

所以如果你没有得到反馈,就没有错误。如果 someSum大于 0,它会告诉你Some error

于 2021-01-12T09:51:48.640 回答