Skip to content

Commit

Permalink
Add myslql slow query log example
Browse files Browse the repository at this point in the history
  • Loading branch information
jordansissel committed Oct 4, 2012
1 parent 2d8b877 commit 634329c
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions spec/examples/mysql-slow-query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require "test_utils"

describe "parse mysql slow query log" do
extend LogStash::RSpec

# The logstash config goes here.
# At this time, only filters are supported.
config <<-'CONFIG'
filter {
grep {
# Drop the '# Time:' lines
match => [ "@message", "^# Time: " ]
negate => true
}
grok {
singles => true
pattern => [
"^# User@Host: %{USER:user}\[[^\]]+\] @ %{HOST:host} \[%{IP:ip}]",
"^# Query_time: %{NUMBER:duration:float} \s*Lock_time: %{NUMBER:lock_wait:float} \s*Rows_sent: %{NUMBER:results:int} \s*Rows_examined: %{NUMBER:scanned:int}",
"^SET timestamp=%{NUMBER:timestamp};"
]
}
multiline {
pattern => "^# User@Host: "
negate => true
what => previous
}
date {
timestamp => UNIX
}
mutate {
remove => "timestamp"
}
}
CONFIG

lines = <<-'MYSQL_SLOW_LOGS'
# Time: 121004 6:00:27
# User@Host: someuser[someuser] @ db.example.com [1.2.3.4]
# Query_time: 0.018143 Lock_time: 0.000042 Rows_sent: 237 Rows_examined: 286
use somedb;
SET timestamp=1349355627;
SELECT option_name, option_value FROM wp_options WHERE autoload = 'yes';
MYSQL_SLOW_LOGS

sample lines.split("\n") do
insist { subject.size } == 1 # 1 event
event = subject.first
insist { event.message.split("\n").size } == 5 # 5 lines

p event.fields
insist { event["user"] } == "someuser"
insist { event["host"] } == "db.example.com"
insist { event["ip"] } == "1.2.3.4"
insist { event["duration"] } == 0.018143
insist { event["lock_wait"] } == 0.000042
insist { event["results"] } == 237
insist { event["scanned"] } == 286
end
end

0 comments on commit 634329c

Please sign in to comment.