From ef312ceba5b0028444ecd51bd37bece294cbe72a Mon Sep 17 00:00:00 2001 From: Tino Vazquez Date: Fri, 17 May 2013 18:58:07 +0200 Subject: [PATCH] feature #1977: Add host monitoring --- install.sh | 3 +- share/install_gems/install_gems | 5 +- src/im_mad/remotes/vmware.d/vmware.rb | 98 ++++++------------------- src/vmm_mad/remotes/vmware/vi_driver.rb | 96 ++++++++++++++++++++---- 4 files changed, 108 insertions(+), 94 deletions(-) diff --git a/install.sh b/install.sh index 38eb8ba46c..8906582541 100755 --- a/install.sh +++ b/install.sh @@ -772,7 +772,8 @@ VMM_EXEC_VMWARE_SCRIPTS="src/vmm_mad/remotes/vmware/cancel \ src/vmm_mad/remotes/vmware/poll \ src/vmm_mad/remotes/vmware/checkpoint \ src/vmm_mad/remotes/vmware/shutdown \ - src/vmm_mad/remotes/vmware/vmware_driver.rb" + src/vmm_mad/remotes/vmware/vmware_driver.rb \ + src/vmm_mad/remotes/vmware/vi_driver.rb" #------------------------------------------------------------------------------- # Information Manager Probes, to be installed under $REMOTES_LOCATION/im diff --git a/share/install_gems/install_gems b/share/install_gems/install_gems index 63941923aa..8845fbb3fb 100755 --- a/share/install_gems/install_gems +++ b/share/install_gems/install_gems @@ -4,7 +4,7 @@ require 'pp' $nokogiri='nokogiri' -DEFAULT=%w{sunstone quota cloud ozones_server auth_ldap} +DEFAULT=%w{sunstone quota cloud ozones_server auth_ldap vmware} if defined?(RUBY_VERSION) && RUBY_VERSION>="1.8.7" SQLITE='sqlite3' @@ -22,7 +22,8 @@ GROUPS={ ], :ozones_server_sqlite => %w{json sequel}< %w{json sequel mysql}, - :auth_ldap => 'net-ldap' + :auth_ldap => 'net-ldap', + :vmware => 'rbvmomi' } PACKAGES=GROUPS.keys diff --git a/src/im_mad/remotes/vmware.d/vmware.rb b/src/im_mad/remotes/vmware.d/vmware.rb index 70fbd67d31..20a32d1d98 100755 --- a/src/im_mad/remotes/vmware.d/vmware.rb +++ b/src/im_mad/remotes/vmware.d/vmware.rb @@ -16,86 +16,30 @@ # limitations under the License. # # ---------------------------------------------------------------------------- # -require 'rubygems' -require 'rbvmomi' -require 'pp' +ONE_LOCATION=ENV["ONE_LOCATION"] if !defined?(ONE_LOCATION) -def init_client(hostname, user, password) - @client = RbVmomi::VIM.connect(:host => hostname, - :user => user, - :password => password, - :insecure => true) - @rootFolder = @client.serviceInstance.content.rootFolder +if !ONE_LOCATION + RUBY_LIB_LOCATION="/usr/lib/one/ruby" if !defined?(RUBY_LIB_LOCATION) +else + RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" if !defined?(RUBY_LIB_LOCATION) end -# Get hold of a VM by name -def get_vm(name) - vm = {} - # Traverse all datacenters - @rootFolder.childEntity.grep(RbVmomi::VIM::Datacenter).each do |dc| - # Traverse all datastores - dc.datastoreFolder.childEntity.collect do |ds| - # Find the VM with "name" - vm[:vm] = ds.vm.find { |v| v.name == name } - if vm[:vm] - vm[:ds] = ds.name - break - end - end - end - return vm +$: << "../../vmm/vmware/" + +require 'vi_driver' + +host = ARGV[2] + +if !host + exit -1 end -# Get hold of a host by name -def get_host(name) - host = {} - @rootFolder.childEntity.grep(RbVmomi::VIM::Datacenter).first.hostFolder.children.first.host.each { |h| - if h.name == name - host = h - break - end - } - return host +vi_drv = VIDriver.new(host) + +results = vi_drv.poll_host_and_vms + +if results != -1 + puts results +else + exit results.to_i end - -def get_used_memory(vm) - vm.summary.quickStats.hostMemoryUsage.to_s -end - -def get_used_cpu(vm, host) - overallCpuUsage = vm.summary.quickStats.overallCpuUsage.to_f - cpuMhz = host.summary.hardware.cpuMhz.to_f - numCpuCores = host.summary.hardware.numCpuCores.to_f - (overallCpuUsage / (cpuMhz * numCpuCores)).round(3).to_s -end - -def get_state(vm) - case vm.summary.runtime.powerState - when "poweredOn" - "a" - when "suspended" - "p" - else - "d" - end -end - -def get_info(vm, host) - str_info = "" - str_info += "USEDCPU="+get_used_cpu(vm, host)+" " - str_info += "USEDMEMORY="+get_used_memory(vm)+" " - str_info += "STATE="+get_state(vm)+" " -end - -begin - init_client(hostname,username,password) - - vm = get_vm(vm_name) - host = get_host(hostname) - str_info = get_info(vm[:vm], host) -rescue Exception => e - str_info = "STATE=d" -end - -pp str_info - diff --git a/src/vmm_mad/remotes/vmware/vi_driver.rb b/src/vmm_mad/remotes/vmware/vi_driver.rb index 26d161005e..ac94fc7bf7 100644 --- a/src/vmm_mad/remotes/vmware/vi_driver.rb +++ b/src/vmm_mad/remotes/vmware/vi_driver.rb @@ -65,8 +65,11 @@ class VIDriver :user => conf[:username], :password => pass, :insecure => true) - @rootFolder = @client.serviceInstance.content.rootFolder - @host = get_host(hostname) + @rootFolder = @client.serviceInstance.content.rootFolder + @host = get_host(hostname) + # Get values to be used in all of this class instantiations + @cpuMhz = @host.summary.hardware.cpuMhz.to_f + @numCpuCores = @host.summary.hardware.numCpuCores.to_f rescue Exception => e raise "Connection error to #{hostname}: " + e.message end @@ -75,7 +78,7 @@ class VIDriver # -------------------------------------------------------------------------# # Poll the monitoring information for a VM # # -------------------------------------------------------------------------# - def poll(deploy_id) + def poll_vm(deploy_id) begin vm = get_vm(deploy_id)[:vm] str_info = get_vm_info(vm) @@ -88,11 +91,30 @@ class VIDriver # -------------------------------------------------------------------------# # Poll the monitoring information for a VM # + # -------------------------------------------------------------------------# + def poll_host_and_vms + begin + str_info = get_host_info + vms_info = get_all_vms_info + str_info += " " + vms_info if vms_info + rescue Exception => e + STDERR.puts e.message + return -1 + end + return str_info + end + + ############################################################################ + # Private Methods # + ############################################################################ + # -------------------------------------------------------------------------# - def poll_all_vms + # Poll the monitoring information for a VM # + # -------------------------------------------------------------------------# + def get_all_vms_info begin vms = get_all_vms - str_info = "VM_POLL=YES " + str_info = "VM_POLL=YES " if vms.length > 0 vms.each do |vm| number = -1 @@ -112,10 +134,6 @@ class VIDriver return str_info end - ############################################################################ - # Private Methods # - ############################################################################ - # -------------------------------------------------------------------------# # Get hold of a VM by name # # -------------------------------------------------------------------------# @@ -170,26 +188,76 @@ class VIDriver net_info[vm][:metrics]["net.packetsTx"].first.to_s end + # -------------------------------------------------------------------------# + # Get information of the initialized host # + # -------------------------------------------------------------------------# + def get_host_info + str_info = "" + # CPU + total_cpu = @host.summary.hardware.numCpuCores*100 + used_cpu = get_host_used_cpu + + str_info += "MODELNAME=\""+ @host.summary.hardware.cpuModel.to_s + "\" " + str_info += "CPUSPEED=" + @host.summary.hardware.cpuMhz.to_s + " " + str_info += "TOTALCPU=" + total_cpu.to_s + " " + str_info += "USEDCPU=" + used_cpu.to_s + " " + str_info += "FREECPU=" + (total_cpu - used_cpu).to_s + " " + + # Memory + total_memory = get_host_total_memory + used_memory = get_host_used_memory + str_info += "TOTALMEMORY=" + total_memory.to_s + " " + str_info += "USEDMEMORY=" + used_memory.to_s + " " + str_info += "FREEMEMORY=" + (total_memory - used_memory).to_s + " " + + # Networking + net_info = get_perf_value([@host], ["net.packetsRx","net.packetsTx"]) + str_info += "NETRX=" + + net_info[@host][:metrics]["net.packetsRx"].first.to_s + " " + str_info += "NETTX=" + + net_info[@host][:metrics]["net.packetsTx"].first.to_s + end + + # -------------------------------------------------------------------------# + # Get used CPO of the initialized host # + # -------------------------------------------------------------------------# + def get_host_used_cpu + overallCpuUsage = @host.summary.quickStats.overallCpuUsage.to_f + (overallCpuUsage / (@cpuMhz * @numCpuCores)).round(3)*100 + end + + # -------------------------------------------------------------------------# + # Get total memory of the initialized host # + # -------------------------------------------------------------------------# + def get_host_total_memory + @host.summary.hardware.memorySize/1024 + end + + # -------------------------------------------------------------------------# + # Get used memory of the initialized host # + # -------------------------------------------------------------------------# + def get_host_used_memory + @host.summary.quickStats.overallMemoryUsage*1024 + end + # -------------------------------------------------------------------------# # Get all VMs available in current @hostname # # -------------------------------------------------------------------------# def get_all_vms - @host.vm.collect { |vm| vm if get_state(vm)=="a"}.compact + @host.vm.collect { |vm| vm if get_state(vm)=="a" }.compact end # -------------------------------------------------------------------------# # Get used memory of a VM # # -------------------------------------------------------------------------# def get_used_memory(vm) - vm.summary.quickStats.hostMemoryUsage.to_s + (vm.summary.quickStats.hostMemoryUsage.*1024).to_s end # Get percentage of the CPU used by a VM in a host def get_used_cpu(vm) overallCpuUsage = vm.summary.quickStats.overallCpuUsage.to_f - cpuMhz = @host.summary.hardware.cpuMhz.to_f - numCpuCores = @host.summary.hardware.numCpuCores.to_f - (overallCpuUsage / (cpuMhz * numCpuCores)).round(3).to_s + ((overallCpuUsage / (@cpuMhz * @numCpuCores)).round(3)*100).to_s end # -------------------------------------------------------------------------#