通过logstash收集mysql慢查询日志转换为json

input {
file {
type => "mysql-slow"
path => "/var/log/slow_mysqld.log"
start_position => "beginning"
codec => multiline {
pattern => "^# Time:"
negate => true
what => "previous"
}
}
}
filter {
grok {
match => { "message" => "SELECT SLEEP" }
add_tag => [ "sleep_drop" ]
tag_on_failure => []
}
if "sleep_drop" in [tags] {
drop {}
}
grok {
match => [ "message", "(?m)^# Time:.*\s+# User@Host: %{USER:user}\[[^\]]+\] @ (?:(?<clienthost>\S*) )?\[(?:%{IP:clientip})?\]\s*Id: %{NUMBER:id}\s+# Query_time: %{NUMBER:query_time}\s+Lock_time: %{NUMBER:lock_time}\s+Rows_sent: %{NUMBER:rows_sent}\s+Rows_examined: %{NUMBER:rows_examined}\s*(?:use %{DATA:database};\s*)?SET timestamp=%{NUMBER:timestamp};\s*(?<query>(?<action>\w+)\s+.*)$" ]
}
grok {
match => [ "message", "(?m)^# Time: %{GREEDYDATA:Time}\s+# User@Host: %{USER:user}\[[^\]]+\] @ (?:(?<clienthost>\S*) )?\[(?:%{IP:clientip})?\]\s*Id: %{NUMBER:id}\s+# Schema: %{GREEDYDATA:schema}\s+# Query_time: %{NUMBER:query_time}\s+Lock_time: %{NUMBER:lock_time}\s+Rows_sent: %{NUMBER:rows_sent}\s+Rows_examined: %{NUMBER:rows_examined}\s+Rows_affected: %{NUMBER:Rows_affected}\s+# Bytes_sent: %{NUMBER:Bytes_sent}\s+SET timestamp=%{NUMBER:sqltimestamp};\s*(?<query>(?<action>\w+)\s+.*)$" ]
}
date {
match => [ "timestamp", "UNIX" ]
remove_field => [ "timestamp" ]
}
}
output {
     stdout {
 codec => rubydebug {}
}
}

 日志格式

# Time: 2018-11-22T01:58:21.726750Z
# User@Host: xxx[xxx] @  [xx.xx.xx.xx]  Id: 23258299
# Schema: xxx  Last_errno: 0  Killed: 0
# Query_time: 3.533099  Lock_time: 0.000021  Rows_sent: 2  Rows_examined: 2517002  Rows_affected: 0
# Bytes_sent: 975
SET timestamp=1542851901;
SELECT * FROM `x`  WHERE `xxx`
# Time: 2018-11-22T01:58:21.726750Z
# User@Host: xxx[xxx] @  [xx.xx.xx.xx]  Id: 23258299
# Schema: xxx  Last_errno: 0  Killed: 0
# Query_time: 3.533099  Lock_time: 0.000021  Rows_sent: 2  Rows_examined: 2517002  Rows_affected: 0
# Bytes_sent: 975
SET timestamp=1542851901;
SELECT * FROM `x`  WHERE `xxx`
# Time: 2018-11-22T01:58:21.726750Z
# User@Host: xxx[xxx] @  [xx.xx.xx.xx]  Id: 23258299
# Schema: xxx  Last_errno: 0  Killed: 0
# Query_time: 3.533099  Lock_time: 0.000021  Rows_sent: 2  Rows_examined: 2517002  Rows_affected: 0
# Bytes_sent: 975
SET timestamp=1542851901;
SELECT * FROM `x`  WHERE `xxx`

 

转载于:https://www.cnblogs.com/cp-miao/p/9996409.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值