不同mongodb版本的日志格式不同,这个需要看mongodb官方对日志格式的定义,在处理前自己去做好这方面的功课。还有就是,要抓取自己感兴趣的内容,这个根据各自的需求来做,没有千篇一律的,全凭各自喜好。
grok预定义的正则匹配规则可以参考 https://github.com/logstash-plugins/logstash-patterns-core/tree/master/patterns 可以把这些文件全部下载下来放到patterns目录下,随时调用。同时,你如果安装了logstash会在这个目录下有一系列自带的正则/opt/logstash/vendor/bundle/jruby/1.9/gems/logstash-patterns-core-2.0.2/patterns,不同安装方式可能目录有别。
我这里使用版本信息如下:
- elasticsearch 2.2.0
- logstash 2.2.2
- kibana 4.4.0
- mongodb 3.2.0
- filebeat 1.1.1
mongodb 日志格式
详细请参考 https://docs.mongodb.org/manual/reference/log-messages/#log-message-components
从3.0版本开始,mongodb日志内容包含severity level和component。
|
<
timestamp
>
<
severity
>
<
component
>
[
<
context
>
]
<
message
>
|
如:
|
2014
-
11
-
03T18
:
28
:
32.450
-
0500
I
NETWORK
[
initandlisten
]
waiting
for
connections
on
port
27017
|
timestamp
时间戳默认使用iso8601-local
severity level
Level Description
F |
Fatal |
E |
Error |
W |
Warning |
I |
Informational, for Verbosity Level of 0 |
D |
Debug, for All Verbosity Levels > 0 |
compoent
Item Description
ACCESS |
消息涉及到访问控制相关的,如验证。 |
COMMAND |
消息涉及到数据库命令相关的,如count。 |
CONTROL |
消息涉及到活动控制相关的,如 initialization。 |
GEO |
消息涉及到空间地理解析相关的,如 verifying the GeoJSON shapes。 |
INDEX |
消息涉及到索引操作相关的,如创建索引。 |
NETWORK |
消息涉及到网络活动相关的,如接收连接。 |
QUERY |
消息涉及到查询相关的,包含查询规划活动状况。 |
REPL |
消息涉及到复制集相关的,如 initial sync and heartbeats。 |
SHARDING |
消息涉及到分片活动相关的,如 the startup of the mongos。 |
STORAGE |
消息涉及到存储活动相关的,如processes involved in the fsync command。 |
JOURNAL |
消息涉及到具体journaling 活动相关的。 |
WRITE |
消息涉及到写操作,如update命令。 |
- |
消息不与命名组件相关的。 |
filebeat配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# vi /etc/filebeat/filebeat.yml
filebeat
:
prospectors
:
-
paths
:
-
/
www
.
ttlsa
.
com
/
logs
/
mysql
/
slow
.
log
document_type
:
mysqlslowlog
input_type
:
log
multiline
:
negate
:
true
match
:
after
-
paths
:
-
/
www
.
ttlsa
.
com
/
logs
/
mongodb
/
mongodb
.
log
document_type
:
mongodblog
registry_file
:
/
var
/
lib
/
filebeat
/
registry
output
:
logstash
:
hosts
:
[
"10.6.66.18:5046"
]
shipper
:
logging
:
files
:
|
logstash配置
1. input配置
参见上一篇。
2. filter配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
# vi 17-mongodblog.conf
filter
{
if
[
type
]
==
"mongodblog"
{
grok
{
match
=
>
[
"message"
,
"%{TIMESTAMP_ISO8601:timestamp}\s+%{MONGO3_SEVERITY:severity}\s+%{MONGO3_COMPONENT:component}\s+(?:\[%{DATA:context}\])?\s+%{GREEDYDATA:body}"
]
}
if
[
body
]
=
~
"ms$"
{
grok
{
match
=
>
[
"body"
,
"query\s+%{WORD:db_name}\.%{WORD:collection_name}.*}.*\}(\s+%{NUMBER:spend_time:int}ms$)?"
]
}
}
date
{
match
=
>
[
"timestamp"
,
"UNIX"
,
"YYYY-MM-dd HH:mm:ss"
,
"ISO8601"
]
remove_field
=
>
[
"timestamp"
]
}
}
}
|
3. output配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
# vim 30-beats-output.conf
output
{
if
"_grokparsefailure"
in
[
tags
]
{
file
{
path
=
>
"/var/log/logstash/grokparsefailure-%{[type]}-%{+YYYY.MM.dd}.log"
}
}
if
[
@
metadata
]
[
type
]
in
[
"mysqlslowlog"
,
"mongodblog"
]
{
elasticsearch
{
hosts
=
>
[
"10.6.66.18:9200"
]
sniffing
=
>
true
manage_template
=
>
false
template_overwrite
=
>
true
index
=
>
"%{[@metadata][beat]}-%{[type]}-%{+YYYY.MM.dd}"
document_type
=
>
"%{[@metadata][type]}"
}
}
|
logstash 标准输出结果
logstash-mongodb-log
kibana
logstash-mongodb-log-kibana<div class="iteye-blog-content-contain" style="font-size: 14px"></div>