如何从表格数据中获取数据然后打印消息

How to I get data from tabulated data then print a message

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

> +| 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)

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

添加如下内容:

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

因此,如果您没有收到反馈,则没有错误。如果某些 Sum 大于 0,它会告诉你 Some error