跳至内容

如何使用 CloudWatch Logs Insights 来查询我的 VPC 流量日志?

2 分钟阅读
0

我想使用 Amazon CloudWatch Logs Insights 查询来处理日志组中的 Amazon Virtual Private Cloud (Amazon VPC) 流日志。

解决方法

有关您使用的语法的概述,请参阅 CloudWatch Logs Insights 语言查询语法

使用示例查询

使用 CloudWatch 控制台运行 CloudWatch Logs Insights 示例查询。要运行之前运行的查询,请选择 History(历史记录)。要导出结果,请选择 Export results(导出结果),然后选择一种格式。

场景 1

您有 Web 服务器、应用程序服务器和数据库服务器。您收到超时或 HTTP 503 错误,并且您想确定错误的原因。

请使用以下示例变量运行查询:

  • 将 “**操作” ** 设置为 “**拒绝”,**这样查询只返回被拒绝的连接。
  • 在查询中仅包括内部网络。
  • 服务器 IP 地址列表同时显示入站和出站连接(srcAddrdstAddr)。
  • 限制设置为 ** 5,**这样查询就只显示前五个条目。
  • Web 服务器 IP 地址为 10.0.0.4。
  • 应用程序服务器 IP 地址为 10.0.0.5。
  • 数据库服务器 IP 地址为 10.0.0.6。

查询示例:

filter(   action="REJECT" and
   dstAddr like /^(10\.|192\.168\.)/ and
   srcAddr like /^(10\.|192\.168\.)/ and
   (srcAddr = "10.0.0.4" or dstAddr = "10.0.0.4" or srcAddr = "10.0.0.5" or dstAddr = "10.0.0.5" or srcAddr = "10.0.0.6" or dstAddr = "10.0.0.6")
)
| stats count(*) as records by srcAddr,dstAddr,dstPort,protocol
| sort records desc
| limit 5

场景 2

您在网络接口上遇到间歇性超时情况。要检查一段时间内网络接口上是否存在拒绝,请运行以下查询:

fields @timestamp, interfaceId, srcAddr, dstAddr, action| filter (interfaceId = 'eni-05012345abcd' and action = 'REJECT')
| sort @timestamp desc
| limit 5

场景 3

要生成有关特定网络接口的报告,请运行以下查询:

fields @timestamp, @message | stats count(*) as records by dstPort, srcAddr, dstAddr as Destination
 | filter interfaceId="eni-05012345abcd"
 | filter dstPort="80" or dstPort="443" or dstPort="22" or dstPort="25"
 | sort HitCount desc
 | limit 10

上述查询会检查发送到不同端口的流量。

场景 4

要列出尝试连接到特定 IP 地址的 IP 地址,请运行以下查询:

fields @timestamp, srcAddr, dstAddr | sort @timestamp desc
 | limit 5
 | filter srcAddr like "172.31."

要列出尝试连接到特定 CIDR 的 IP 地址,请运行以下查询:

fields @timestamp, srcAddr, dstAddr | sort @timestamp desc
 | limit 5
 | filter isIpv4InSubnet(srcAddr,"172.31.0.0/16")

更多查询示例,请参阅 Amazon VPC 流日志查询

相关信息

使用 CloudWatch Logs Insights 分析日志数据

Supported logs and discovered fields