Created
August 30, 2011 09:22
-
-
Save iconara/1180527 to your computer and use it in GitHub Desktop.
Show queue stats in pretty tables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'open-uri' | |
require 'json' | |
class Queue | |
def initialize(info) | |
@info = info | |
end | |
def name | |
@info['name'] | |
end | |
def host | |
@info['node'].gsub(/^[^@]+@(.+)$/, '\1') | |
end | |
def group_name | |
name.gsub(/^(.+?)\d+$/, '\1') | |
end | |
def size | |
@info['messages_ready'] | |
end | |
def avg_publish_rate | |
@info['backing_queue_status']['avg_ingress_rate'] | |
end | |
def avg_ack_rate | |
@info['backing_queue_status']['avg_ack_egress_rate'] | |
end | |
end | |
class QueueGroup | |
def initialize(name, *queues) | |
@name = name | |
@queues = queues | |
end | |
def name | |
@name | |
end | |
def host | |
@queues.first.host.gsub(/^(.+?)\d+$/, '\1') | |
end | |
def size | |
@queues.map(&:size).reduce(&:+) | |
end | |
def avg_publish_rate | |
@queues.map(&:avg_publish_rate).reduce(&:+) | |
end | |
def avg_ack_rate | |
@queues.map(&:avg_ack_rate).reduce(&:+) | |
end | |
end | |
def load_queue_groups(host, port) | |
queues = JSON.parse(%x(curl -s 'http://guest:guest@#{host}:#{port}/api/queues')).map { |q| Queue.new(q) } | |
queues.group_by { |queue| queue.group_name }.map { |name, queues| QueueGroup.new(name, *queues) } | |
end | |
def now | |
Time.now.strftime('%Y-%m-%d %H:%M:%S') | |
end | |
def print_host(queue_groups) | |
max_queue_name_with = queue_groups.map { |q| q.name.length }.max | |
max_host_name_with = queue_groups.map { |q| q.host.length }.max | |
max_date_width = now.length | |
max_number_witdth = 10 | |
row_formats = [] | |
row_formats << "%-#{max_queue_name_with}s" | |
row_formats << "%#{max_number_witdth}d" | |
row_formats << "%#{max_number_witdth}.1f" | |
row_formats << "%#{max_number_witdth}.1f" | |
header_formats = [] | |
header_formats << "%-#{max_queue_name_with}s" | |
header_formats << "%#{max_number_witdth}s" | |
header_formats << "%#{max_number_witdth}s" | |
header_formats << "%#{max_number_witdth}s" | |
row_format = '| ' + row_formats.join(' | ') + ' |' | |
header_format = '| ' + header_formats.join(' | ') + ' |' | |
header = header_format % %w(Queue Size Publish Ack) | |
divier_format = '+-' + header_formats.join('-+-') + '-+' | |
divider = (divier_format % [max_queue_name_with, max_number_witdth, max_number_witdth, max_number_witdth].map { |n| '-' * n }) | |
banner_format = " %-#{max_host_name_with}s #{' ' * (divider.length - max_host_name_with - max_date_width - 4)} %#{max_date_width}s " | |
queue_groups.group_by { |q| q.host }.each do |host, groups| | |
print_queue_groups(groups, banner_format, row_format, header, divider) | |
end | |
end | |
def print_queue_groups(groups, banner_format, row_format, header, divider) | |
host = groups.first.host | |
puts | |
puts banner_format % [host, now] | |
puts divider | |
puts header | |
puts divider | |
groups.sort_by { |q| q.name }.each do |group| | |
puts row_format % [group.name, group.size, group.avg_publish_rate, group.avg_ack_rate] | |
end | |
puts divider | |
puts | |
end | |
queue_groups = [55672, 55673, 55674, 55675, 55676].flat_map { |port| load_queue_groups('localhost', port) } | |
print_host(queue_groups) | |
__END__ | |
{ | |
"messages"=>8461139, | |
"message_stats"=> | |
{ | |
"publish"=>5210391, | |
"publish_details"=> | |
{ | |
"rate"=>0.7995881577507896, | |
"last_event"=>1314681069612 | |
} | |
}, | |
"consumer_details"=> | |
[ | |
], | |
"exclusive_consumer_pid"=>"", | |
"exclusive_consumer_tag"=>"", | |
"messages_ready"=>8461139, | |
"messages_unacknowledged"=>0, | |
"consumers"=>0, | |
"memory"=>113768776, | |
"backing_queue_status"=> | |
{ | |
"q1"=>0, | |
"q2"=>0, | |
"delta"=> | |
[ | |
"delta", | |
16384, | |
8444755, | |
8461139 | |
], | |
"q3"=>16384, | |
"q4"=>0, | |
"len"=>8461139, | |
"pending_acks"=>0, | |
"outstanding_txns"=>0, | |
"target_ram_count"=>0, | |
"ram_msg_count"=>0, | |
"ram_ack_count"=>0, | |
"ram_index_count"=>1, | |
"next_seq_id"=>8461139, | |
"persistent_count"=>0, | |
"avg_ingress_rate"=>0.9438081185241786, | |
"avg_egress_rate"=>0.0, | |
"avg_ack_ingress_rate"=>0.0, | |
"avg_ack_egress_rate"=>0.0 | |
}, | |
"name"=>"exposures0", | |
"vhost"=>"/", | |
"durable"=>true, | |
"auto_delete"=>false, | |
"owner_pid"=>"none", | |
"arguments"=> | |
{ | |
}, | |
"pid"=>"<[email protected]>", | |
"node"=>"rabbit@richmqcrunch01" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment