lib/redis-stat.rb in redis-stat-0.1.4 vs lib/redis-stat.rb in redis-stat-0.2.0

- old
+ new

@@ -3,18 +3,23 @@ require 'insensitive_hash' require 'redis' require 'tabularize' require 'ansi' require 'csv' +require 'parallelize' class RedisStat DEFAULT_TERM_WIDTH = 180 DEFAULT_TERM_HEIGHT = 25 def initialize options = {} @options = RedisStat::Option::DEFAULT.merge options - @redis = Redis.new(Hash[ @options.select { |k, _| [:host, :port].include? k } ]) + @hosts = @options[:hosts] + @redises = @hosts.map { |e| + host, port = e.split(':') + Redis.new(Hash[ {:host => host, :port => port}.select { |k, v| v } ]) + } @max_count = @options[:count] @count = 0 @colors = @options[:colors] || COLORS end @@ -23,16 +28,34 @@ trap('INT') { Thread.main.raise Interrupt } csv = File.open(@options[:csv], 'w') if @options[:csv] update_term_size! - prev_info = nil + begin + # Warm-up + @redises.each { |r| r.info } + @started_at = Time.now + prev_info = nil loop do - info = @redis.info.insensitive + info = {}.insensitive + class << info + def sumf label + (self[label] || []).map(&:to_f).inject(:+) + end + end + info[:at] = Time.now.to_f + @redises.pmap(@redises.length) { |redis| + redis.info.insensitive + }.each do |rinfo| + rinfo.each do |k, v| + info[k] ||= [] + info[k] << v + end + end output info, prev_info, csv prev_info = info @count += 1 @@ -96,13 +119,13 @@ init_table info_output unless @table movement = nil if @count == 0 - movement = 0 output_static_info info + movement = 0 if file file.puts CSV.generate_line(info_output.map { |pair| LABELS[pair.first] || pair.first }) end @@ -145,42 +168,41 @@ raise end end def output_static_info info - data = - { - :redis_stat_version => RedisStat::VERSION, - :redis_host => @options[:host], - :redis_port => @options[:port], - :csv => @options[:csv], - }.merge( - Hash[ - [ - :redis_version, - :process_id, - :uptime_in_seconds, - :uptime_in_days, - :gcc_version, - :role, - :connected_slaves, # FIXME: not so static - :aof_enabled, - :vm_enabled - ].map { |k| [k, info[k]] } - ] - ).reject { |k, v| v.nil? }.to_a - @os.puts Tabularize.it(data, :align => :left).map { |pair| - ansi(:bold) { pair.first } + ' : ' + pair.last - } + tab = Tabularize.new( + :unicode => false, :align => :right, + :hborder => ansi(:black, :bold) { '-' }, + :vborder => ansi(:black, :bold) { '|' }, + :iborder => ansi(:black, :bold) { '+' } + ) + tab << [nil] + @hosts.map { |h| ansi(:bold, :green) { h } } + tab.separator! + [ + :redis_version, + :process_id, + :uptime_in_seconds, + :uptime_in_days, + :gcc_version, + :role, + :connected_slaves, + :aof_enabled, + :vm_enabled + ].each do |key| + tab << [ansi(:bold) { key }] + info[key] + end + @os.puts tab end def init_table info_output @table = Tabularize.new :unicode => false, :align => :right, :hborder => ansi(:black, :bold) { '-' }, :iborder => ansi(:black, :bold) { '+' }, :vborder => ' ', + :ellipsis => ansi(:bold) { '>' }, :pad_left => 0, :pad_right => 0, :screen_width => @term_width @table << info_output.map { |pair| ansi(*((@colors[pair.first] || []) + [:underline])) { @@ -198,12 +220,12 @@ def process_how info, prev_info, key dur = prev_info && (info[:at] - prev_info[:at]) get_diff = lambda do |label| - if dur - (info[label].to_f - prev_info[label].to_f) / dur + if dur && dur > 0 + (info.sumf(label) - prev_info.sumf(label)) / dur else nil end end @@ -212,23 +234,25 @@ Time.now.strftime('%H:%M:%S') when :used_cpu_user, :used_cpu_sys val = get_diff.call(key) [humanize_number(val), val] when :keys - val = Hash[ info.select { |k, v| k =~ /^db[0-9]+$/ } ].values.inject(0) { |sum, v| - sum + Hash[ v.split(',').map { |e| e.split '=' } ]['keys'].to_i + val = Hash[ info.select { |k, v| k =~ /^db[0-9]+$/ } ].values.inject(0) { |sum, vs| + sum + vs.map { |v| Hash[ v.split(',').map { |e| e.split '=' } ]['keys'].to_i }.inject(:+) } [humanize_number(val), val] when :evicted_keys_per_second, :expired_keys_per_second, :keyspace_hits_per_second, :keyspace_misses_per_second, :total_commands_processed_per_second val = get_diff.call(key.to_s.gsub(/_per_second$/, '').to_sym) [humanize_number(val), val] when :total_commands_processed, :evicted_keys, :expired_keys, :keyspace_hits, :keyspace_misses - [humanize_number(info[key].to_i), info[key]] + val = info.sumf(key) + [humanize_number(val.to_i), val] when :used_memory, :used_memory_rss, :aof_current_size, :aof_base_size - [humanize_number(info[key].to_i, 1024, 'B'), info[key]] + val = info.sumf(key) + [humanize_number(val.to_i, 1024, 'B'), val] else - info[key] + format_number info.sumf(key) end end def format_number num if num.to_i == num