Skip to content

Instantly share code, notes, and snippets.

@Jarvizx
Last active June 13, 2019 18:00
Show Gist options
  • Save Jarvizx/d0f8eff0f69524ad72ca30215f72123c to your computer and use it in GitHub Desktop.
Save Jarvizx/d0f8eff0f69524ad72ca30215f72123c to your computer and use it in GitHub Desktop.
Ruby parse slow query to CSV (mysql-slow.log)
require 'csv'
def get_max_lines_file(file)
count = 0
file = File.open(file, "r") { |file| file.each_line { |line| count += 1 }}
return count
end
def fit_log(file_in, file_out)
# vars
line = {}
lines = []
col_sep = ";"
new_line = false
line_split = ""
count_rows = 0
concat_query = 0
limit_rows_file = get_max_lines_file(file_in)
File.open(file_in, "r") do |fr|
fr.each_line do |liner|
# get Time
if liner.include? "# Time:"
line_split = liner.split(/ /)
line[:Time] = line_split[2].strip
concat_query = 0
end
# get User@Host
if liner.include? "# User@Host:"
line_split = liner.split(/ /)
line[:UserHost] = line_split[2].strip + "@" + line_split[5].strip
line[:Id] = line_split[8].strip
end
# get Query_time
if liner.include? "# Query_time:"
line_split = liner.split(/ /)
line[:Query_time] = line_split[2].strip
line[:Lock_time] = line_split[5].strip
line[:Rows_sent] = line_split[7].strip
line[:Rows_examined] = line_split[10].strip
end
if concat_query == 1
# continue concat query
line[:query] << " " << liner.strip
end
if liner.include?("SELECT") || liner.include?("select")
line[:query] = liner.strip
concat_query = 1
end
# delimite end concat and new line
if liner.include? "# Time:"
if line[:Query_time]
new_line = true
end
end
count_rows += 1
# last row
last_row = limit_rows_file == count_rows
if new_line == true || last_row
lines.push([line[:Time], line[:Query_time], line[:Rows_examined], line[:query]])
new_line = false
line[:query] = "< Isn't query >"
line[:has_ip] = ""
end
end
end
CSV.open(file_out, "wb", { :col_sep => col_sep }) do |csv|
csv << ["Time","Query_time","Rows_examined","query"]
lines.each { |row| csv << row }
end
end
# name File
file_in = "mysql-slow.log"
file_out = "mysql-slow-gits.csv"
fit_log(file_in, file_out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment