Class: Detector::Addons::MariaDB
- Inherits:
-
MySQL
show all
- Defined in:
- lib/detector/addons/mariadb.rb
Instance Attribute Summary
Attributes inherited from Base
#keys, #uri
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from MySQL
#close, #connection_count, #connection_limit, #estimated_row_count, #protocol_type, #replication_available?, #table_count, #usage
Methods inherited from Base
#asn, #close, #connection?, #connection_count, #connection_limit, #connection_usage_percentage, #databases?, detect, #estimated_row_count, #geo, #geography, #host, #infrastructure, #ip, #kind, #ping, #port, #protocol_type, #region, register_addon, #replication_available?, #sql?, #summary, #table_count, #tables?, #tcp_test, #transport?, #udp_test, #usage, #valid?, #with_connection
Constructor Details
#initialize(url) ⇒ MariaDB
Cache for database requests
56
57
58
59
|
# File 'lib/detector/addons/mariadb.rb', line 56
def initialize(url)
super
@cache = {}
end
|
Class Method Details
.capabilities_for(url) ⇒ Object
10
11
12
|
# File 'lib/detector/addons/mariadb.rb', line 10
def self.capabilities_for(url)
{ sql: true, kv: true, url: url, kind: :mariadb, databases: true, tables: true }
end
|
.handles_uri?(uri) ⇒ Boolean
6
7
8
|
# File 'lib/detector/addons/mariadb.rb', line 6
def self.handles_uri?(uri)
uri.scheme.downcase == 'mariadb'
end
|
Instance Method Details
#cli_name ⇒ Object
420
421
422
|
# File 'lib/detector/addons/mariadb.rb', line 420
def cli_name
"mariadb"
end
|
#connection ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/detector/addons/mariadb.rb', line 61
def connection
return @conn if @conn && @conn.ping
db_name = uri.path ? uri.path.sub(/^\//, '') : nil
begin
with_retry do
conn = Mysql2::Client.new(
host: host,
username: uri.user,
password: uri.password,
database: db_name,
port: port,
connect_timeout: 15,
read_timeout: 30,
write_timeout: 30,
reconnect: true
)
conn.query("SELECT 1")
@conn = conn
end
@conn
rescue Mysql2::Error => e
error_message = "MariaDB connection error: #{e.message}"
error_type = case
when e.error_number == 1226 then "max_user_connections exceeded"
when e.error_number == 1045 then "access denied (auth failure)"
when e.error_number == 1049 then "unknown database '#{db_name}'"
when e.error_number == 2003 then "server unavailable or network error"
when e.error_number == 2005 then "unknown host"
when e.error_number == 2006 then "server gone away"
when e.error_number == 2013 then "connection lost"
else "general error"
end
puts "#{error_message} [#{error_type}]" if ENV['DETECTOR_DEBUG']
@cache[:connection_error] = {
message: e.message,
type: error_type,
error_number: e.error_number,
retriable: retriable_error?(e.error_number)
}
nil
rescue => e
puts "General connection error: #{e.class} - #{e.message}" if ENV['DETECTOR_DEBUG']
@cache[:connection_error] = {
message: e.message,
type: "general error",
error_number: 0,
retriable: false
}
nil
end
end
|
#connection_error ⇒ Object
143
144
145
|
# File 'lib/detector/addons/mariadb.rb', line 143
def connection_error
@cache[:connection_error]
end
|
#connection_info ⇒ Object
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
# File 'lib/detector/addons/mariadb.rb', line 246
def connection_info
return @cache[:connection_info] if @cache[:connection_info]
if connection.nil?
error_msg = connection_error ? connection_error[:type] : "unknown error"
@cache[:connection_info] = {
connection_count: { user: "ERROR", global: "ERROR" },
connection_limits: { user: "ERROR", global: "ERROR" },
error: "Connection error: #{error_msg}"
}
return @cache[:connection_info]
end
begin
user_limit_result = execute_query("SELECT @@max_user_connections AS `limit`")
user_count_result = execute_query("SELECT COUNT(*) AS count FROM information_schema.PROCESSLIST WHERE user = USER()")
global_limit_result = execute_query("SELECT @@max_connections AS `limit`")
global_count_result = execute_query("SELECT COUNT(*) AS count FROM information_schema.PROCESSLIST")
if !user_limit_result || !user_count_result || !global_limit_result || !global_count_result
return {
connection_count: { user: "ERROR", global: "ERROR" },
connection_limits: { user: "ERROR", global: "ERROR" },
error: "Error executing connection info queries"
}
end
user_limit = user_limit_result.first['limit'].to_i
user_count = user_count_result.first['count'].to_i
global_limit = global_limit_result.first['limit'].to_i
global_count = global_count_result.first['count'].to_i
user_limit = global_limit if user_limit == 0
@cache[:connection_info] = {
connection_count: { user: user_count, global: global_count },
connection_limits: { user: user_limit, global: global_limit }
}
return @cache[:connection_info]
rescue Mysql2::Error => e
error_type = case
when e.error_number == 1226 then "max_user_connections exceeded"
else "query error"
end
puts "MariaDB connection_info error: #{e.message} [#{error_type}]" if ENV['DETECTOR_DEBUG']
@cache[:connection_info] = {
connection_count: { user: "ERROR", global: "ERROR" },
connection_limits: { user: "ERROR", global: "ERROR" },
error: "Connection error: #{error_type}"
}
return @cache[:connection_info]
rescue => e
puts "General connection_info error: #{e.message}" if ENV['DETECTOR_DEBUG']
nil
end
end
|
#database_count ⇒ Object
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
# File 'lib/detector/addons/mariadb.rb', line 311
def database_count
return @cache[:database_count] if @cache[:database_count]
if @cache[:databases]
@cache[:database_count] = @cache[:databases].size
return @cache[:database_count]
end
if connection.nil? && uri.path
@cache[:database_count] = 1
return @cache[:database_count]
end
return nil unless connection
begin
result = execute_query("SELECT COUNT(*) AS count FROM information_schema.SCHEMATA WHERE schema_name NOT IN ('mysql', 'information_schema', 'performance_schema', 'sys')")
if result && result.first
@cache[:database_count] = result.first['count']
return @cache[:database_count]
else
return 0
end
rescue Mysql2::Error => e
puts "Error getting database count: #{e.message} [#{e.error_number}]" if ENV['DETECTOR_DEBUG']
if e.error_number == 1045 || e.error_number == 1044
return 0
end
if e.error_number == 1226 && uri.path
return 1
end
nil
rescue => e
puts "General error getting database count: #{e.message}" if ENV['DETECTOR_DEBUG']
nil
end
end
|
#databases ⇒ Object
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
# File 'lib/detector/addons/mariadb.rb', line 204
def databases
return @cache[:databases] if @cache[:databases]
return [] unless connection
begin
query = "SELECT
s.schema_name AS name,
IFNULL(FORMAT(SUM(t.data_length + t.index_length) / 1024 / 1024, 2), '0.00') AS size_mb,
IFNULL(SUM(t.data_length + t.index_length), 0) AS raw_size,
COUNT(t.table_name) AS table_count
FROM information_schema.SCHEMATA s
LEFT JOIN information_schema.TABLES t ON t.table_schema = s.schema_name
WHERE s.schema_name NOT IN ('mysql', 'information_schema', 'performance_schema', 'sys')
GROUP BY s.schema_name
ORDER BY raw_size DESC"
result = execute_query(query)
if result
db_list = result.map do |row|
{
name: row['name'],
size: "#{row['size_mb']} MB",
raw_size: row['raw_size'].to_i,
table_count: row['table_count'].to_i
}
end
@cache[:databases] = db_list.sort_by { |db| -db[:raw_size] }
return @cache[:databases]
else
return []
end
rescue => e
puts "Error getting databases: #{e.message}"
[]
end
end
|
#execute_query(query) ⇒ Object
Method to execute queries with retry for retriable errors
130
131
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/detector/addons/mariadb.rb', line 130
def execute_query(query)
return nil unless connection
begin
with_retry do
connection.query(query)
end
rescue => e
puts "Query execution error: #{e.message}" if ENV['DETECTOR_DEBUG']
nil
end
end
|
#info ⇒ Object
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
# File 'lib/detector/addons/mariadb.rb', line 147
def info
return @cache[:info] if @cache[:info]
if connection.nil? && uri.path && uri.user
db_name = uri.path.sub(/^\//, '')
error_msg = connection_error ? " (#{connection_error[:type]})" : " (connection issue)"
@cache[:info] = {
'version' => "Unknown#{error_msg}",
'database' => db_name,
'user' => "#{uri.user}@remote"
}
return @cache[:info]
end
return nil unless connection
begin
result = execute_query("SELECT VERSION() AS version, DATABASE() AS `database`, USER() AS user")
@cache[:info] = result ? result.first : nil
return @cache[:info]
rescue Mysql2::Error => e
error_type = case
when e.error_number == 1226 then "max_user_connections exceeded"
else "query error"
end
puts "MariaDB info error: #{e.message} [#{error_type}]" if ENV['DETECTOR_DEBUG']
db_name = uri.path ? uri.path.sub(/^\//, '') : 'unknown'
{
'version' => "Unknown (#{error_type})",
'database' => db_name,
'user' => "#{uri.user}@error"
}
rescue => e
puts "General info error: #{e.message}" if ENV['DETECTOR_DEBUG']
nil
end
end
|
#retriable_error?(error_number) ⇒ Boolean
Determine if an error is retriable
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/detector/addons/mariadb.rb', line 15
def retriable_error?(error_number)
retriable_codes = [
1040,
1053,
1077,
2002,
2003,
2006,
2008,
2013,
2026,
2055
]
retriable_codes.include?(error_number)
end
|
#tables(database_name) ⇒ Object
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
|
# File 'lib/detector/addons/mariadb.rb', line 359
def tables(database_name)
@tables ||= {}
return @tables[database_name] if @tables[database_name]
if connection.nil? && uri.path && uri.path.sub(/^\//, '') == database_name
if @cache[:dummy_tables]
return @cache[:dummy_tables]
else
return []
end
end
return [] unless connection
begin
result = execute_query("SELECT
table_name AS name,
IFNULL(FORMAT((data_length + index_length) / 1024 / 1024, 2), '0.00') AS size_mb,
IFNULL((data_length + index_length), 0) AS raw_size,
IFNULL(table_rows, 0) AS row_count
FROM information_schema.TABLES
WHERE table_schema = '#{database_name}'
ORDER BY raw_size DESC")
if result
@tables[database_name] = result.map do |row|
{
name: row['name'],
size: "#{row['size_mb']} MB",
raw_size: row['raw_size'].to_i,
row_count: row['row_count'].to_i
}
end
return @tables[database_name]
else
@tables[database_name] = []
return []
end
rescue Mysql2::Error => e
error_type = case
when e.error_number == 1044 then "access denied to information_schema"
when e.error_number == 1045 then "authentication failure"
when e.error_number == 1226 then "max_user_connections exceeded"
else "query error (#{e.error_number})"
end
puts "Error getting tables for #{database_name}: #{e.message} [#{error_type}]" if ENV['DETECTOR_DEBUG']
@tables[database_name] = []
return []
rescue => e
puts "General error getting tables for #{database_name}: #{e.message}" if ENV['DETECTOR_DEBUG']
[]
end
end
|
#user_access_level ⇒ Object
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
# File 'lib/detector/addons/mariadb.rb', line 424
def user_access_level
access_level = super
return access_level unless connection
begin
result = connection.query("SELECT 1 FROM information_schema.plugins WHERE plugin_name = 'ROLES'")
if result.count > 0
roles_result = connection.query("SELECT CURRENT_ROLE()").first
current_role = roles_result.values.first
if current_role && current_role != '' && current_role != 'NONE'
return "#{access_level} (Role: #{current_role})"
end
end
rescue => e
end
access_level
end
|
#version ⇒ Object
190
191
192
193
194
195
196
197
198
199
200
201
202
|
# File 'lib/detector/addons/mariadb.rb', line 190
def version
return @cache[:version] if @cache[:version]
return nil unless info
begin
@cache[:version] = "MariaDB #{info['version']} on #{info['database']} (#{info['user']})"
return @cache[:version]
rescue => e
@cache[:version] = "MariaDB (connection error: #{e.message})"
return @cache[:version]
end
end
|
#with_retry(max_retries = 3) ⇒ Object
Retry a connection with exponential backoff
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/detector/addons/mariadb.rb', line 34
def with_retry(max_retries = 3)
retries = 0
begin
yield
rescue Mysql2::Error => e
if retriable_error?(e.error_number) && retries < max_retries
retries += 1
wait_time = 0.5 * (2 ** retries)
puts "Connection error (#{e.error_number}): #{e.message}. Retrying in #{wait_time}s (attempt #{retries}/#{max_retries})..." if ENV['DETECTOR_DEBUG']
sleep(wait_time)
retry
else
raise
end
end
end
|