myslog 0.0.7 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. data/README.md +36 -12
  2. data/lib/myslog.rb +38 -33
  3. data/myslog.gemspec +1 -1
  4. data/spec/myslog.rb +1 -1
  5. metadata +2 -2
data/README.md CHANGED
@@ -19,9 +19,15 @@ myslog = MySlog.new
19
19
  text = <<-EOF
20
20
  # Time: 111003 14:17:38
21
21
  # User@Host: root[root] @ localhost []
22
- # Query_time: 0.000270 Lock_time: 0.000097 Rows_sent: 1 Rows_examined: 0
22
+ # Query_time: 0.000270 Lock_time: 0.000097 Rows_sent: 1 Rows_examined: 0
23
23
  SET timestamp=1317619058;
24
24
  SELECT * FROM life;
25
+ # User@Host: php[php] @ [192.168.10.235]
26
+ # Thread_id: 313 Schema: ename_bbs_dx15 Last_errno: 0 Killed: 0
27
+ # Query_time: 0.031467 Lock_time: 0.000197 Rows_sent: 0 Rows_examined: 0 Rows_affected: 0 Rows_read: 2
28
+ # Bytes_sent: 1243 Tmp_tables: 0 Tmp_disk_tables: 0 Tmp_table_sizes: 0
29
+ SET timestamp=1359008764;
30
+ SELECT * FROM pre_common_session WHERE sid='vWWzwC' AND CONCAT_WS('.', ip1,ip2,ip3,ip4)='192.168.200.57';
25
31
  EOF
26
32
 
27
33
  records = myslog.parse(text)
@@ -30,15 +36,33 @@ records = myslog.parse(text)
30
36
  `records` is Array of Hash
31
37
 
32
38
  ```ruby
33
- record = records.first
34
-
35
- record[:time] #=> Time(20111003 14:17:38)
36
- record[:user] #=> "root[root]"
37
- record[:host] #=> "localhost"
38
- record[:host_ip] #=> ""
39
- record[:query_time] #=> 0.000270
40
- record[:lock_time] #=> 0.000097
41
- record[:rows_sent] #=> 1
42
- record[:rows_examined] #=> 0
43
- record[:sql] #=> "SET timestamp=1317619058; SELECT * FROM life;"
39
+ records[0]
40
+ #=> {:time => 2011-10-03 14:17:38 +0900,
41
+ #=> :user => "root[root]",
42
+ #=> :host => "localhost",
43
+ #=> :host_ip => "",
44
+ #=> :query_time => 0.000270,
45
+ #=> :lock_time => 9.7e-05,
46
+ #=> :rows_sent => 1,
47
+ #=> :rows_examined => 0,
48
+ #=> :sql => "SET timestamp=1317619058; SELECT * FROM life;"}
49
+ records[1]
50
+ #=> {:user => "php[php]",
51
+ #=> :host => "",
52
+ #=> :host_ip => "192.168.10.235",
53
+ #=> :query_time => 0.031467,
54
+ #=> :lock_time => 0.000197,
55
+ #=> :rows_sent => 0,
56
+ #=> :rows_examined => 0,
57
+ #=> :rows_affected => 0,
58
+ #=> :rows_read => 2,
59
+ #=> :thread_id => 313,
60
+ #=> :schema => "ename_bbs_dx15",
61
+ #=> :last_errno => 0,
62
+ #=> :killed => 0,
63
+ #=> :bytes_sent => 1243,
64
+ #=> :tmp_tables => 0,
65
+ #=> :tmp_disk_tables => 0,
66
+ #=> :tmp_table_sizes => 0,
67
+ #=> :sql => "SET timestamp=1359008764; SELECT * FROM pre_common_session WHERE sid='vWWzwC' AND CONCAT_WS('.', ip1,ip2,ip3,ip4)='192.168.200.57';"}
44
68
  ```
data/lib/myslog.rb CHANGED
@@ -30,18 +30,15 @@ class MySlog
30
30
  while line
