Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Realtime console-based vSphere cluster usage monitoring with Ruby
#!/usr/bin/ruby
require 'rubygems'
require 'yaml'
require 'rbvmomi'
require 'ruby-progressbar'
def getStats()
datacenter = $vim.serviceInstance.find_datacenter
datacenter.hostFolder.children.each do |folder|
if ARGV[0]
next unless ARGV[0] == folder.name
end
print folder.name, "\n"
folder.name.length.times { print '-' }
print "\n"
folder.host.each do |host|
print host.name, "\n"
sta = host.overallStatus
con = host.runtime.connectionState
mnt = host.runtime.inMaintenanceMode
if host.summary.quickStats.overallCpuUsage and host.hardware.cpuInfo.numCpuCores and host.hardware.cpuInfo.hz
cpu_val = host.summary.quickStats.overallCpuUsage / ( ( host.hardware.cpuInfo.numCpuCores ) * ( host.hardware.cpuInfo.hz / 102400000 ) )
else
cpu_val = 0
end
cpu_val = 100 if cpu_val > 100
cpu = ProgressBar.create( :title => 'cpu', :length => 40, :progress_mark => "\u2593", :remainder_mark => "\u2591" )
cpu_val.times { cpu.increment }
print cpu, " status: #{sta} (#{con})\n"
if host.summary.quickStats.overallMemoryUsage and host.hardware.memorySize
mem_val = ( host.summary.quickStats.overallMemoryUsage * 1024 * 1024 ) * 100 / ( host.hardware.memorySize )
else
mem_val = 0
end
mem_val = 100 if mem_val > 100
mem = ProgressBar.create( :title => 'mem', :length => 40, :progress_mark => "\u2593", :remainder_mark => "\u2591" )
mem_val.times { mem.increment }
print mem, " maintenance mode: #{mnt}\n"
print "\n"
end
end
end
fog_file = File.expand_path("~/.fog")
fog_data = YAML.load_file(fog_file)[:default]
$vim = RbVmomi::VIM.connect(
:host => fog_data[:vsphere_server],
:user => fog_data[:vsphere_username],
:password => fog_data[:vsphere_password],
:ssl => true,
:insecure => true,
:rev => '5.1'
)
getStats()
$vim.close
$ show-vmware-host-usage.rb acceptance1
acceptance1
-----------
opdx-a0-chassis5-3.example.com
cpu: |░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░| status: green (disconnected)
mem: |░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░| maintenance mode: true
opdx-a0-chassis5-2.example.com
cpu: |▓▓▓▓░░░░░░░░░░░░░░░░░░░░░░░░░░░░░| status: green (connected)
mem: |▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░| maintenance mode: false
opdx-a0-chassis5-4.example.com
cpu: |▓▓▓░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░| status: green (connected)
mem: |▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░| maintenance mode: false
opdx-a0-chassis5-6.example.com
cpu: |▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░░░░░░| status: green (connected)
mem: |▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░| maintenance mode: false
opdx-a0-chassis5-8.example.com
cpu: |▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░░░░░░░░░░| status: green (connected)
mem: |▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░| maintenance mode: false
opdx-a2-chassis6-1.example.com
cpu: |▓▓▓▓▓░░░░░░░░░░░░░░░░░░░░░░░░░░░░| status: green (connected)
mem: |▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░| maintenance mode: false
opdx-a2-chassis6-3.example.com
cpu: |▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░░░░░░░░░░| status: green (connected)
mem: |▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░| maintenance mode: false
opdx-a2-chassis6-4.example.com
cpu: |▓▓▓▓░░░░░░░░░░░░░░░░░░░░░░░░░░░░░| status: green (connected)
mem: |▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░| maintenance mode: false
opdx-a0-chassis5-1.example.com
cpu: |▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░| status: green (connected)
mem: |▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░░░░░░| maintenance mode: false
opdx-a0-chassis5-7.example.com
cpu: |▓▓▓▓▓░░░░░░░░░░░░░░░░░░░░░░░░░░░░| status: green (connected)
mem: |▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░| maintenance mode: false
opdx-a2-chassis6-2.example.com
cpu: |▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░░░░░░░░| status: green (connected)
mem: |▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░░░░░| maintenance mode: false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.