Skip to content

Instantly share code, notes, and snippets.

@gswallow
Last active December 26, 2015 05:59
Show Gist options
  • Save gswallow/7104983 to your computer and use it in GitHub Desktop.
Save gswallow/7104983 to your computer and use it in GitHub Desktop.
Quick & dirty mongodb stats
#!/usr/bin/env ruby
require 'rubygems'
require 'mongo'
include Mongo
info = Array.new
counts = [ 0 ]
sizes = [ 0 ]
avgs = [ 0 ]
indexes = [ 0 ]
indexsizes = [ 0 ]
def color_on(color_code)
"\e[#{color_code}m"
end
def color_off
"\e[0m"
end
def red
color_on(31)
end
def pank
color_on(35)
end
def yellow
color_on(33)
end
def off
color_off
end
def top_three?(array, value)
array.sort!.reverse!
case array.index(value)
when 0
return yellow, value, off
when 1
return pank, value, off
when 2
return red, value, off
else
return off, value, off
end
end
client = MongoReplicaSetClient.new( ['10.120.18.201:27017', '10.120.18.202:27017', '10.120.18.203:27017'], :read => :primary )
client.database_info.each do |name, size|
db = client.db(name)
db.collection_names.each do |cname|
stats = db.collection(cname).stats
unless stats.nil?
avg_obj_size = stats['avgObjSize'].nil? ? 0 : (stats['avgObjSize'] / 1024).to_i
sizes << (stats['size'] / 1024).to_i
counts << stats['count']
avgs << avg_obj_size
indexes << stats['nindexes']
indexsizes << (stats['totalIndexSize'] / 1024).to_i
info.push( {
:db => name,
:coll => cname,
:stats => {
:count => stats['count'],
:size => (stats['size'] / 1024).to_i,
:avg_obj_size => avg_obj_size,
:idx_count => stats['nindexes'],
:idx_size => (stats['totalIndexSize'] / 1024).to_i
}
} )
end
end
end
printf("%-40s %-35s %-10s %-10s %-10s %-10s %-10s\n", "DB", "Collection", "Objects", "Size", "Avg Obj Sz", "Indexes", "Index Size")
printf("%-40s %-35s %-10s %-10s %-10s %-10s %-10s\n", "--", "----------", "-------", "----", "----------", "-------", "----------")
info.each do |c|
printf_args = c[:db], c[:coll]
printf_args.concat(top_three?(counts, c[:stats][:count]))
printf_args.concat(top_three?(sizes, c[:stats][:size]))
printf_args.concat(top_three?(avgs, c[:stats][:avg_obj_size]))
printf_args.concat(top_three?(indexes, c[:stats][:idx_count]))
printf_args.concat(top_three?(indexsizes, c[:stats][:idx_size]))
unless printf_args.count < 17
printf("%-40s %-35s %s%-10s%s %s%-10s%s %s%-10s%s %s%-10s%s %s%-10s%s\n", *printf_args)
end
end
@gswallow
Copy link
Author

Next: send this shiz to graphite.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment