您的位置:首页 > 数据库 > MySQL

elk 5.1 用于mysql slow 日志

2016-12-27 20:35 387 查看
最近面临一个任务,要对mysql的慢查询日志进行整理输出。之前的Le的时候,写过一个脚本,multiline处理mysql的日志,然后每天发送日志。不过,由于日志格式不同,有些可以解析,但是有些也是解析错误了。

听说elk很牛已经很久了,之前也测试过,当时是为了打造一款日志处理中心,但是需要有RBA功能,role based authorization,区分不同的用户权限,但是当时的测试非常糟糕,一个是不懂elk,一个是RBA需要付费才有,所以后来也就搁浅了。

这次重装上阵,直捣黄龙,目标只有一个,就是解析慢查询日志。

e

下载

配置

# config/elasticsearch.yml

node.name: t17
path.data: ./data
path.logs: ./logs
network.host: 192.168.126.17

http.cors.enabled: true
http.cors.allow-origin: "*"


插件 (head)

git clone git://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head
npm install -f
grunt server
open http://localhost:9100/[/code] 

启动

# 直接启动,也不要啥启动脚本了。
./elasticsearch


l

input {
#stdin {
file {
type => "mysql-slow"
path => "/opt/elk/logstash-5.1.1/bin/master-slow.log"
start_position => "beginning"
codec => multiline {
pattern => "^# User@Host:"
negate => true
what => previous
}
}
}

filter {
# drop sleep events
grok {
match => { "message" => "SELECT SLEEP" }
add_tag => [ "sleep_drop" ]
tag_on_failure => [] # prevent default _grokparsefailure tag on real records
}
if "sleep_drop" in [tags] {
drop {}
}
grok {
#match => [ "message", "(?m)^# User@Host: %{USER:user}\[[^\]]+\] @ (?:(?<clienthost>\S*) )?\[(?:%{IP:clientip})?\]\s*# Query_time: %{NUMBER:query_time:float}\s+Lock_time: %{NUMBER:lock_time:float}\s+Rows_sent: %{NUMBER:rows_sent:int}\s+Rows_examined: %{NUMBER:rows_examined:int}\s*(?:use %{DATA:database};\s*)?SET timestamp=%{NUMBER:timestamp};\s*(?<query>(?<action>\w+)\s+.*)\n# Time:.*$" ]
#match => [ "message", "# User@Host: %{WORD:test};" ]
match => [ "message", "# User@Host:\s+%{WORD:user1}\[%{WORD:user2}\]\s+@\s+\[(?:%{IP:clientip})?\]\s+#\s+Thread_id:\s+%{NUMBER:thread_id:int}\s+Schema:\s+%{WORD:schema}\s+QC_hit:\s+%{WORD:qc_hit}\s+#\s+Query_time:\s+%{NUMBER:query_time:float}\s+Lock_time:\s+%{NUMBER:lock_time:float}\s+Rows_sent:\s+%{NUMBER:rows_sent:int}\s+Rows_examined:\s+%{NUMBER:rows_examined:int}\s+#\s+Rows_affected:\s+%{NUMBER:rows_affected:int}\s+SET\s+timestamp=%{NUMBER:timestamp};\s+(?<query>(?<action>\w+)\s+.*);"]
}
date {
match => [ "timestamp", "UNIX" ]
remove_field => [ "timestamp" ]
}
}

output {
elasticsearch {
hosts => ["192.168.126.17:9200"]
index=>"mysql-slow-log-%{+YYYY.MM.dd}"
}
stdout { codec => rubydebug }
}


最讨厌的就是filter,我们的日志是mariadb10.1.16的,网上还没有发现这种filter,所以参照着写了个。

k 暂时不用了

正则表达式 http://www.runoob.com/regexp/regexp-syntax.html

grok正则捕获 http://udn.yyuap.com/doc/logstash-best-practice-cn/filter/grok.html

mysql 慢查询日志 logstash捕获 http://kibana.logstash.es/content/logstash/examples/mysql-slow.html

专业处理mysql http://soft.dog/2016/01/30/logstash-mysql-slow-log/

非常棒的一篇文章 http://www.fblinux.com/?p=40
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  elk