如何从 Amazon Cloudwatch 过滤和提取原始日志事件数据

How do I filter and extract raw log event data from Amazon Cloudwatch

有什么方法可以 1) 过滤和 2) 通过 API 或从 CLI 从 Cloudwatch 检索原始日志数据?我需要从 Cloudwatch 中提取一部分日志事件进行分析。

我不需要创建指标或类似的东西。这是为了对特定时间的特定事件进行历史研究。

我已转到控制台中的日志查看器,但我正在尝试拉出特定的行来告诉我某个时间的故事。日志查看器几乎不可能用于此目的。如果我有实际的日志文件,我只需 grep 并在大约 3 秒内完成。但是我没有。

澄清

Cloudwatch Logs的描述中,它说,"You can view the original log data (only in the web view?) to see the source of the problem if needed. Log data can be stored and accessed (only in the web view?) for as long as you need using highly durable, low-cost storage so you don’t have to worry about filling up hard drives." --斜体是我的

如果此控制台视图是获取源数据的唯一方法,那么通过 Cloudwatch 存储日志对我来说不是一个可接受的解决方案。我需要以足够的灵活性来获取实际数据以搜索模式,而不是点击几十页行和 copy/paste。然而,似乎没有更好的获取源数据的方法。

我自己没用过,但这是我在 GitHub:

上遇到的 Excel 导出器的开源云监视

https://github.com/petezybrick/awscwxls

Generic AWS CloudWatch to Spreadsheet Exporter CloudWatch doesn't provide an Export utility - this does. awscwxls creates spreadsheets based on generic sets of Namespace/Dimension/Metric/Statistic specifications. As long as AWS continues to follow the Namespace/Dimension/Metric/Statistic pattern, awscwxls should work for existing and future Namespaces (Services). Each set of specifications is stored in a properties file, so each properties file can be configured for a specific set of AWS Services and resources. Take a look at run/properties/template.properties for a complete example.

我认为检索数据的最佳选项已按 API 中所述提供。

关于使用 AWSCLI(普通的以及 cwlogs 插件)见 http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/SearchDataFilterPattern.html

有关模式语法(plain text[space separated]{JSON syntax})请参阅:http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/FilterAndPatternSyntax.html

对于 python 命令行实用程序 awslogs 请参阅 https://github.com/jorgebastida/awslogs

AWSCLI:aws 记录过滤日志事件

AWSCLI 是 AWS 服务的官方 CLI,现在它也支持日志。

显示帮助:

$ aws logs filter-log-events help

过滤器可以基于:

  • 日志组名--log-group-name(只用最后一个)
  • 日志流名称--log-stream-name(可多次指定)
  • 开始时间--start-time
  • 结束时间--end-time(不是--stop-time
  • 过滤模式--filter-pattern

只有 --log-group-name 是必须的。

时间使用毫秒(而非秒)表示为纪元。

调用可能如下所示:

$ aws logs filter-log-events \
    --start-time 1447167000000 \
    --end-time 1447167600000 \
    --log-group-name /var/log/syslog \
    --filter-pattern ERROR \
    --output text

它打印 6 列制表符分隔的文本:

  • 第一个:EVENTS(表示,该行是日志记录而不是其他信息)
  • 第二名:eventId
  • 第三:timestamp(记录声明为事件时间的时间)
  • 第 4 名:logStreamName
  • 第五名:message
  • 第六名:ingestionTime

因此,如果您手头有 Linux 命令行实用程序并且只关心从 2015-11-10T14:50:00Z2015-11-10T15:00:00Z 的时间间隔内的日志记录消息,您可能会得到如下结果:

$ aws logs filter-log-events \
    --start-time `date -d 2015-11-10T14:50:00Z +%s`000 \
    --end-time `date -d 2015-11-10T15:00:00Z +%s`000 \
    --log-group-name /var/log/syslog \
    --filter-pattern ERROR \
    --output text| grep "^EVENTS"|cut -f 5

带有 cwlogs 插件的 AWSCLI

cwlogs AWSCLI 插件使用起来更简单:

$ aws logs filter \
    --start-time 2015-11-10T14:50:00Z \
    --end-time 2015-11-10T15:00:00Z \
    --log-group-name /var/log/syslog \
    --filter-pattern ERROR

它期望人类可读的日期时间和总是 returns 带有(space 分隔)列的文本输出:

  • 第一名:logStreamName
  • 第二名:date
  • 第三名:time
  • 第四到最后:message

另一方面,安装有点困难(需要多做几个步骤,而且当前 pip 需要将安装域声明为可信域)。

$ pip install awscli-cwlogs --upgrade \
--extra-index-url=http://aws-cloudwatch.s3-website-us-east-1.amazonaws.com/ \
--trusted-host aws-cloudwatch.s3-website-us-east-1.amazonaws.com
$ aws configure set plugins.cwlogs cwlogs

(如果您在最后一个命令中输入错误,请在 ~/.aws/config 文件中更正)

awslogs 命令来自 jorgebastida/awslogs

这成为我最喜欢的一个——易于安装、功能强大、易于使用。

安装:

$ pip install awslogs

列出可用的日志组:

$ awslogs groups

列出日志流

$ awslogs streams /var/log/syslog

获取记录并关注它们(看到新的记录):

$ awslogs get --watch /var/log/syslog

您可以按时间范围过滤记录:

$ awslogs get /var/log/syslog -s 2015-11-10T15:45:00 -e 2015-11-10T15:50:00

从版本 0.2.0 开始,您还有 --filter-pattern 选项。

输出有列:

  • 第一个:日志组名
  • 第二个:日志流名称
  • 第三名:message

使用 --no-group--no-stream 您可以关闭前两列。

使用 --no-color 可以去除输出中的颜色控制字符。

编辑:作为 awslogs 版本 0.2.0 添加 --filter-pattern,文本已更新。

如果您使用 Python Boto3 库提取 AWS cloudwatch 日志。 get_log_events() 的函数接受以毫秒为单位的开始和结束时间。

供参考:http://boto3.readthedocs.org/en/latest/reference/services/logs.html#CloudWatchLogs.Client.get_log_events

为此,您可以输入 UTC 时间并使用 Datetime 和 timegm 模块将其转换为毫秒,这样就可以了:

from calendar import timegm
from datetime import datetime, timedelta

# If no time filters are given use the last hour
now = datetime.utcnow()
start_time = start_time or now - timedelta(hours=1)
end_time = end_time or now
start_ms = timegm(start_time.utctimetuple()) * 1000
end_ms = timegm(end_time.utctimetuple()) * 1000

因此,您可以使用 sys 输入如下所述提供输入:

python flowlog_read.py '2015-11-13 00:00:00' '2015-11-14 00:00:00'

虽然 Jan 的回答很好并且可能是作者想要的,但请注意还有一种额外的方法可以通过 subscriptions.

以编程方式访问日志

这适用于不断获取数据(通常进入 Kinesis 流)然后进一步处理的始终在线的流式传输场景。