Skip to content

Instantly share code, notes, and snippets.

@jacaetevha
Created June 20, 2012 15:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacaetevha/2960485 to your computer and use it in GitHub Desktop.
Save jacaetevha/2960485 to your computer and use it in GitHub Desktop.
a quick BASH script to parse times and SQL statements out of a Rails log
#!/bin/bash
#################################################################
# #
# I don't typically use this on an entire Rails log file, #
# I usually copy out portions of the log that I am interested #
# in and then run it through this script. While this script #
# will work on the bare Rails log I don't recommend it, since #
# the Rails log includes color formatting information and can #
# be quite large. #
# #
# So, typical usage is to tail the log, select portions from #
# the console, copy, and then invoke this script: #
# #
# pbpaste | rails_log_sql_extractor #
# #
#################################################################
egrep '(Load|SQL.+COUNT|INSERT|UPDATE|DELETE)' | gawk -F')' '
BEGIN {
total = 0;
sql_total = 0;
load_total = 0;
update_total = 0;
insert_total = 0;
delete_total = 0
print "Module,Time in MS,Query"
}
{
gsub(/[[:space:]]*/,"",$1);
gsub(/\(/,",",$1);
gsub(/ms/,"",$1);
split($1,a,",");
if(a[1] ~ "SQL") {
sql_total = sql_total+a[2];
} else if(a[1] ~ "Load") {
load_total = load_total+a[2];
} else if($2 ~ "INSERT") {
insert_total = insert_total+a[2];
} else if($2 ~ "UPDATE") {
update_total = update_total+a[2];
} else if($2 ~ "DELETE") {
delete_total = delete_total+a[2];
}
total=total+a[2];
gsub(/^[[:space:]]+/,"",$2);
s="";
for(i=2; i<NF; i++) {
s=s $i FS;
}
s=s $i;
gsub(/"/, "", s);
if (a[1] ~ "SQL") {
# print "s: " s;
match(s, /FROM ([a-zA-Z_0-9]+) (INNER|LEFT|WHERE) /, table);
# print "table[1]: " table[1];
# print "table[2]: " table[2];
# print "a[1]: " a[1];
# print "a[2]: " a[2];
gsub(/SQL/, table[1], a[1]);
}
print a[1] "," a[2] "," "\"" s "\"";
}
END {
print "Other SQL Total," sql_total;
print "Load Total," load_total;
print "Insert Total," insert_total;
print "Update Total," update_total;
print "Delete Total," delete_total;
print "Total," total
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment