Last active
October 1, 2018 20:08
-
-
Save kapkaev/ef633348dbecfedc0166 to your computer and use it in GitHub Desktop.
Calculate kafka topics sizes
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
#!/usr/bin/ruby | |
require 'net/ssh' | |
require 'pp' | |
def fetch(host) | |
data = [] | |
Net::SSH.start(host, 'virool') do |ssh| | |
index_folders = ssh.exec!("ls -d /var/lib/kafka/*").split("\n") | |
data = index_folders.flat_map do |ifolder| | |
topics = ssh.exec!("ls -d #{ifolder}/*").split("\n") | |
calculate_size(topics, ssh) | |
end | |
end | |
end | |
def calculate_size(topics, ssh) | |
topics.map do |topic| | |
size_in_bytes, _ = ssh.exec!("du -cs #{topic}/*.log | tail -1").split("\t") | |
t = File.basename(topic).gsub(/-\d/, '').gsub(/\d/, '') | |
p "#{topic} with #{size_in_bytes.to_i/1024} MB" | |
[t, size_in_bytes.to_i/1024] | |
end | |
end | |
def calculate_topics_size(nodes) | |
Hash.new(0).tap do |result| | |
nodes.each do |host| | |
fetch(host).each do |k,v| | |
result[k] += v | |
end | |
end | |
end | |
end | |
def humanize(data) | |
data.each do |k,v| | |
data[k] = "#{v/1024} GB" | |
end | |
end | |
pp humanize calculate_topics_size(['kafka01']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment