Skip to content

Instantly share code, notes, and snippets.

@duckworth
Created August 29, 2014 15:24
Show Gist options
  • Save duckworth/1cb8c6bf39ec41a6cf1f to your computer and use it in GitHub Desktop.
Save duckworth/1cb8c6bf39ec41a6cf1f to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems'
require 'net/ssh'
#need to install gem 'parallel'
require 'parallel'
#Path to file with hosts in it, one per line
HOSTFILE = "hosts.txt"
USER = 'root'
#ssh key path
KEYS = [ "~/.ssh/key" ]
#command to run that outputs hostname, memory, disk and cores
COMMAND = <<-STR
echo -e -n "$HOSTNAME\t" && free -m | awk 'NR==2{printf "%s\t",$2 }' && df -lP | awk '{total+=$2} END {printf "%d\t", total/(1024*1024) + 0.5}' && awk '$1=="processor"{ count++ } END { printf "%d", count }' /proc/cpuinfo
STR
def fetch_info_into_array(hostname, array)
begin
Net::SSH.start(hostname, USER, :keys => KEYS ) do|ssh|
array << ssh.exec!(COMMAND) + "\n"
end
rescue Exception => e
puts "#{hostname} EXCEPTION" + e.to_s
array << "#{hostname}\tunknown\n"
end
end
output = ["Hostname\tMemory MB\tDisk Size Total GB\tCPU Cores\n"]
File.open(HOSTFILE, "r") do |f|
Parallel.each(f.each_line, :in_threads => 8) do |line|
fetch_info_into_array(line.strip, output)
end
end
#save result to file
File.open('output.txt', 'w') {|f| f.puts(output) }
puts output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment