Skip to content

Instantly share code, notes, and snippets.

@stulentsev
Created December 13, 2012 21:49
Show Gist options
  • Save stulentsev/4280269 to your computer and use it in GitHub Desktop.
Save stulentsev/4280269 to your computer and use it in GitHub Desktop.
A small ruby script to aggregate output of mongotop MongoDB command
# Usage:
# mongotop --host host --port port | ruby mongotop_handler.rb
require 'curses'
include Curses
def print_stats stats, time_elapsed
h = stats.length
w = 130
win = Window.new(h + 6, w + 6,
5, (cols - w) / 2)
win.box(?|, ?-)
msg = "mongotop running totals (elapsed #{time_elapsed.to_i} secs)"
win.setpos 2, (w - msg.length) / 2
win.addstr msg
sorted = stats.sort{|lhs, rhs| rhs[1][:total] <=> lhs[1][:total] }
sorted.each_with_index do |(k, v), idx|
win.setpos(4 + idx, 3)
win.addstr(k.rjust(60, ' '))
win.setpos(4 + idx, 66)
t, r, w = v.values_at(:total, :read, :write)
.map(&:to_s)
.map{|s| "#{s} ms".ljust(15, ' ')}
win.addstr "total: #{t}read: #{r}write: #{w}"
end
win.refresh
win.close
end
def handle_line result, line
parts = line.split(/\s/).reject(&:empty?)
stop_list = %w[connected ns]
return if stop_list.include?(parts[0]) || parts.empty?
name = parts.shift
total, read, write = parts.map(&:to_i)
result[name] ||= {total: 0, read: 0, write: 0}
result[name][:total] += total
result[name][:read] += read
result[name][:write] += write
end
init_screen
start_time = Time.now
result = {}
begin
crmode
while line = gets
handle_line result, line
print_stats result, Time.now - start_time
end
`say quitting`
sleep(5)
ensure
close_screen
p result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment