Chris Waters has been working on using Ruby to monitor VMware systems using the VI web services API. He has created a simple ruby library which wraps the SOAP access in an easier to use form. The functionality is pretty basic, but it may be helpful for other people who want to use VMware from ruby.
You can access the code here:
http://svn.paglo.com/paglo_open_source/ruby_vmware/trunk/
The code is licensed under the GPL.
Thursday, October 15. 2009
Ruby sample code and library for VI SDK
require 'vmware'
require 'pp'
#
# Dump the inventory as a tree.
#
def dump_inventory(object, level = 0)
puts "%s%s [%s] (%s)" % [" " * level, object["name"], object.object_reference, object.class]
level += 1
case object.class.to_s
when "VMware::Folder"
object.children.each { |child| dump_inventory(child, level) }
when "VMware::Datacenter"
dump_inventory(object.host_folder, level)
object.datastores.each { |child| dump_inventory(child, level) }
when "VMware::ComputeResource"
object.hosts.each { |child| dump_inventory(child, level) }
when "VMware::HostSystem"
object.vms.each { |child| dump_inventory(child, level) }
when "VMware::Datastore", "VMware::VirtualMachine"
nil
else
puts "WARNING: Unhandled class in dump_inventory: #{object.class}"
end
object.properties.each do |prop_name, prop_value|
puts "%s%s => %s" % [" " * level, prop_name, VMware::DataObject::dump(prop_value)]
end
end
@connection = VMware::Connection.new("https://10.10.10.24/sdk", "root", "admin", false)
@session = VMware::Session.new(@connection)
folder = @session.root_folder
#pp folder.properties
#folder.children.each do |child|
# pp child.properties
#end
dump_inventory(folder)
