myslog 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,24 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ myslog (0.0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.1.3)
10
+ rspec (2.10.0)
11
+ rspec-core (~> 2.10.0)
12
+ rspec-expectations (~> 2.10.0)
13
+ rspec-mocks (~> 2.10.0)
14
+ rspec-core (2.10.1)
15
+ rspec-expectations (2.10.0)
16
+ diff-lcs (~> 1.1.3)
17
+ rspec-mocks (2.10.1)
18
+
19
+ PLATFORMS
20
+ ruby
21
+
22
+ DEPENDENCIES
23
+ myslog!
24
+ rspec
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ myslog
2
+ ======
3
+
4
+ MySQL slow query log parser
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/lib/myslog.rb ADDED
@@ -0,0 +1,86 @@
1
+ #
2
+ # MySlog
3
+ #
4
+ # Copyright (C) 2012 Yuku TAKAHASHI
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ require "time"
19
+
20
+ class MySlog
21
+ def parse(lines)
22
+ divide(lines).map {|record| parse_record(record) }
23
+ end
24
+
25
+ protected
26
+
27
+ def divide(lines)
28
+ records = []
29
+
30
+ line = lines.shift
31
+
32
+ while line
33
+ record = []
34
+
35
+ if line.start_with? "# Time:"
36
+ record << line
37
+ record << lines.shift # user@host
38
+ record << lines.shift # query time
39
+ else
40
+ record << line # user@host
41
+ record << lines.shift # query time
42
+ end
43
+
44
+ record << lines.shift # user@host
45
+ record << lines.shift # query time
46
+
47
+ while (line = lines.shift) != nil && !line.start_with?("#")
48
+ record << line
49
+ end
50
+
51
+ records << record
52
+ end
53
+
54
+ records
55
+ end
56
+
57
+ def parse_record(lines)
58
+ response = {}
59
+
60
+ line = lines.shift
61
+ if line.start_with? "# Time:"
62
+ date = line[8..-1].strip
63
+ response[:date] = Time.parse(date)
64
+
65
+ line = lines.shift
66
+ else
67
+ response[:date] = nil
68
+ end
69
+
70
+ elems = line.split(" ")
71
+ response[:user] = elems[2].strip
72
+ response[:host] = elems[4].strip
73
+ response[:host_ip] = elems[5].strip[1...-1]
74
+
75
+ line = lines.shift
76
+ elems = line.split(" ")
77
+ response[:query_time] = elems[2].to_f
78
+ response[:lock_time] = elems[4].to_f
79
+ response[:rows_sent] = elems[6].to_i
80
+ response[:rows_examined] = elems[8].to_i
81
+
82
+ response[:sql] = lines.map{|line| line.strip}.join("\n")
83
+
84
+ response
85
+ end
86
+ end
data/myslog.gemspec ADDED
@@ -0,0 +1,17 @@
1
+ # -*- coding:utf-8 -*-
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.authors = ["taka84u9"]
5
+ gem.email = ["taka84u9@gmail.com"]
6
+ gem.description = %q{MySQL slow query parser.}
7
+ gem.summary = %q{MySQL slow query parser.}
8
+ gem.homepage = "https://github.com/taka84u9/myslog"
9
+
10
+ gem.files = `git ls-files`.split($\)
11
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
12
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
13
+ gem.name = "myslog"
14
+ gem.require_paths = ["lib"]
15
+ gem.version = "0.0.1"
16
+ gem.add_development_dependency "rspec"
17
+ end
data/spec/myslog.rb ADDED
@@ -0,0 +1,182 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require "spec_helper"
4
+
5
+ describe "MySlog" do
6
+ before :all do
7
+ @log = <<-EOF
8
+ # User@Host: gimp[drool] @ algernon.retards.org [10.10.10.7]
9
+ # Time: 14 Lock_time: 0 Rows_sent: 127 Rows_examined: 87189
10
+
11
+ select retard_user.id, cname.first,cname.last
12
+ from retard_user,contact,cname,helmet
13
+ where cname.id = contact.name_id
14
+ and contact.id = retard_user.contact_id
15
+ and retard_user.helmet_id = helmet.id
16
+ and helmet.brand_id = 9
17
+ and helmet.id = 143
18
+ group by retard_user.id
19
+ order by cname.last;
20
+ # User@Host: gimp[drool] @ algernon.retards.org [10.10.10.7]
21
+ # Time: 0 Lock_time: 0 Rows_sent: 2 Rows_examined: 1
22
+
23
+ select workbook_code from workbook_defs where brand_id=9;
24
+ # Time: 010626 10:44:50
25
+ # User@Host: staff[staff] @ algernon.retards.org [10.10.10.7]
26
+ # Time: 0 Lock_time: 0 Rows_sent: 3 Rows_examined: 1
27
+ use lead_generator;
28
+
29
+ SELECT answer_id, answer_text
30
+ FROM pl_answers
31
+ WHERE poll_id = 4
32
+ ;
33
+ # Time: 010626 10:44:51
34
+ # User@Host: gimp[drool] @ algernon.retards.org [10.10.10.7]
35
+ # Time: 0 Lock_time: 0 Rows_sent: 1 Rows_examined: 1
36
+ use webtie;
37
+
38
+ select count(tm.id)
39
+ from text_message tm, tm_status tms
40
+ where tm.tm_status_id = tms.id and
41
+ tm.to_id = 21259 and
42
+ tm.to_group = 'retard_user' and
43
+ tms.description = 'new';
44
+ EOF
45
+ end
46
+
47
+ before :each do
48
+ @myslog = MySlog.new
49
+ @lines = @log.split("\n").map {|line| line.chomp}
50
+ end
51
+
52
+ describe "#parse" do
53
+ it "should return Array of Hash" do
54
+ results = @myslog.parse(@lines)
55
+ results.should be_an_instance_of Array
56
+ results.each do |result|
57
+ results.should be_an_instance_of Array
58
+ end
59
+ end
60
+ end
61
+
62
+ describe "#divide" do
63
+
64
+ it "should return Array of Array" do
65
+ results = @myslog.send(:divide, @lines)
66
+ results.should be_an_instance_of Array
67
+ results.each do |result|
68
+ results.should be_an_instance_of Array
69
+ end
70
+ end
71
+
72
+ it "should devide lines in set of records" do
73
+ results = @myslog.send(:divide, @lines)
74
+
75
+ record = results[0]
76
+ record.size.should == 12
77
+ record[0].should ==
78
+ "# User@Host: gimp[drool] @ algernon.retards.org [10.10.10.7]"
79
+
80
+ record = results[1]
81
+ record.size.should == 4
82
+ record[0].should ==
83
+ "# User@Host: gimp[drool] @ algernon.retards.org [10.10.10.7]"
84
+
85
+ record = results[2]
86
+ record.size.should == 9
87
+ record[0].should ==
88
+ "# Time: 010626 10:44:50"
89
+
90
+ record = results[3]
91
+ record.size.should == 11
92
+ record[0].should ==
93
+ "# Time: 010626 10:44:51"
94
+ end
95
+ end
96
+
97
+ describe "#parse_record" do
98
+ it "should return an instance of Hash having specific keys" do
99
+ lines = [
100
+ "# Time: 010626 10:44:51",
101
+ "# User@Host: gimp[drool] @ algernon.retards.org [10.10.10.7]",
102
+ "# Time: 0 Lock_time: 0 Rows_sent: 1 Rows_examined: 1",
103
+ "use webtie;"
104
+ ]
105
+ response = @myslog.send(:parse_record, lines)
106
+
107
+ response.should be_an_instance_of Hash
108
+ %w[
109
+ date user host host_ip query_time lock_time rows_sent rows_examined sql
110
+ ].each { |k| response.should have_key k.to_sym}
111
+ end
112
+
113
+ context "given full log" do
114
+ describe "response" do
115
+ before :each do
116
+ @date = Time.now
117
+ @user = "root[root]"
118
+ @host = "localhost"
119
+ @host_ip = ""
120
+ @query_time = 0.000270
121
+ @lock_time = 0.000097
122
+ @rows_sent = 1
123
+ @rows_examined = 0
124
+ @sql = "SET timestamp=1317619058;\nSELECT * FROM life;"
125
+ @lines = [
126
+ "# Time: #{@date.strftime("%y%m%d %H:%M:%S")}",
127
+ "# User@Host: #{@user} @ #{@host} [#{@host_ip}]",
128
+ "# Query_time: #{@query_time} Lock_time: #{@lock_time} Rows_sent: #{@rows_sent} Rows_examined: #{@rows_examined}",
129
+ @sql
130
+ ]
131
+ @response = @myslog.send(:parse_record, @lines)
132
+ end
133
+
134
+ it "should have expected values" do
135
+ @response[:date].to_i.should == @date.to_i
136
+ @response[:user].should == @user
137
+ @response[:host].should == @host
138
+ @response[:host_ip].should == @host_ip
139
+ @response[:query_time].should == @query_time
140
+ @response[:lock_time].should == @lock_time
141
+ @response[:rows_sent].should == @rows_sent
142
+ @response[:rows_examined].should == @rows_examined
143
+ @response[:sql].should == @sql.strip
144
+ end
145
+ end
146
+ end
147
+
148
+ context "given partial log" do
149
+ describe "response" do
150
+ before :each do
151
+ @user = "root[root]"
152
+ @host = "localhost"
153
+ @host_ip = ""
154
+ @query_time = 0.000270
155
+ @lock_time = 0.000097
156
+ @rows_sent = 1
157
+ @rows_examined = 0
158
+ @sql = "SET timestamp=1317619058; SELECT * FROM life;"
159
+ @lines = [
160
+ "# User@Host: #{@user} @ #{@host} [#{@host_ip}]",
161
+ "# Query_time: #{@query_time} Lock_time: #{@lock_time} Rows_sent: #{@rows_sent} Rows_examined: #{@rows_examined}",
162
+ @sql
163
+ ]
164
+ @response = @myslog.send(:parse_record, @lines)
165
+ end
166
+
167
+ it "should have expected values" do
168
+ @response[:date].should == nil
169
+ @response[:user].should == @user
170
+ @response[:host].should == @host
171
+ @response[:host_ip].should == @host_ip
172
+ @response[:query_time].should == @query_time
173
+ @response[:lock_time].should == @lock_time
174
+ @response[:rows_sent].should == @rows_sent
175
+ @response[:rows_examined].should == @rows_examined
176
+ @response[:sql].should == @sql.strip
177
+ end
178
+ end
179
+
180
+ end
181
+ end
182
+ end
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH.unshift File.expand_path("../", __FILE__)
2
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
3
+
4
+ require "myslog"
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: myslog
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - taka84u9
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-07 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70118056303180 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70118056303180
25
+ description: MySQL slow query parser.
26
+ email:
27
+ - taka84u9@gmail.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - Gemfile.lock
35
+ - README.md
36
+ - Rakefile
37
+ - lib/myslog.rb
38
+ - myslog.gemspec
39
+ - spec/myslog.rb
40
+ - spec/spec_helper.rb
41
+ homepage: https://github.com/taka84u9/myslog
42
+ licenses: []
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 1.8.10
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: MySQL slow query parser.
65
+ test_files:
66
+ - spec/myslog.rb
67
+ - spec/spec_helper.rb