Skip to content

Instantly share code, notes, and snippets.

@fabioyamate
Created February 26, 2013 18:35
Show Gist options
  • Save fabioyamate/5040896 to your computer and use it in GitHub Desktop.
Save fabioyamate/5040896 to your computer and use it in GitHub Desktop.
Export MongoDB system.profile collection to csv format with some friendly visualization
source "https://rubygems.org"
gem "mongo", "~> 1.8.2"
gem "bson_ext", "~> 1.8.2"
gem "actionpack", "~> 3.2.12"
require "rubygems"
require "bundler/setup"
require "action_view"
require "mongo"
include ActionView::Helpers::NumberHelper
include Mongo
log_file = "log-#{Time.now.to_i}.csv"
report = %w(port ts ns query ntoreturn responseLength friendly millis nscanned nreturned ratio)
fields = %w(ts ns query ntoreturn responseLength millis nscanned nreturned)
host = "localhost"
port = 27017
database = "db"
File.open(log_file, "w") do |f|
f.puts report.join(",")
@client = MongoClient.new(host, port, :read => :secondary)
@db = @client[database]
@coll = @db["system.profile"]
@coll.find({ "nscanned" => { "$exists" => true, "$gt" => 0 }, "millis" => { "$gt" => 1 } }, { fields: fields }).each do |doc|
values = fields.map do |f|
case f
when "responseLength"
[doc[f], number_to_human_size(doc[f])]
else
doc[f]
end
end
values.unshift(port)
values << number_to_percentage(doc["nreturned"] * 100 / doc["nscanned"].to_f)
f.puts values.flatten.map { |v| "\"#{v.to_s.gsub('"', "'")}\"" }.join(",")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment