2

我正在尝试使用 aws log 洞察对包含 nginx 日志的日志组运行查询。

这是我在我的 ec2 机器上设置的日志格式:

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

示例 NGINX 日志:

xx.xx.xx.xx - - [10/Nov/2020:15:28:30 +0530] "POST /xx HTTP/1.1" 200 57 "https://xxxx.in/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36" "-"

我正在尝试使用带有以下代码的日志见解来解析它:

parse @message '* - - [*] "* * *" * * "-" "*"' as remote_addr, timestamp, request_type, location, protocol, response_code, body_bytes_sent

我收到以下错误:

Expression is invalid: parse@message'* - - [*] "* * *" * * "-" "*"'asremote_addr,timestamp,request_type,location,protocol,response_code,body_bytes_sent

任何帮助将不胜感激

4

2 回答 2

11

你已经完成了 99% 的工作。缺少的只是匹配选择器和输出变量的数量,因此如果您将附加user_agent到列表中,它将开始工作。

示例工作查询:

fields @message
| parse @message '* - - [*] "* * *" * * "-" "*"' as remote_addr, timestamp, request_type, location, protocol, response_code, body_bytes_sent, user_agent
| display request_type, location
于 2021-01-26T10:31:09.877 回答
0

使用referrer和user_agent会更好

fields @timestamp, @message
| parse @message '* - - [*] "* * *" * * "*" "*"' as remote_addr, timestamp, request_type, location, protocol, response_code, body_bytes_sent, referrer, user_agent
| display @timestamp, remote_addr, request_type, location, protocol, response_code, body_bytes_sent, referrer, user_agent
| sort @timestamp desc
于 2022-02-18T03:08:27.753 回答