31
31
  record = []
32
32
 
33
- if line.start_with? "# Time:"
34
- record << line
35
- record << lines.shift # user@host
36
- record << lines.shift # query time
37
- else
38
- record << line # user@host
39
- record << lines.shift # query time
33
+ while line != nil && line.start_with?("#")
34
+ record << line.strip
35
+ line = lines.shift
40
36
  end
41
37
 
42
38
  sql = []
43
- while (line = lines.shift) != nil && !line.start_with?("#")
39
+ while line != nil && !line.start_with?("#")
44
40
  sql << line.strip
41
+ line = lines.shift
45
42
  end
46
43
  record << sql.join(" ")
47
44
 
@@ -54,34 +51,42 @@ class MySlog
54
51
  def parse_record(records)
55
52
  response = {}
56
53
 
57
- record = records.shift
58
- if record.start_with? "# Time:"
59
- date = record[8..-1].strip
60
- response[:date] = Time.parse(date)
61
-
62
- record = records.shift
63
- else
64
- response[:date] = nil
65
- end
54
+ while (record = records.shift) != nil
66
55
 
67
- elems = record.split(" ")
68
- response[:user] = elems[2].strip
69
- if elems[5] == nil
70
- response[:host] = nil
71
- response[:host_ip] = elems[4].strip[1...-1]
72
- else
73
- response[:host] = elems[4].strip
74
- response[:host_ip] = elems[5].strip[1...-1]
75
- end
56
+ if record.start_with? "# User@Host:"
76
57
 
77
- record = records.shift
78
- elems = record.split(" ")
79
- response[:query_time] = elems[2].to_f
80
- response[:lock_time] = elems[4].to_f
81
- response[:rows_sent] = elems[6].to_i
82
- response[:rows_examined] = elems[8].to_i
58
+ elems = record.split(" ")
59
+ response[:user] = elems[2].strip
60
+ if elems[5] == nil
61
+ response[:host] = nil
62
+ response[:host_ip] = elems[4].strip[1...-1]
63
+ else
64
+ response[:host] = elems[4].strip
65
+ response[:host_ip] = elems[5].strip[1...-1]
66
+ end
67
+ elsif record.start_with? "#"
83
68
 
84
- response[:sql] = records.shift
69
+ # split with two space
70
+ elems = record[2..-1].strip().split " "
71
+ if elems.size == 1 && elems[0].start_with?("Time:")
72
+ response[:date] = Time.parse(record[8..-1].strip)
73
+ else
74
+ elems.each do |elem|
75
+ name, value = elem.split ":"
76
+ value.strip!
77
+ case value
78
+ when /^\d+$/
79
+ value = value.to_i
80
+ when /^\d+\.\d+(?:e[-+]\d+)?$/
81
+ value = value.to_f
82
+ end
83
+ response[name.downcase.to_sym] = value
84
+ end
85
+ end
86
+ else
87
+ response[:sql] = record
88
+ end
89
+ end
85
90
 
86
91
  response
87
92
  end
data/myslog.gemspec CHANGED
@@ -12,6 +12,6 @@ Gem::Specification.new do |gem|
12
12
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
13
13
  gem.name = "myslog"
14
14
  gem.require_paths = ["lib"]
15
- gem.version = "0.0.7"
15
+ gem.version = "0.0.8"
16
16
  gem.add_development_dependency "rspec"
17
17
  end
data/spec/myslog.rb CHANGED
@@ -108,7 +108,7 @@ use webtie;
108
108
 
109
109
  response.should be_an_instance_of Hash
110
110
  %w[
111
- date user host host_ip query_time lock_time rows_sent rows_examined sql
111
+ date user host host_ip time lock_time rows_sent rows_examined sql
112
112
  ].each { |k| response.should have_key k.to_sym}
113
113
  end
114
114
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: myslog
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-16 00:00:00.000000000 Z
12
+ date: 2013-01-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec