Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Compiles the CPU, RAM, and Disk usage of our Pivotal Cloud environments
#!/usr/bin/env ruby
require 'rbvmomi'
def vCenter_session
# host is a vCenter, not an ESXi host
RbVmomi::VIM.connect host: ENV['VCENTER_HOST'], user: 'root', password: ENV['VCENTER_PASSWORD']
end
def find_datacenter(datacenter_name)
vCenter_session.serviceInstance.find_datacenter(datacenter_name)
end
def find_cluster(datacenter)
datacenter.hostFolder.children
end
def find_resource_pools(clusters)
resource_pools = []
clusters.each do |cluster|
resource_pools.push(cluster.resourcePool.resourcePool)
end
resource_pools.flatten
end
def non_empty_resource_pools(resource_pools)
resource_pools.select { |rp| !rp.vm.empty? }
end
# def get_vm_info(vm)
# memory = vm.summary.config.memorySize
# num_cpu = vm.summary.config.numCpu
# hd_size = vm.summary.storage.committed
# end
datacenter = find_datacenter("private")
clusters = find_cluster(datacenter)
resource_pools = find_resource_pools(clusters)
# resource_pools.each do |rp|
# puts rp.name
# end
def to_kb(kib)
kib / 1024.0 * 1000
end
non_empty_resource_pools = resource_pools.select { |rp| !rp.vm.empty? }
#print "RESOURCE_POOL,INSTANCE_NAME,MEMORY,DISK_COUNT,DISKS,TOTAL\n"
print "Resource Pool Disk (GB) RAM (GiB) VMs\n"
non_empty_resource_pools.each do |rp|
total_rp_vms = 0
total_rp_disk = 0
total_rp_ram = 0
# print rp.name + "\n"
rp.vm.each do |vm|
#print rp.name + "," + vm.name + "," + vm.summary.config.memorySizeMB.to_s + "," + vm.disks.count.to_s + ","
last_disk = vm.disks[-1]
total_disk=0
vm.disks.each do |disk|
# convert kiB to kB
total_disk=total_disk+to_kb(disk.capacityInKB)
unless disk == last_disk
#print (disk.capacityInKB/1024).to_s + "+"
else
#print (disk.capacityInKB/1024).to_s
end
end
total_rp_vms += 1
total_rp_ram += vm.summary.config.memorySizeMB
total_rp_disk += total_disk
#print ", #{total_disk}\n"
end
print "#{ '%-13s' % rp.name } #{ '%9d' % (total_rp_disk/1000000) } #{ '%9d' % (total_rp_ram/1024)} #{'%3d' % total_rp_vms}\n"
end
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.