Skip to content

Instantly share code, notes, and snippets.

@renchap
Created October 1, 2013 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renchap/6780612 to your computer and use it in GitHub Desktop.
Save renchap/6780612 to your computer and use it in GitHub Desktop.
Summarize the hardware resources for a set of chef nodes
# hostname patters to search
patterns=["a-search*", "b-search*"]
TERA_SIZE = 1073741824.0*1024
GIGA_SIZE = 1073741824.0
MEGA_SIZE = 1048576.0
KILO_SIZE = 1024.0
# Return the file size with a readable style.
def human_size(size, precision=3)
if size == 1
"1 Byte"
elsif size < KILO_SIZE
"%d Bytes" % size
elsif size < MEGA_SIZE
"%.#{precision}f KB" % (size / KILO_SIZE)
elsif size < GIGA_SIZE
"%.#{precision}f MB" % (size / MEGA_SIZE)
elsif size < TERA_SIZE
"%.#{precision}f GB" % (size / GIGA_SIZE)
else
"%.#{precision}f TB" % (size / TERA_SIZE)
end
end
def human_freq(mhz, precision=3)
if mhz < 10**3
"%d Mhz" % mhz
elsif mhz < 10**6
"%.#{precision}f GHz" % (mhz / 10**3.to_f)
elsif mhz < 10**9
"%.#{precision}f THz" % (mhz / 10**6.to_f)
elsif mhz < 10**12
"%.#{precision}f PHz" % (mhz / 10**9.to_f)
else
"#{mhz} MHz"
end
end
cpus = 0
memory = 0
cpu_freq = 0
patterns.each { |pattern|
nodes.find(:bootstrap_hostname => pattern).each { |node|
if node.has_key?('cpu') then
cpus += node.cpu.total.to_i
if node['cpu'].has_key?('0')
cpu_freq+=node['cpu']['0']['mhz'].to_i*node.cpu.total.to_i
else
cpu_freq+=node['cpu']['mhz'].to_i*node.cpu.total.to_i
end
if node.memory.total =~ /kB/
memory+=node.memory.total.gsub("kB","").to_i*1024
else
memory+=node.memory.total.to_i
end
end
}
}
puts "Search total CPUs : #{cpus}"
puts "Search CPU frequency : #{human_freq(cpu_freq)}"
puts "Search total memory : #{human_size(memory)}"
cpus=0
memory=0
cpu_freq = 0
hosts = 0
nodes.list.each { |node|
begin
if node.has_key?('cpu') then
hosts+=1
cpus += node.cpu.total.to_i
if node['cpu'].has_key?('0')
cpu_freq+=node['cpu']['0']['mhz'].to_i*node.cpu.total.to_i
else
cpu_freq+=node['cpu']['mhz'].to_i*node.cpu.total.to_i
end
if node.memory.total =~ /kB/
memory+=node.memory.total.gsub("kB","").to_i*1024
else
memory+=node.memory.total.to_i
end
end
rescue Exception => e
puts "Oops !"
puts node.name
raise e
end
}
puts "number of hosts : #{hosts}"
puts "total CPUs : #{cpus}"
puts "total CPU frequency : #{human_freq(cpu_freq)}"
puts "total memory : #{human_size(memory)}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment