一、有强烈的审计需求。python
二、能容许10%-15%左右的性能损失。sql
三、有强烈的对数据库操做实时查看需求(通常都是为了领导要求)。数据库
1
2
3
4
5
6
7
8
9
10
11
|
input {
file {
path => ["/u02/backup/audit.log"]
codec => json
}
}
output {
elasticsearch {
hosts => ["192.168.1.233"]
}
}
|
上面的配置看上去是没有问题的,若是是通常的json数据哪就能解析成功了,json
可是在 Percona audit plugin 中应用程序应用程序生成的SQL是五花八门,各类字符都有其中有。python2.7
以下审计的日志被 python 读取后的字符串展示(红色标记):elasticsearch
从上图能够看到有一些换行后tab的字符,这些字符使用 json.load 的时候会报错,不能解析成jsonide
使用python json 模块解析相关日志文件报错:性能
1
2
3
4
5
6
7
8
9
10
|
>>> json.loads(json_line)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.7/site-packages/simplejson/__init__.py", line 516, in loads
return _default_decoder.decode(s)
File "/usr/lib64/python2.7/site-packages/simplejson/decoder.py", line 370, in decode
obj, end = self.raw_decode(s)
File "/usr/lib64/python2.7/site-packages/simplejson/decoder.py", line 400, in raw_decode
return self.scan_once(s, idx=_w(s, idx).end())
simplejson.scanner.JSONDecodeError: Invalid control character '\t' at: line 1 column 232 (char 231)
|
因此在使用logstash的时候也就解析不了这样的json数据了,spa
最终logstash只能报错并将整个message记录到 Elasticsearch 中插件
解决办法就是把这些字符替换掉。以下 Logstash 配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
input {
file {
path => ["/u02/backup/audit.log"]
}
}
filter {
mutate {
gsub => ["message", "\\\\n", " "]
gsub => ["message", "\t", " "]
replace => [ "message", "%{message}" ]
}
json{
source => "message"
}
mutate {
remove_field => [ "message" ]
}
}
output {
elasticsearch {
hosts => ["192.168.1.233"]
}
}
|
该配置文件是投机取巧的办法, 把 (换行/tab) 字符替换成空格,要注意的一点最终显示的SQL和原来的有所差异。
这种方法有点不灵活若是sql语句中还有遇到一些json不能解析的字符又要进行处理。
>>朋友们若是有更好的方法也告知一声哈!<<<
刚开始觉得这一切都万事大吉了。其实还有个一坑就是在使用 Kibana 查看的时候,这时候问题就来了。
有是有过 Percona audit 插件的估计都有这样的问题,就是他记录的是时间是国际形式的(如上图黄色标记),不像咱们习惯了北京时间。所以在页面显示的时间会比咱们平时的少 8 小时。
通常来讲在ELK中使用国际的标准格式是合理的。由于在使用 Kibana 查看的时候会帮你自动转化成本地时间格式。也就是若是咱们在中国他会自动把 timezone 转化为 Asia/Shanghai(东8区) 的。因此显示的时间应该是正确的才对。但是实际状况并无。
是应为 Elasticsearch 将 "2016-08-30T01:45:30 UTC" 这串字符解析成了String类型。按道理应该解析成和@timestamp同样的date类型。
将 "2016-08-30T01:45:30 UTC" 格式转化成和 @timestamp 同样的格式("2016-08-30T01:45:30Z")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
input {
file {
path => ["/u02/backup/audit.log"]
}
}
filter {
mutate {
gsub => ["message", "\\\\n", " "]
gsub => ["message", "\t", " "]
replace => [ "message", "%{message}" ]
}
json{
source => "message"
}
mutate {
remove_field => [ "message" ]
gsub => ["[audit_record][timestamp]", " UTC", "Z"]
replace => [ "[audit_record][timestamp]", "%{[audit_record][timestamp]}" ]
}
}
output {
elasticsearch {
hosts => ["192.168.1.233"]
}
}
|
使用上面配置就能顺利的将 时间格式 转化成 Elasticsearch 想要的时间格式,而且能在 Kibana 中正确显示。