Logstash – 解析日志
Logstash – 解析日志
Logstash 使用输入插件接收日志,然后使用过滤器插件来解析和转换数据。日志的解析和转换是根据输出目的地中存在的系统执行的。Logstash 解析日志数据并仅转发必填字段。稍后,这些字段被转换为目标系统的兼容且可理解的形式。
如何解析日志?
日志的解析是使用GROK(知识的图形表示)模式执行的,您可以在 Github 中找到它们 –
https://github.com/elastic/logstash/tree/v1.4.2/patterns。
Logstash 将日志数据与指定的 GROK Pattern 或 Pattern 序列匹配,用于解析日志,例如“%{COMBINEDAPACHELOG}”,通常用于 apache 日志。
解析后的数据更加结构化,易于搜索和执行查询。Logstash 在输入日志中搜索指定的 GROK 模式并从日志中提取匹配的行。您可以使用 GROK 调试器来测试您的 GROK 模式。
GROK 模式的语法是 %{SYNTAX:SEMANTIC}。Logstash GROK 过滤器采用以下形式编写 –
%{PATTERN:FieldName}
这里,PATTERN 代表 GROK 模式,fieldname 是字段的名称,它代表输出中解析的数据。
例如,使用在线 GROK 调试器https://grokdebug.herokuapp.com/
输入
日志中的示例错误行 –
[Wed Dec 07 21:54:54.048805 2016] [:error] [pid 1234:tid 3456829102] [client 192.168.1.1:25007] JSP Notice: Undefined index: abc in /home/manu/tpworks/tutorialspoint.com/index.jsp on line 11
GROK 模式序列
此 GROK 模式序列与日志事件匹配,其中包含时间戳,后跟日志级别、进程 ID、事务 ID 和错误消息。
\[(%{DAY:day} %{MONTH:month} %{MONTHDAY} %{TIME} %{YEAR})\] \[.*:%{LOGLEVEL:loglevel}\]
\[pid %{NUMBER:pid}:tid %{NUMBER:tid}\] \[client %{IP:clientip}:.*\]
%{GREEDYDATA:errormsg}
输出
输出为 JSON 格式。
{
"day": [
"Wed"
],
"month": [
"Dec"
],
"loglevel": [
"error"
],
"pid": [
"1234"
],
"tid": [
"3456829102"
],
"clientip": [
"192.168.1.1"
],
"errormsg": [
"JSP Notice: Undefined index: abc in
/home/manu/tpworks/tutorialspoint.com/index.jsp on line 11"
]
}